Welcome to L-Type Motor repository
For ESP32
Drive motor and read encoder-IIC
1.1 Explanation
Please read 《0. Motor introduction and usage》first to understand the motor parameters, wiring method, and power supply voltage you are currently using. To avoid improper operation and damage to the driver board or motor.
I2C and serial communication cannot be shared, only one can be selected.
This course uses the YahboomESP32 image transmission module lite version.
If you use other ESP32-S3 boards, you need to modify the pin settings in the program according to your board pin situation.
Use ESP-IDF 5.4.0 version to compile the project.
Hardware wiring:

| Motor | 4-channel motor drive board(Motor) |
|---|---|
| M2 | M- |
| V | 3V3 |
| A | H1A |
| B | H1B |
| G | GND |
| M1 | M+ |
| 4-channel motor drive board | ESP32-S3 |
|---|---|
| SDA | SDA |
| SCL | SCL |
| GND | GND |
| 5V | 5V |
USB to TTL serial port module needs to be connected, mainly for printing data.
When using the Yahboom ESP32 image transmission module lite version, the 3V3 of the USB to TTL serial port module must be replaced with 5V and connected to the 5V on the ESP32 board to write the program normally.
After the program is written, it can be switched back to 3V3.
| USB TO TTL | ESP32S3 |
|---|---|
| 3V3 | 3V3 |
| TXD | RX0 |
| RXD | TX0 |
| GND | GND |
Serial port configuration: Baud rate 115200, no parity check, no hardware flow control, 1 stop bit
Note: The serial port here is used to print data on the serial port assistant, not for communication with the driver board
1.2 Code analysis
// SCL// SDAThis code is defined in the file i2c_module.h.
If you need to change the pin for I2C communication, you can modify the number here.
x// 1:接收总的编码器数据 2:接收实时的编码器 1: Receive total encoder data 2: Receive real-time encoder data
//1:520电机 2:310电机 3:测速码盘TT电机 4:TT直流减速电机 5:L型520电机 //1:520 motor 2:310 motor 3:speed code disc TT motor 4:TT DC reduction motor 5:L type 520 motorUPLOAD_DATA: used to set the data of the motor encoder. Set 1 to the total number of encoder pulses and 2 to the real-time pulse data of 10ms.
MOTOR_TYPE: used to set the type of motor used. Just modify the corresponding numbers according to the comments according to the motor you are currently using. You don’t need to modify the rest of the code.
If you need to drive the motor and observe the data, just modify the two numbers at the beginning of the program. No changes are required to the rest of the code.
xxxxxxxxxx Set_motor_type(1);//配置电机类型 Configure motor type delay(100); Set_Pluse_Phase(30);//配置减速比 查电机手册得出 Configure the reduction ratio. Check the motor manual to find out delay(100); Set_Pluse_line(11);//配置磁环线 查电机手册得出 Configure the magnetic ring wire. Check the motor manual to get the result. delay(100); Set_Wheel_dis(67.00);//配置轮子直径,测量得出 Configure the wheel diameter and measure it delay(100); Set_motor_deadzone(1900);//配置电机死区,实验得出 Configure the motor dead zone, and the experiment shows delay(100); Set_motor_type(2); delay(100); Set_Pluse_Phase(20); delay(100); Set_Pluse_line(13); delay(100); Set_Wheel_dis(48.00); delay(100);