Adafruit Motor Shield v2, two motors, on an Arduino Uno
An Adafruit Motor Shield v2 on an Uno: its PCA9685 at 0x60 drives two TB6612s, and M1 and M2 take turns forward and backward, all over SDA and SCL. 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.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $51. On Mokxi they're free.
What you'll learn
- I2C motor control uses just two pins
- Using the
Adafruit_MotorShieldlibrary RELEASElets a motor coast- Running two motors in opposite directions
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
The Adafruit Motor Shield v2 talks to the Uno over I2C, using only SDA and SCL. Its PCA9685 chip, at address 0x60, drives two TB6612 chips that run up to four motors. Here two motors are used on M1 and M2.
A battery on VIN+ and VIN- powers the motors. The Uno supplies only the logic.
The sketch sets both speeds to 200. Then it runs motor 1 forward and motor 2 backward for 1.5 seconds, releases both for 0.5 seconds, reverses them for 1.5 seconds, and releases again. This repeats.
Parts list
5 parts, plus the jumper wires. They retail for about $51; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Adafruit Motor Shield v2
- 2 × DC motorIn Robot Car Kit.
- 1 × Battery: AA, coin cell, LiPoIn Handheld Console Kit, Laser Sword Kit, Power Bank Kit.
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 |
|---|---|
| Adafruit Motor Shield v2 pin SCL | Arduino Uno R3 pin SCL |
| Adafruit Motor Shield v2 pin SDA | Arduino Uno R3 pin SDA |
| Adafruit Motor Shield v2 pin 5V | Arduino Uno R3 pin 5V |
| Adafruit Motor Shield v2 pin GND | Arduino Uno R3 pin GND |
| Battery: AA, coin cell, LiPo pin V+ | Adafruit Motor Shield v2 pin VIN+ |
| Battery: AA, coin cell, LiPo pin V- | Adafruit Motor Shield v2 pin VIN- |
| DC motor (1) pin 1 | Adafruit Motor Shield v2 pin M1A |
| DC motor (1) pin 2 | Adafruit Motor Shield v2 pin M1B |
| DC motor (2) pin 1 | Adafruit Motor Shield v2 pin M2A |
| DC motor (2) pin 2 | Adafruit Motor Shield v2 pin M2B |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Two DC motors on the Adafruit Motor Shield v2.
//
// The shield's PCA9685 sits at I2C address 0x60 and drives all four bridges
// of its two TB6612s, so the Uno spends only SDA and SCL on four motors.
// Motor power goes on VIN+ and VIN-.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield shield;
Adafruit_DCMotor *m1 = shield.getMotor(1);
Adafruit_DCMotor *m2 = shield.getMotor(2);
void setup() {
Serial.begin(9600);
if (!shield.begin()) Serial.println("No motor shield at 0x60");
m1->setSpeed(200);
m2->setSpeed(200);
}
void loop() {
m1->run(FORWARD);
m2->run(BACKWARD);
delay(1500);
m1->run(RELEASE);
m2->run(RELEASE);
delay(500);
m1->run(BACKWARD);
m2->run(FORWARD);
delay(1500);
m1->run(RELEASE);
m2->run(RELEASE);
delay(500);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial, then calls shield.begin(). If no shield answers, it prints "No motor shield at 0x60". It sets both motors to speed 200 using setSpeed.
void setup() {
Serial.begin(9600);
if (!shield.begin()) Serial.println("No motor shield at 0x60");
m1->setSpeed(200);
m2->setSpeed(200);
}loop sketch.ino
Runs a four step cycle: motor 1 forward and motor 2 backward for 1500 ms, release both for 500 ms, then reverse both for 1500 ms and release again.
void loop() {
m1->run(FORWARD);
m2->run(BACKWARD);
delay(1500);
m1->run(RELEASE);
m2->run(RELEASE);
delay(500);
m1->run(BACKWARD);
m2->run(FORWARD);
delay(1500);
m1->run(RELEASE);
m2->run(RELEASE);
delay(500);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- "No motor shield at 0x60" This project
Check SDA and SCL, 5 V and ground between the Uno and the shield. - Motors do not run This project
Check the battery is on VIN+ and VIN-. The Uno does not power the motors. - The motors do not run. The shield needs external motor power on its terminal, and the USB power alone is not enough. Adafruit Motor Shield v2
Connect a 5 to 12 V motor supply to the power block and join its ground. Check the motor numbers M1 to M4 in code, and call begin() first. - The shield is not found, or conflicts with other I2C parts. The default address is 0x60 and the library needs begin(). Adafruit Motor Shield v2
Scan I2C for 0x60. Move other devices to a different address if they clash, and set the address jumpers if you stack shields. Do not use the pins the shield needs. - 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.