DRV8833 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. The DRV8833 motor drive module can directly replace the TB6612 driver module, with the pins basically fully compatible.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-19589090137.12.51d836b4bfdLAi&id=613483776328
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. speed is the duty cycle controlled by PWM, and the motor speed is controlled through the duty cycle.
Porting Process
Pin Selection
The DRV8833 controls the motor speed by connecting IN1 and IN2 to PWM and directly adjusting the duty cycle of the PWM for speed control. Therefore, AIN1/AIN2/BIN1/BIN2 all need to use the PWM function. Here, STBY is connected to 3.3V so that it remains at a high level all the time; subsequent motor stop control is performed through IN1 and IN2.
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_drv8833.c and bsp_drv8833.h, and the folder name to DRV8833.
Write Code
In the file bsp_drv8833.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_drv8833.h"
/************************************************
Function Name : DRV8833_Init
Description : DRV8833 configuration
Parameters :
Return Value : None
Author :
*************************************************/
void DRV8833_Init(void)
{
// Prepare and apply the PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = PWM_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 LEDC1 PWM channel configuration
ledc_channel_config_t ledc1_channel = {
.speed_mode = PWM_MODE, // Low speed mode
.channel = PWMA1_CHANNEL, // Channel 0
.timer_sel = PWMA_TIMER, // Timer source Timer 0
.intr_type = LEDC_INTR_DISABLE, // Disable interrupt
.gpio_num = GPIO_AIN1, // Output pin GPIO1
.duty = 0, // Set duty cycle to 0
.hpoint = 0
};
ledc_channel_config(&ledc1_channel);
// Prepare and apply the LEDC2 PWM channel configuration
ledc_channel_config_t ledc2_channel = {
.speed_mode = PWM_MODE, // Low speed mode
.channel = PWMA2_CHANNEL, // Channel 1
.timer_sel = PWMA_TIMER, // Timer source Timer 0
.intr_type = LEDC_INTR_DISABLE, // Disable interrupt
.gpio_num = GPIO_AIN2, // Output pin GPIO2
.duty = 0, // Set duty cycle to 0
.hpoint = 0
};
ledc_channel_config(&ledc2_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 )
{
// Set AIN1 duty cycle
ledc_set_duty(PWM_MODE, PWMA1_CHANNEL, 0);
ledc_update_duty(PWM_MODE, PWMA1_CHANNEL);
// Set AIN2 duty cycle
ledc_set_duty(PWM_MODE, PWMA2_CHANNEL, speed);
ledc_update_duty(PWM_MODE, PWMA2_CHANNEL);
}
else
{
// Set AIN1 duty cycle
ledc_set_duty(PWM_MODE, PWMA1_CHANNEL, speed);
ledc_update_duty(PWM_MODE, PWMA1_CHANNEL);
// Set AIN2 duty cycle
ledc_set_duty(PWM_MODE, PWMA2_CHANNEL, 0);
ledc_update_duty(PWM_MODE, PWMA2_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
81
82
83
84
85
86
87
88
89
90
91
92
In the file bsp_drv8833.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 1
#define GPIO_AIN2 2
#define PWMA_TIMER LEDC_TIMER_0 // Timer 0
#define PWMA1_CHANNEL LEDC_CHANNEL_0 // Use LEDC channel 0
#define PWMA2_CHANNEL LEDC_CHANNEL_1 // Use LEDC channel 0
#define PWM_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 DRV8833_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
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_drv8833.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();
DRV8833_Init();
printf("DRV8833 Start......\r\n");
while(1)
{
i += 500;
if( i > 6000 ) i = 0;
AO_Control(0,i);// Reverse rotation of motor on side A
printf("PWM-AO: %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
Power-on effect:
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.