给小车换驱动板,TB6612之外DRV8833是另一块值得认真看的芯片:单芯片双路H桥,持续电流1.5A、峰值2A,2.7到10.8V的电机电压范围把常见的N20、TT小电机全覆盖。更舒服的是它的控制逻辑——每路只要两个引脚,正反转和PWM调速全在两根线上完成。这篇拿UNO把它跑通。

模块长什么样
常见的紫板/黑板模块上,中间那颗带散热焊盘的芯片就是DRV8833,两排各四个引脚分别是两路输出的OUT1/OUT2和输入的AIN1/AIN2、BIN1/BIN2,另有一组电源脚。

控制逻辑:两根线定方向和速度
DRV8833每个通道用IN1、IN2两个输入:IN1高IN2低正转,反过来反转,两个都低刹车,都高滑行。调速不用额外的使能脚,直接往其中一个输入脚打PWM、另一个拉低就行——正转时PWM走IN1,反转时PWM走IN2,这正是下面代码里set_motor_pwm()干的事。
供电分两组:VMOT接电机电源(2.7到10.8V,注意和逻辑电压无关),VCC接3.3到5V逻辑电。common ground务必共地。模块还有nSLEEP脚,拉低芯片进休眠,接高才工作,多数模块默认已拉高,有些板留了跳线。
接线
按官方推荐把两路输入接到带PWM的引脚:A路IN1接D10、IN2接D9,B路IN1接D6、IN2接D5(UNO上3、5、6、9、10、11是PWM脚)。

电机电源正极进VMOT、负极进GND,和Arduino共地。驱动板务必和电机电源一起供电,别拿UNO的5V给电机凑合,电流一大就复位。
完整代码
下面这份示例程序把正反转、调速、差速转向全部走了一遍,可以直接烧:
// Define the control inputs
#define MOT_A1_PIN 10
#define MOT_A2_PIN 9
#define MOT_B1_PIN 6
#define MOT_B2_PIN 5
void setup(void)
{
// Set all the motor control inputs to OUTPUT
pinMode(MOT_A1_PIN, OUTPUT);
pinMode(MOT_A2_PIN, OUTPUT);
pinMode(MOT_B1_PIN, OUTPUT);
pinMode(MOT_B2_PIN, OUTPUT);
// Turn off motors - Initial state
digitalWrite(MOT_A1_PIN, LOW);
digitalWrite(MOT_A2_PIN, LOW);
digitalWrite(MOT_B1_PIN, LOW);
digitalWrite(MOT_B2_PIN, LOW);
// Initialize the serial UART at 9600 baud
Serial.begin(9600);
}
void loop(void)
{
// Generate a fixed motion sequence to demonstrate the motor modes.
// Ramp speed up.
for (int i = 0; i < 11; i++) {
spin_and_wait(25*i, 25*i, 500);
}
// Full speed forward.
spin_and_wait(255,255,2000);
// Ramp speed into full reverse.
for (int i = 0; i < 21 ; i++) {
spin_and_wait(255 - 25*i, 255 - 25*i, 500);
}
// Full speed reverse.
spin_and_wait(-255,-255,2000);
// Stop.
spin_and_wait(0,0,2000);
// Full speed, forward, turn, reverse, and turn for a two-wheeled base.
spin_and_wait(255, 255, 2000);
spin_and_wait(0, 0, 1000);
spin_and_wait(-255, 255, 2000);
spin_and_wait(0, 0, 1000);
spin_and_wait(-255, -255, 2000);
spin_and_wait(0, 0, 1000);
spin_and_wait(255, -255, 2000);
spin_and_wait(0, 0, 1000);
}
/// Set the current on a motor channel using PWM and directional logic.
///
/// \param pwm PWM duty cycle ranging from -255 full reverse to 255 full forward
/// \param IN1_PIN pin number xIN1 for the given channel
/// \param IN2_PIN pin number xIN2 for the given channel
void set_motor_pwm(int pwm, int IN1_PIN, int IN2_PIN)
{
if (pwm < 0) { // reverse speeds
analogWrite(IN1_PIN, -pwm);
digitalWrite(IN2_PIN, LOW);
} else { // stop or forward
digitalWrite(IN1_PIN, LOW);
analogWrite(IN2_PIN, pwm);
}
}
/// Set the current on both motors.
///
/// \param pwm_A motor A PWM, -255 to 255
/// \param pwm_B motor B PWM, -255 to 255
void set_motor_currents(int pwm_A, int pwm_B)
{
set_motor_pwm(pwm_A, MOT_A1_PIN, MOT_A2_PIN);
set_motor_pwm(pwm_B, MOT_B1_PIN, MOT_B2_PIN);
// Print a status message to the console.
Serial.print("Set motor A PWM = ");
Serial.print(pwm_A);
Serial.print(" motor B PWM = ");
Serial.println(pwm_B);
}
/// Simple primitive for the motion sequence to set a speed and wait for an interval.
///
/// \param pwm_A motor A PWM, -255 to 255
/// \param pwm_B motor B PWM, -255 to 255
/// \param duration delay in milliseconds
void spin_and_wait(int pwm_A, int pwm_B, int duration)
{
set_motor_currents(pwm_A, pwm_B);
delay(duration);
}核心是set_motor_pwm():传入-255到255的pwm,负数就把PWM打在IN2上实现反转,正数打在IN1上正转。loop里先从0匀加到满速再倒回全速反转,最后用正负号组合演示两轮底盘的直行、原地转和差速弯。
跑起来看什么
烧录完打开串口监视器(9600),能看到每一步的PWM值。电机慢速爬坡时如果有叫声,那是PWM频率附近的本体共振,属正常;如果电机只抖不转,先查VMOT电压够不够,再看nSLEEP是不是被拉低了。空载全速时芯片温热没问题,长时间堵转就会触发过流保护,输出自动关断——这也是选1.5A持续电流的芯片时给电机留余量的原因。
相比L298N那代压降大、发热狠的老方案,DRV8833这类MOSFET桥效率高得多,体积也小,做小型机器人和两轮小车的首选之一。把这两根线玩明白,差速底盘的底层就通了。











VMOT别接UNO 5V,血泪教训,电机一转板子就重启
nSLEEP被拉低电机不动,查了半小时跳线,看到这段想起当时
两轮差速那段直接抄进小车了,原地转好用
set_motor_pwm这个封装舒服,负数反转正数正转,比L298N那套IN1 IN2 IN3 IN4 mental gymnastics清爽多了