TB6612 Motor Drive Module
Compared with the traditional L298N, this module is much more efficient and significantly smaller in size. Within the rated range, the chip basically does not heat up — which of course also makes it more delicate. Therefore, we recommend it for users with some hands-on experience. Be extremely careful when wiring, and pay attention to polarity.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-19589090137.14.b3db36b42q0dfQ&id=616285586821
Specifications
VM motor voltage: <12V VCC chip voltage: 2.7~5.5V Output current: 1A Control method: PWM
View Materials
The STBY pin is connected to the MCU's IO pin. When STBY is set to 0, all motors stop; when set to 1, it can work. After STBY is set to 1, the forward/reverse rotation is controlled via AIN1, AIN2, BIN1, BIN2.
The A side (AIN1 and AIN2) can only control the AO1 and AO2 terminals. The B side (BIN1 and BIN2) can only control the BO1 and BO2 terminals. Therefore, it is a dual-channel motor driver.
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. Just note that here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_tb6612.c and bsp_tb6612.h, and the folder name to TB6612.
Write Code
In the file bsp_tb6612.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-15 LCKFB-lp first version
*/
#include "bsp_tb6612.h"
/************************************************
Function Name : TB6612_Init
Description : TB6612 configuration
Parameters :
Return Value : None
Author :
*************************************************/
void TB6612_Init(void)
{
gpio_config_t OUT_config = {
.pin_bit_mask = (1ULL<<GPIO_AIN1)|(1ULL<<GPIO_AIN2), // Configure pins
.mode =GPIO_MODE_OUTPUT, // Output mode
.pull_up_en = GPIO_PULLUP_DISABLE, // Disable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&OUT_config);
// Prepare and apply the PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = PWMA_MODE, // Low speed mode
.timer_num = PWMA_TIMER, // Timer source of the channel Timer 0
.duty_resolution = LEDC_DUTY_RES, // Set duty cycle resolution to 13 bits
.freq_hz = LEDC_FREQUENCY, // Set output frequency to 5 kHz
.clk_cfg = LEDC_AUTO_CLK // Set the LED PWM clock source to automatic
//LEDC_AUTO_CLK = When starting the timer, the source clock will be automatically selected based on the given resolution and duty cycle parameters
};
ledc_timer_config(&ledc_timer);
// Prepare and apply the LEDC PWM channel configuration
ledc_channel_config_t ledc_channel = {
.speed_mode = PWMA_MODE, // Low speed mode
.channel = PWMA_CHANNEL, // Channel 0
.timer_sel = PWMA_TIMER, // Timer source Timer 0
.intr_type = LEDC_INTR_DISABLE, // Disable interrupt
.gpio_num = GPIO_PWMA, // Output pin GPIO1
.duty = 0, // Set duty cycle to 0
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
}
/******************************************************************
* Function Name: AO_Control
* Function Description: Motor control of port A
* Function Parameters: dir rotation direction 1 forward 0 reverse speed rotation speed, range (0 ~ per-1)
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void AO_Control(uint8_t dir, uint32_t speed)
{
if( dir == 1 )
{
AIN1_OUT(0);
AIN2_OUT(1);
}
else
{
AIN1_OUT(1);
AIN2_OUT(0);
}
// Set duty cycle
ledc_set_duty(PWMA_MODE, PWMA_CHANNEL, speed);
ledc_update_duty(PWMA_MODE, PWMA_CHANNEL);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
In the file bsp_tb6612.h, write the following code.
#ifndef _BSP_TB6612_H
#define _BSP_TB6612_H
#include <stdio.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "driver/spi_common.h"
#include "hal/gpio_types.h"
#include "driver/ledc.h"
#include "string.h"
#define GPIO_AIN1 7
#define GPIO_AIN2 8
#define GPIO_PWMA 1
#define PWMA_TIMER LEDC_TIMER_0 // Timer 0
#define PWMA_CHANNEL LEDC_CHANNEL_0 // Use LEDC channel 0
#define PWMA_MODE LEDC_LOW_SPEED_MODE // Low speed mode
#define LEDC_DUTY_RES LEDC_TIMER_13_BIT // LEDC resolution set to 13 bits
#define LEDC_DUTY (4095) // Set duty cycle to 50%. ((2^13) - 1) * 50% = 4095
#define LEDC_FREQUENCY (5000) // Frequency unit is Hz. Set frequency to 5000 Hz
#define AIN1_OUT(X) gpio_set_level(GPIO_AIN1, X?1:0)
#define AIN2_OUT(X) gpio_set_level(GPIO_AIN2, X?1:0)
void TB6612_Init(void);
void AO_Control(uint8_t dir, uint32_t speed);
#endif /* _BSP_TB6612_H */2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Porting Verification
In the main function of your project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-15 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_tb6612.h"
#include "string.h"
#include "esp_private/esp_task_wdt.h"
#include "esp_private/esp_task_wdt_impl.h"
int app_main(void)
{
int i = 0;
esp_task_wdt_deinit();
TB6612_Init();
printf("WS2812 Start......\r\n");
while(1)
{
i += 500;
if( i > 5000 ) i = 0;
AO_Control( 0 , i );// Reverse rotation of motor on side A
printf("PWM: %d \r\n",i);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Driver code:
File Download
📌 Materials Download Center (Click to Jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.