Robotics
How a two-wheeled robot steers and sets its speed
A differential drive robot has two driven wheels, one each side, and no steering. It turns by driving them at different speeds: equal speeds go straight, a slower wheel on one side curves the robot toward that side, and one wheel stopped makes it pivot round that wheel. Each wheel's speed comes from a PWM signal through a motor driver.
Two wheels, no steering wheel
A typical small rover has a driven wheel on each side and a caster at the back that just rolls along. There's nothing to point the wheels left or right. It steers the way a tank or a wheelchair does: push one side harder and you curve.
Live experiment
Two wheel speeds, one rover
Pick a speed for each wheel, 0 to 255 by analogWrite, and drive the rover for two and a half seconds. Equal speeds go straight, unequal ones curve toward the slower side, and one wheel stopped pivots round it.
“Two wheels” is lesson 1 of 6 in Rover Ridge, Robotics World 1. Free with an account. It’s open as soon as you sign up.
A speed per wheel
A board's pin can only supply about 20 mA, far too little for a motor. So each motor gets a motor driver, a chip that takes the board's small signal and feeds the motor the big current it needs. Many motor driver chips contain an H-bridge, which also lets them reverse the motor.
The signal is PWM from analogWrite, 0 to 255: 0 is stopped and 255 is full speed. The motor responds to the average, so 128 runs it at about half speed.
analogWrite(9, 255); // left wheel: full speed
analogWrite(10, 128); // right wheel: about half
The four moves
- Both at 255: straight
- Both at 0: stopped
- Left 255, right 0: the right wheel is planted and the left drives round it, so the rover pivots right
- Left 255, right 128: a curve to the right, tighter the bigger the difference
The curve's size follows from the wheel speeds and the track, the distance between the wheels. The centre of the rover follows a circle of radius:
where L and R are the two wheel speeds. For a rover with a 12 cm track, left 255 and right 64 drive a circle about 20 cm across; left 255 and right 192, one about 85 cm across. Equal speeds make the bottom of the fraction zero: a circle too big to close, a straight line.
Moves in time
A move is a pair of speeds held for a while. Straight for a second, then pivot for three quarters of a second, about a quarter turn on the lesson's rover. Four of those in a row drive a square:
void loop() {
analogWrite(9, 255); // straight
analogWrite(10, 255);
delay(1000);
analogWrite(10, 0); // pivot right
delay(750);
}
Real motors never match perfectly, so two "equal" speeds drift slightly. Robots that must drive dead straight measure each wheel's turning and correct it with feedback.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Two wheels” is lesson 1 of 6 in Rover Ridge, Robotics World 1. Free with an account. It’s open as soon as you sign up.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Pivots right, around the stopped right wheel. The right wheel is planted and the left one drives round it, so the rover swings its nose to the right. Stop the left instead and it swings left.
- Why not “Reverses”? Reversing needs the wheels to run backwards. Here the left wheel drives forwards, so the rover moves forwards round the right wheel.
- Why not “Pivots left”? A rover turns toward its slower side, and here that's the stopped right wheel. To pivot left, run the right wheel and stop the left.
- Why not “Goes straight”? Going straight needs both wheels at the same speed. With only the left one driving, the rover swings round the right wheel.
Show the answer
Answer: Curves gently to the right. The faster left wheel swings the nose toward the slower right side. A big speed difference turns sharply; a small one curves gently.
- Why not “Curves left”? A rover curves toward its slower wheel. The right wheel is slower here, so it curves right.
- Why not “Spins on the spot”? Spinning on the spot needs the wheels to turn in opposite directions. Both run forwards here, so the rover keeps moving forwards as it turns.
- Why not “Goes straight”? Straight needs equal speeds. The left wheel runs about twice as fast as the right, so the path bends.
Show the answer
Answer: One wheel runs, the other stops. The stopped wheel is the pivot; the running one swings the rover round it.
- Why not “Both run at 255”? Equal speeds on both sides drive the rover straight. Turning needs a difference between the wheels.
- Why not “Both run at half speed”? Equal speeds go straight, just more slowly. Only a difference in speed turns it, and the biggest forward-only difference is one wheel stopped.
- Why not “Both wheels stop”? With both wheels stopped the rover doesn't move at all, so it doesn't turn either.
“Two wheels” is lesson 1 of 6 in Rover Ridge, Robotics World 1. Free with an account. It’s open as soon as you sign up.
Questions people ask
What is differential drive?
A way to steer with two independently driven wheels and no steering mechanism, usually with a caster for balance. Equal wheel speeds drive straight; a speed difference turns the robot toward the slower side. Tanks, wheelchairs, robot vacuums and most hobby robots steer this way.
How do you control a DC motor's speed with an Arduino?
Send a PWM signal with analogWrite, 0 to 255, to a motor driver, which switches the motor's bigger current. The motor responds to the average, so a higher duty cycle turns it faster. Never power a motor straight from a pin: it needs far more current than a pin can give.
How tight a turn can a two-wheeled robot make?
With forward speeds only, the tightest turn is a pivot: stop one wheel and the robot swings round it. If the motor driver can also reverse a wheel, running the wheels at equal speeds in opposite directions spins the robot on the spot.
Related topics
- How does PWM work?How a pin that's only on or off dims an LED: duty cycle and analogWrite's 0–255.
- PID control explainedWhat the P, I and D terms each do, shown on a drone climbing to 10 m.
- MOSFET as a switch, explainedA gate voltage, not a current, opens the channel: threshold, R_DS(on) and the flyback diode.