yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Motor with Encoder📖
1.Introduction
  • 1.Introduction
2.2-channel motor drive module tutorials
For Arduino
  • 1.AT8236 drive tutorials
For STM32
  • 1.AT8236 drive tutorials
For MSPM0G3507
  • 1.AT8236 drive tutorials
  • 2.TB6612 drive tutorials
For MSPM0G3519
  • 1.AT8236 drive tutorialsnew
  • 2.TB6612 drive tutorialsnew
3.4-channel motor drive module tutorials
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
Download
  • Annex_2-channel motor drive module tutorials
  • Annex_4-channel motor drive module tutorials
Download ZIP

Welcome to Motor with Encoder repository

2.Control command

Control command

Control commandThe 4 motor interfaces on the module correspond to motors on robot car, as shown belowSerial port configuration1.Configure motor type2.Configuring motor deadband3.Configuring motor phase lines4.Configure motor reduction ratio5.Configure the wheel diameter (Optional)6.Configure PID parameters for motor control7.Reset all variables to defaults value8.Control speed command9.Direct control PWM instruction10.Report encoder data (This command is only valid for motors with encoder)11.Query flash variables12.Check battery levelIIC protocol control

The 4 motor interfaces on the module correspond to motors on robot car, as shown below

M1 -> Upper left motor (left front wheel of the car) M2 -> Lower left motor (left rear wheel of the car) M3 -> Upper right motor (right front wheel of the car) M4 -> Lower right motor (right rear wheel of the car)

Serial port configuration

Baud rate 115200, no parity, no hardware flow control, 1 stop bit

 

1.Configure motor type

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$mtype:x#Motor model$mtype:1#The motor model is 520 motor520 motorY
Note     
  1. Motor type selection. If the motor encoder A is connected to the A port of the board, then B is connected to B. You need to select the 310 motor model. Otherwise, you need to select the 520 motor or TT motor.

  2. The command can be sent in all uppercase or lowercase letters.

  3. If the above command is successful, it will return the message command+OK. If no message is returned, check the serial port connection.

  4. x: is the type of motor. The motor types represented by different values are as follows: 1: 520 motor 2: 310 motor 3: TT motor (with encoder) 4: TT motor (without encoder)

Note: If you are using a motor without an encoder, you can select type 4, that is, the command is: $mtype:4# If you are using a motor with an encoder, you can select one of 1, 2, and 3.

2.Configuring motor deadband

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$deadzone:xxxx#Configure the motor pwm pulse dead zone$deadzone:1650#When controlling PWM, the dead zone value will be added by default so that the motor will not have an oscillation area.1600Y
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. If the above command is successful, it will return the message of command+OK. If no message is returned, check the serial port connection

  3. xxxx: is the value of the dead zone, which needs to be measured. By changing this value, the minimum vibration of the motor can be eliminated

  4. The range of the dead zone value (0-3600)

3.Configuring motor phase lines

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$mline:xx#Configure the motor phase lines$mline:13#Configure the phase of the motor Hall encoder to 13 lines11Y
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. The above command will return command+OK information if it is successful. If no information is returned, check the serial port connection

  3. xx: This is the phase of the Hall encoder for one turn. This value needs to be obtained by checking the merchant's motor parameter table

  4. For motors with encoders: This value plays a main role in controlling speed. This value needs to be correct

  5. Motors without encoders: This value configuration can be ignored

4.Configure motor reduction ratio

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$mphase:xx#Configure the motor reduction ratio$mphase:40#Configure the motor reduction ratio to 4030Y
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. The above command will return command+OK information if it is successful. If no information is returned, check the serial port connection

  3. xx: This is the motor reduction ratio parameter. This value needs to be obtained by checking the merchant's motor parameter table

  4. For motors with encoders: This value plays a main role in controlling speed. This value needs to be correct

  5. Motors without encoders: This value configuration can be ignored

5.Configure the wheel diameter (Optional)

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$wdiameter:xx#Configure the wheel diameter$wdiameter:50#The diameter of the wheel is 50mm67 mmY
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. The above command will return command+OK information if it is successful. If no information is returned, check the serial port connection

  3. xx: This is the diameter of the wheel. This value can be measured or obtained using the merchant's information

  4. For motors with encoders: This value plays a main role in controlling speed. This value needs to be correct in millimeters (mm); if this value is incorrect, it will only affect the speed data inaccurately, and will not affect the encoder data

  5. Motors without encoders: This value configuration can be ignored

6.Configure PID parameters for motor control

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$MPID:x.xx,x.xx,x.xx#Configure PID parameters$MPID:1.5,0.03,0.1#The configuration control speed is P: 1.5, I: 0.03, D: 0.1P:0.8 I:0.06 D:0.5Y
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. The above command will return command+OK if it is successful. If no information is returned, check the serial port connection

  3. x.xx, x.xx, x.xx: These are the parameters for controlling motor p, i, d respectively. Every time the value is changed, the chip will restart and stop the moving motor. This is a normal situation

  4. For motors with encoders: the pid parameter is valid, and this value needs to be correct. Generally, there is no need to modify the pid, and the default value can be used

  5. Motors without encoders: the pid parameter is invalid, and this value configuration can be ignored

7.Reset all variables to defaults value

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$flash_reset#Restore factory defaults value----
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. If the above command is successful, it will return the message command+OK. If no message is returned, check the serial port connection

  3. Execute this command and the module will restart once

8.Control speed command

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$spd:0,0,0,0#Control the speed of 4 motors$spd:100,-100,0,50#Control the speed of 4 motors M1:100 M2:-100 M3:0 M4:50-N
Note     
  1. The command can be sent in all uppercase or all lowercase letters

  2. If the above command is successful, the motor will move, and the serial port will not return anything

  3. This command is only valid for encoder type motors.

  4. The speed range is (-1000~1000) and it is invalid if it exceeds the range

  5. 0,0,0,0: represents M1, M2, M3, M4 on the board screen

  6. When the spd command parameter is 0, PID still works. At this time, the hand cannot turn the wheel. To turn the wheel, use the pwm command to set it to 0.

9.Direct control PWM instruction

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$pwm:0,0,0,0#Control 4 motors with PWM output$pwm:0,-520,300,800#Control the PWM output of 4 motors M1:0 M2:-520 M3:300 M4:800-N
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. If the above command is successful, the motor will move, and nothing will be returned from the serial port

  3. The speed range is (-3600~3600) and it is invalid if it exceeds the range

  4. For motor control without encoder, you can control it through this command

  5. 0,0,0,0: represents M1,M2,M3,M4 on the board screen

10.Report encoder data (This command is only valid for motors with encoder)

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$upload:0,0,0#Receive encoder data$upload:1,0,0#Receive the total encoder data of wheel rotation
1: open, 0: do not receive
-N
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. $upload:0,0,0#: The first 0 represents: the total encoder data of the wheel rotation The second 0 represents: the real-time encoder data of the wheel rotation (10ms) The third 0 represents: the speed of the wheel

  3. The corresponding information can be received at the same time

  • The total encoder data of the wheel rotation returns the information: "$MAll:M1,M2,M3,M4#"

  • The real-time encoder data of the wheel rotation returns the information: "$MTEP:M1,M2,M3,M4#"

  • The speed of the wheel returns the information: "$MSPD:M1,M2,M3,M4#"

11.Query flash variables

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$read_flash#Query flash variables---N
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. If the above command is successful, it will return the message command+OK. If no message is returned, check the serial port connection

12.Check battery level

CommandExplanationExampleRemarkFirmware DefaultsPower off save
$read_vol#Check battery level---N
Note     
  1. The command can be sent in all uppercase or all lowercase

  2. If the above command is successful, it will return the information of battery power ($Battery:7.40V#). If no information is returned, please check the serial port connection

IIC protocol control

4-channel motor driver board IIC device address: 0x26

Register addressR/WTypeRangeExplanationExample
0x01wuint8_t1: 520 motor
2: 310 motor
3: TT motor (with encoder)
4: TT motor (without encoder)
Write motor typeDevice address + register address + motor type
0x02wuint16_t0-3600Configuring motor deadbandDevice address + register address + motor dead zone value
0x03wuint16_t0-65535Configure the number of motor magnetic ring linesDevice address + register address + number of motor magnetic ring lines
0x04wuint16_t0-65535Configure motor reduction ratioDevice address + register address + motor reduction ratio
0x05wfloat-Enter the wheel diameter, unit: mmDevice address + register address + wheel diameter (need to convert float to bytes - little endian first)
0x06wint16_t-1000~1000Speed control, this register is only effective for motors with encoders, unit: mmDevice address + register address + speed (transmitting data of 4 motors each time)
Big-endian modeEach speed occupies 2 bits
For uint8_t
eg: m1 motor speed 200, m2 motor speed -200, m3 motor speed 0, m4 motor speed 500
That is: [0x00 0xC8 0xFF 0x38 0x00 0x00 0x01 0xf4]
0x07wint16_t-3600~3600PWM control, this control does not require encoder data and can directly control the motor rotationDevice address + register address + speed (transmitting data of 4 motors each time)
Big-endian modeEach speed occupies 2 bits
For uint8_t
eg: m1 motor pwm200, m2 motor pwm is -200, m3 motor pwm is 0, m4 motor pwm is 500
That is: [0x00 0xC8 0xFF 0x38 0x00 0x00 0x01 0xf4]
0x08Ruint16_t-Reading battery levelA correct data: data = (buf[0]<<8|buf[1])/10.0
0x10Rint16_t-Read M1 encoder real-time pulse data - 10msA correct data: data = buf[0]<<8|buf[1]
0x11Rint16_t-Read M2 encoder real-time pulse data - 10msA correct data: data = buf[0]<<8|buf[1]
0x12Rint16_t-Read M3 encoder real-time pulse data - 10msA correct data: data = buf[0]<<8|buf[1]
0x13Rint16_t-Read M4 encoder real-time pulse data - 10msA correct data: data = buf[0]<<8|buf[1]
0x20Rint16_t-Read the total pulse data of the M1 motor encoder (High bit)-
0x21Rint16_t-Read the total pulse data of the M1 motor encoder (Low bit)The acquired data needs to be shifted to get the correct data.
High bit represents: buf[0] buf[1]
Low bit represents: bf[0] bf[1] (data = buf[0]<<24|buf[1]<<16|bf[0]<<8|bf[1])
0x22Rint16_t-Read the total pulse data of the M2 motor encoder (High bit)-
0x23Rint16_t-Read the total pulse data of the M2 motor encoder (Low bit)Same calculation method as M1
0x24Rint16_t-Read the total pulse data of the M3 motor encoder (High bit)-
0x25Rint16_t-Read the total pulse data of the M3 motor encoder (Low bit)Same calculation method as M1
0x26Rint16_t-Read the total pulse data of the M4 motor encoder (High bit)-
0x27Rint16_t-Read the total pulse data of the M4 motor encoder (Low bit)Same calculation method as M1
Introduction to 4-channel motor drive board
Motor introduction and usage

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/中文