MX1508 two-motor drive on an Arduino Uno
An MX1508 module on pins 5, 6, 9 and 10 drives two gear motors off four AA cells: forward, a turn, back and a coast. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- Driving two DC motors with an H-bridge module
- Direction and speed from a pair of inputs
- PWM speed control with
analogWrite - Turning by running the motors in opposite directions
- Writing a helper function to avoid repeating code
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
An MX1508 module is a dual motor driver. Each motor gets a pair of inputs: motor A on pins 5 and 6, motor B on pins 9 and 10. Four AA cells power the module and the motors, and their minus is joined to the Uno's GND.
To run a motor, the sketch sends PWM to one input of the pair and holds the other at zero. Swap which input gets the PWM to reverse. Both low lets the motor coast.
The sketch repeats a short routine: forward, turn left, forward, back, then coast. It prints each step to serial.
Parts list
5 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| MX1508 dual motor driver pin - | Arduino Uno R3 pin GND |
| Battery: AA, coin cell, LiPo pin V- | Arduino Uno R3 pin GND |
| MX1508 dual motor driver pin IN4 | Arduino Uno R3 pin 10 |
| MX1508 dual motor driver pin IN3 | Arduino Uno R3 pin 9 |
| MX1508 dual motor driver pin IN2 | Arduino Uno R3 pin 6 |
| MX1508 dual motor driver pin IN1 | Arduino Uno R3 pin 5 |
| Battery: AA, coin cell, LiPo pin V+ | MX1508 dual motor driver pin + |
| DC motor (1) pin 1 | MX1508 dual motor driver pin MA1 |
| DC motor (1) pin 2 | MX1508 dual motor driver pin MA2 |
| DC motor (2) pin 1 | MX1508 dual motor driver pin MB1 |
| DC motor (2) pin 2 | MX1508 dual motor driver pin MB2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Two gear motors on an MX1508 module, from an Uno: a robot car's drive.
//
// MX1508 IN1 -> pin 5 IN2 -> pin 6 (motor A)
// MX1508 IN3 -> pin 9 IN4 -> pin 10 (motor B)
// MX1508 + -> four AA cells, - -> the cells' - and the Uno's GND
//
// Each motor has a pair of inputs: PWM on one with the other low runs it,
// both low lets it coast, both high brakes. The module runs from 2 to 9.6 V,
// chip and motors on the same supply.
const int A1_PIN = 5, A2_PIN = 6, B1_PIN = 9, B2_PIN = 10;
void motor(int p1, int p2, int speed) {
if (speed >= 0) {
analogWrite(p2, 0);
analogWrite(p1, speed);
} else {
analogWrite(p1, 0);
analogWrite(p2, -speed);
}
}
void go(const char *what, int left, int right, int ms) {
Serial.println(what);
motor(A1_PIN, A2_PIN, left);
motor(B1_PIN, B2_PIN, right);
delay(ms);
}
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: MX1508 drives two motors");
}
void loop() {
go("forward", 200, 200, 1500);
go("turn left", -150, 150, 800);
go("forward", 200, 200, 1500);
go("back", -200, -200, 1000);
go("coast", 0, 0, 1000);
}
Code walkthrough
The key functions of the sketch, and what each one does.
motor sketch.ino
It runs one motor from a pair of pins. A positive speed puts PWM on the first pin and zero on the second. A negative speed does the reverse, using the speed without its minus sign. A speed of 0 sets both pins to 0, so the motor coasts.
void motor(int p1, int p2, int speed) {
if (speed >= 0) {
analogWrite(p2, 0);
analogWrite(p1, speed);
} else {
analogWrite(p1, 0);
analogWrite(p2, -speed);
}
}go sketch.ino
It prints a label, sets the left and right motors with motor, and waits for the given number of milliseconds. One call describes a whole move, such as "forward" at 200 for 1500 ms.
void go(const char *what, int left, int right, int ms) {
Serial.println(what);
motor(A1_PIN, A2_PIN, left);
motor(B1_PIN, B2_PIN, right);
delay(ms);
}loop sketch.ino
It calls go five times in a row: forward, turn left, forward, back, coast. Turn left runs the left side at -150 and the right at 150, so the motors spin in opposite directions. Then the routine repeats.
void loop() {
go("forward", 200, 200, 1500);
go("turn left", -150, 150, 800);
go("forward", 200, 200, 1500);
go("back", -200, -200, 1000);
go("coast", 0, 0, 1000);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- A motor runs the wrong way This project
Swap that motor's two leads, or swap which pin pair the PWM lands on. - The motors do nothing This project
Check that the battery's minus, the module's minus and the Uno's GND are all joined, and the battery plus goes to the module's plus. - The motor does not turn or gets weak. MX1508 dual motor driver
Supply 2 V to 9.6 V on VCC and share ground with the controller. Use PWM on one input and low on the other per direction. Mind the current per channel. - The motor does not stop as expected. MX1508 dual motor driver
Both inputs low coasts, both high brakes on some drivers. Check what your board does. Add a capacitor across the supply for motor noise. - The motor does not spin, or the pin gets hot or the board resets when it is wired straight to a GPIO pin. DC motor
A pin gives about 20 mA and a motor needs far more. Drive it through a transistor or motor driver, with its own supply and a shared ground. - The transistor or pin is damaged by spikes when the motor stops. DC motor
Put a flyback diode across the motor, cathode to the positive side. A motor is a coil and kicks back voltage when switched off. Add a 0.1 uF capacitor across the motor terminals to cut noise. - The battery is wired backwards, or the voltage is too low or too high for the circuit. A fresh AA is about 1.5 V, a LiPo is 3.7 V nominal and 4.2 V full. Battery: AA
Match the plus and minus marks. Check the voltage your parts need. Two AAs make 3 V, four make 6 V. Never feed a LiPo to a 3.3 V chip directly at 4.2 V. - The circuit resets or browns out when a motor or servo starts. A battery has internal resistance, so the voltage sags under load. Battery: AA
Use a fresh pack with enough current rating. Add a 100 uF or larger capacitor near the load. Power motors from their own pack and join the grounds.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.