yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
520 Motor📖
1.About motor
  • About motor
2.ROS robot control board drive
  • 2.1 Control motor
  • 2.2 Timer captures encoder data
  • 2.3 PID control robot car
  • 2.4 Kinematic Analysis-Mecanum wheel structure
  • 2.5 Kinematic Analysis-Ackerman structure
  • 2.6 Kinematic Analysis-4WD structure
3.STM32 Self-balancing car tutorials
Get started quickly
  • 03.P, PI, PD controller theory
  • 01.Balancing car assembly
  • 02.Get started quicklyy
  • 03.Balance car interface description
  • 04.Common Problem Analysis
  • 05.Balance Car V2 Library Function Development Manual
Environment construction and development
  • 01.MDK-ARM installation
  • 02.STM32CubeIDE installation
  • 03.Common driver installation
  • 04.Download the program
  • 05.MDK-ARM project usage
  • 06.Program simulation
Robot car PID control course
  • 01.PID basic concept
  • 02.PID example analysis
  • 04.Position PID
  • 05.Incremental PID
  • 06.Cascade PID
  • 07.Balance principle of car
  • 08.Car upright control (PD)
  • 09.Car speed ​​control (PI)
  • 10.Car steering control (PD)
  • 11.Got angle and angular velocity (DMP algorithm)
  • 12.Got angle and angular velocity (Kalman filter algorithm)
  • 13.Got angle and angular velocity(Complementary filter algorithm)
Robot car basic course
  • 01.Car parameter adjustment
  • 02.Ultrasonic obstacle avoidance
  • 03.Ultrasonic follow
  • 04.Bluetooth remote control
  • 05.Load balance
Robot car LQR control
  • Robot car LQR control
Circuit design
  • 01.Overall circuit design
  • 02.STM32 minimum system
  • 03.Serial port circuit design
  • 04.Power circuit design
  • 05.MPU6050 Circuit Design
  • 06.Motor drive circuit design
Mathematical model
  • Balance car mathematical model
4.4-channel motor drive module
Introduction and Debug
  • 1.Introduction to 4-channel motor drive board
  • 2.Control command
  • 3.Motor introduction and usage
  • 4.PC host
IIC communication
  • For Arduino
  • For ESP32
  • For Jetson NANO
  • For MSPM0G3507
  • For MSPM0G3519new
  • For PICO2
  • For RDK X5
  • For RPi5
  • For STM32
USART communication
  • For Arduino
  • For ESP32
  • For Jetson NANO
  • For MSPM0G3507
  • For MSPM0G3519new
  • For PICO2
  • For RDK X5
  • For RPi5
  • For STM32
5.2-channel motor drive module
For MSPM0G3507
  • 1.AT8236 drive tutorials
  • 2.TB6612 drive tutorials
For MSPM0G3519
  • 1.AT8236 drive tutorialsnew
  • 2.TB6612 drive tutorialsnew
Download
  • Model File
  • Annex_ROS_robot_board
  • Annex_STM32_balancing_car
  • Annex_4channel_motor_drive
  • Annex_2channel_motor_drive
Download ZIP

Welcome to 520 Motor repository

For Arduino

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.

The course uses the Arduino UNO board. And use is Arduino 1.8.5 IDE

Hardware wiring:

Motor4-channel motor drive board(Motor)
M2M-
V3V3
AH1A
BH1B
GGND
M1M+
4-channel motor drive boardArduino UNO
SDASDA
SCLSCL
GNDGND
5V5V

When the TYPE-B interface on the UNO motherboard is connected to a computer, you can directly open the serial port to view the printed data without connecting a USB to TTL serial port module.

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

​x
#define UPLOAD_DATA 2  // 1:接收总的编码器数据 2:接收实时的编码器 1: Receive total encoder data 2: Receive real-time encoder data

#define MOTOR_TYPE 1   //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 motor
  • UPLOAD_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
void setup() {
  Serial.begin(115200);
  IIC_Motor_Init();

  #if MOTOR_TYPE == 1
    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);
    
  #elif MOTOR_TYPE == 2
  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);
    Set_motor_deadzone(1600);
    delay(100);
    
  #elif MOTOR_TYPE == 3
  Set_motor_type(3);
    delay(100);
    Set_Pluse_Phase(45);
    delay(100);
    Set_Pluse_line(13);
    delay(100);
    Set_Wheel_dis(68.00);
    delay(100);
    Set_motor_deadzone(1250);
    delay(100);
    
  #elif MOTOR_TYPE == 4
  Set_motor_type(4);
    delay(100);
    Set_Pluse_Phase(48);
    delay(100);
    Set_motor_deadzone(1000);
    delay(100);
    
  #elif MOTOR_TYPE == 5
  Set_motor_type(1);
    delay(100);
    Set_Pluse_Phase(40);
    delay(100);
    Set_Pluse_line(11);
    delay(100);
    Set_Wheel_dis(67.00);
    delay(100);
    Set_motor_deadzone(1900);
    delay(100);
  #endif

This is used to store the parameters of the Yahboom motor. By modifying the MOTOR_TYPE parameter above, one-click configuration can be achieved.

In normally, do not modify the code here when using the Yahboom motor.

If you are using your own motor, or if a certain data needs to be modified according to your needs, you can check the course《1.2 Control command》 to understand the specific meaning of each command.

xxxxxxxxxx
void loop() {
  for (int i = 0; i < 100; i++) {
    #if MOTOR_TYPE == 4
    control_pwm(i*20,i*20,i*20,i*20);
    #else
    control_speed(i * 10, i * 10, i * 10, i * 10);
    delay(100);
    #endif

    #if UPLOAD_DATA == 1
      Read_ALL_Enconder();
      Serial.print("M1:"); Serial.print(Encoder_Now[0]); Serial.print("\t");
      Serial.print("M2:"); Serial.print(Encoder_Now[1]); Serial.print("\t");
      Serial.print("M3:"); Serial.print(Encoder_Now[2]); Serial.print("\t");
      Serial.print("M4:"); Serial.print(Encoder_Now[3]); Serial.println("\t");
    #elif UPLOAD_DATA == 2
      Read_10_Enconder();
      Serial.print("M1:"); Serial.print(Encoder_Offset[0]); Serial.print("\t");
      Serial.print("M2:"); Serial.print(Encoder_Offset[1]); Serial.print("\t");
      Serial.print("M3:"); Serial.print(Encoder_Offset[2]); Serial.print("\t");
      Serial.print("M4:"); Serial.print(Encoder_Offset[3]); Serial.println("\t");
    #endif

    if (i == 100) i = 0;
  }
}

In the loop program, the speed of the four motors will be slowly increased from 0 to 1000. If the motor type is 4, that is, the motor without encoder, the motor's PWM is directly controlled.

At the same time, the data sent by the driver board is read and printed out at the same time.

xxxxxxxxxx
//读取相对时间的编码器的数据 10ms的   Read the data of the encoder of relative time 10ms
void Read_10_Enconder(void)
{
    static int8_t buf[2];
        
    //M1电机编码器的数据    M1 motor encoder data
    i2cRead(Motor_model_ADDR, READ_TEN_M1Enconer_REG, 2, buf);
    Encoder_Offset[0] = buf[0]<<8|buf[1]; 
    
    //M2电机编码器的数据    M2 motor encoder data
    i2cRead(Motor_model_ADDR, READ_TEN_M2Enconer_REG, 2, buf);
    Encoder_Offset[1] = buf[0]<<8|buf[1];
    
    //M3电机编码器的数据    M3 motor encoder data
    i2cRead(Motor_model_ADDR, READ_TEN_M3Enconer_REG, 2, buf);
    Encoder_Offset[2] = buf[0]<<8|buf[1];
    
    //M4电机编码器的数据    M4 motor encoder data
    i2cRead(Motor_model_ADDR, READ_TEN_M4Enconer_REG, 2, buf);
    Encoder_Offset[3] = buf[0]<<8|buf[1];
    
}

//读取电机转动的编码器数据  Read the encoder data of the motor rotation
void Read_ALL_Enconder(void)
{
    static uint8_t buf[2];
    static uint8_t buf2[2];
    
    //M1电机编码器的数据    M1 motor encoder data
    i2cRead(Motor_model_ADDR, READ_ALLHigh_M1_REG, 2, buf);
    i2cRead(Motor_model_ADDR, READ_ALLLOW_M1_REG, 2, buf2);
    
    Encoder_Now[0] = buf[0]<<24|buf[1]<<16|buf2[0]<<8|buf2[1]; 
    
    //M2电机编码器的数据    M2 motor encoder data
    i2cRead(Motor_model_ADDR, READ_ALLHigh_M2_REG, 2, buf);
    i2cRead(Motor_model_ADDR, READ_ALLLOW_M2_REG, 2, buf2);
    Encoder_Now[1] = buf[0]<<24|buf[1]<<16|buf2[0]<<8|buf2[1];
    
    //M3电机编码器的数据    M3 motor encoder data
    i2cRead(Motor_model_ADDR, READ_ALLHigh_M3_REG, 2, buf);
    i2cRead(Motor_model_ADDR, READ_ALLLOW_M3_REG, 2, buf2);
    Encoder_Now[2] = buf[0]<<24|buf[1]<<16|buf2[0]<<8|buf2[1];
    
    
    //M4电机编码器的数据    M4 motor encoder data
    i2cRead(Motor_model_ADDR, READ_ALLHigh_M4_REG, 2, buf);
    i2cRead(Motor_model_ADDR, READ_ALLLOW_M4_REG, 2, buf2);
    Encoder_Now[3] = buf[0]<<24|buf[1]<<16|buf2[0]<<8|buf2[1];
    
}

After getting the data from the driver board, it is shifted, and the shift is required to get the correct data.

1.3 Experimental phenomenon

After the wiring is correct, click the upload button in the Arduino IDE to write the program to the Arduino board. After powering on the Arduino again, you can see that the motor will gradually speed up, then stop, and repeat.

At the same time, you can see the printed motor values changing continuously in the serial port monitor of the ardiono IDE.

PC host
For ESP32

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文