Example Code for Arduino-Motor Control
Control motor positive inversion and PWM speed control. This project demonstrates how to use the HR8833 motor driver to control the direction and speed of two DC motors using Arduino.
Hardware Preparation
- UNO x1
- Motor driver-HR8833 x1
- Several DuPont wire
Software Preparation
- Arduino IDE version 1.6.8 click to download Arduino IDE
Wiring Diagram
IA1<——>10
IA2<——>12
IB1<——>11
IB2<——>13
Other Preparation Work
The IA1 and IA2 input pins control the state of the MA1 and MA2 outputs; similarly, the IB1 and IB2 input pins control the state of the MB1 and MB2 outputs. Below table shows the logic.
| Ix1 | Ix2 | Mx1 | Mx2 | Function |
|---|---|---|---|---|
| 0 | 0 | Z | Z | Coast/fast decay |
| 0 | 1 | L | H | Reverse |
| 1 | 0 | H | L | Forward |
| 1 | 1 | L | L | Brake/slow decay |
The inputs can also be used for PWM control of the motor speed. When controlling a winding with PWM, when the drive current is interrupted, the inductive nature of the motor requires that the current must continue to flow.This is called recirculation current. To handle this recirculation current, the H-bridge can operate in two different states, fast decay or slow decay. In fast decay mode, the H-bridge is disabled and recirculation current flows through the body diodes; in slow decay, the motor winding is shorted.
To PWM using fast decay, the PWM signal is applied to one Ix pin while the other is held low; to use slow decay, one Ix pin is held high.
| Ix1 | Ix2 | Function | Description |
|---|---|---|---|
| PWM | 0 | Forward PWM,fast decay | Speed = High duty-cycle |
| 1 | PWM | Forward PWM,slow decay | Speed = Low duty-cycle |
| 0 | PWM | Reverse PWM,fast decay | Speed = High duty-cycle |
| PWM | 1 | Reverse PWM,slow decay | Speed = Low duty-cycle |
Sample Code
/*
* @file Motor driver HR8833-Test.ino
* @brief HR8833-Test.ino Motor control program
*
* control motor positive inversion
*
* @author [email protected]
* @version V1.0
* @date 2016-4-13
*/
const int IA1=10;
const int IA2=12;
const int IB1=11;
const int IB2=13;
void setup() {
pinMode(IA1, OUTPUT);
pinMode(IA2, OUTPUT);
pinMode(IB1, OUTPUT);
pinMode(IB2, OUTPUT);
}
void loop() {
MA1_Forward(200);//Motor MA1 forward; PWM speed control
MB1_Forward(200);//Motor MB1 forward; PWM speed control
delay(1000);
MA2_Backward(200);//Motor MA2 backward; PWM speed control
MB2_Backward(200);//Motor MB2 backward; PWM speed control
delay(1000);
}
void MA1_Forward(int Speed1) //fast decay; Speed = High duty-cycle
{
analogWrite(IA1,Speed1);
digitalWrite(IA2,LOW);
}
void MA2_Backward(int Speed1) //slow decay; Speed = Low duty-cycle
{
int Speed2=255-Speed1;
analogWrite(IA1,Speed2);
digitalWrite(IA2,HIGH);
}
void MB1_Forward(int Speed1)
{
analogWrite(IB1,Speed1);
digitalWrite(IB2,LOW);
}
void MB2_Backward(int Speed1)
{
int Speed2=255-Speed1;
analogWrite(IB1,Speed2);
digitalWrite(IB2,HIGH);
}
Result
Motor Reversing
Was this article helpful?
