Lesson 10 of 13
Steer with two motors
Objective: Drive two motors through a TB6612FNG H-bridge: choose each motor's direction, brake or coast, and steer a rover by giving its wheels different speeds.
While the sketch runs, the rover moves on the mat, a zoomed-in view shows both PWM pins, and a chart draws each wheel's speed over the last 4 seconds.
Stage 1 of 7 · What am I building?
Explore
A rover with two wheels steers by their speeds alone. The TB6612FNG drives both motors either way, brakes them, or lets them coast; the board only sends it signals. Select each part on the circuit, or below.
Pick a part. 0/8 inspected.
| IN1 | IN2 | PWM | motor |
|---|---|---|---|
| HIGH | LOW | HIGH | CW (forward here) |
| LOW | HIGH | HIGH | CCW (reverse here) |
| HIGH | HIGH | any | short brake |
| HIGH or LOW | the other | LOW | short brake |
| LOW | LOW | HIGH | stop: outputs off (coasts) |
From the TB6612FNG datasheet. STBY LOW puts both channels in standby, outputs off.
⚠ The motor battery goes to VM only, never to a pin or the board's 5V. Join every GND, lift the wheels for the first test, and keep the power-off switch in reach.
Before you start (optional refreshers)
PWM and duty cycle
analogWrite() switches a ~ pin on and off quickly. The ON fraction (0–255 → 0–100%) sets average power. It is not a true analog voltage.
Actuators and external power
Motors and servos need more current than a pin gives. Use a driver (transistor), a separate supply, a flyback diode for motors, and a shared GND.
A transistor as a switch
A small current into an NPN transistor's base lets a much larger current flow from collector to emitter. A pin drives the base through a resistor (the signal path); the load's own supply flows through the collector (the power path).
New ideas in this lesson
H-bridges, direction and steering
An H-bridge has four switches around a motor, so the driver can send current through it either way: two direction pins choose forward, reverse, brake or off, and a PWM pin sets the speed. Two motors at different speeds steer a rover: it turns towards the slower wheel.
Build it for real · Arduino Uno R3
You need
- Arduino Uno R3 (or compatible) and its USB cable
- Breadboard and jumper wires
- TB6612FNG dual motor driver (breakout board)
- Two-wheel rover chassis with 2 small DC gear motors
- Motor battery: 4 × AA (6 V) with a power-off switch
Wiring
- Driver VCC → 5V; driver GND → GND.
- Motor battery + (through its power-off switch) → VM; battery − → driver GND, so every GND is joined.
- STBY → D3.
- Left motor: PWMA → D5, AIN1 → D4, AIN2 → D7; its leads → AO1 and AO2.
- Right motor: PWMB → D6, BIN1 → D12, BIN2 → D11; its leads → BO1 and BO2.
- Lift the wheels off the table, upload, and check both turn forward. If one turns backward, swap that motor's two leads.
Before you power up: Never power the motors from a pin or the board's 5V pin: the battery goes to VM only (4.5–13.5 V for this driver). Choose motors whose stall current is within the driver's 1.0 A per channel. Join every GND, and test with the wheels lifted and the power-off switch in reach.
Tip: The driver's inputs, STBY included, have pull-downs inside: a loose STBY wire reads LOW and the driver stays in standby.
The lab models ideal parts. Real circuits add tolerances, noise and loose wires, so check your wiring against this list before you plug in.
// Steer with two motors through a TB6612FNG dual H-bridge
// STBY D3~; left motor (A): PWMA D5~, AIN1 D4, AIN2 D7
// right motor (B): PWMB D6~, BIN1 D12, BIN2 D11~
// VM from the motor battery, VCC from 5V, all GNDs joined
const int stbyPin = 3; // HIGH = driver on, LOW = standby
const int pwmAPin = 5; // left speed: a ~ pin
const int ain1Pin = 4; // left direction
const int ain2Pin = 7; // left direction
const int pwmBPin = 6; // right speed: a ~ pin
const int bin1Pin = 12; // right direction
const int bin2Pin = 11; // right direction
void leftMotor(int in1, int in2, int speed) {
digitalWrite(ain1Pin, in1);
digitalWrite(ain2Pin, in2);
analogWrite(pwmAPin, speed); // 0 to 255
}
void rightMotor(int in1, int in2, int speed) {
digitalWrite(bin1Pin, in1);
digitalWrite(bin2Pin, in2);
analogWrite(pwmBPin, speed); // 0 to 255
}
void setup() {
pinMode(stbyPin, OUTPUT);
pinMode(ain1Pin, OUTPUT);
pinMode(ain2Pin, OUTPUT);
pinMode(bin1Pin, OUTPUT);
pinMode(bin2Pin, OUTPUT);
digitalWrite(stbyPin, HIGH); // wake the driver up
}
void loop() {
// both forward (CW)
leftMotor(HIGH, LOW, 200);
rightMotor(HIGH, LOW, 200);
}Press ▶ Run.