Robotics
How to turn a robot 90 degrees with encoder ticks
To turn a robot a set angle, count encoder ticks on the turning wheel instead of timing the turn. A timed turn, delay(800) and hope, depends on how fast the wheel happens to spin: less on carpet, less on a tired battery. A quarter turn is a fixed arc of wheel, and a fixed arc is a fixed number of ticks, so a counted turn comes out the same on any floor at any speed.
Time lies
Pivot for 800 ms on a bench and the robot turns about a quarter turn. On carpet, where the wheel gets only 70 % of its speed, the same 800 ms gives about 66°. On a flat battery, about 43°. The program waited the same time; the wheel rolled different distances.
Ticks don't
In a pivot, one wheel stops and the other rolls round it, along a circle whose radius is the track width w, the distance between the wheels. The angle turned is the arc rolled divided by that radius:
With a 12 cm track, a quarter turn is an arc of 18.85 cm. A 6 cm wheel with 5 notches gives a tick every 3.77 cm, so that's 5 ticks.
if (now == LOW && last == HIGH) ticks = ticks + 1; // count edges
last = now;
if (ticks < 5) { // pivot on the right wheel
analogWrite(9, 255); analogWrite(10, 0);
} else if (ticks < 10) { // then straight
analogWrite(9, 255); analogWrite(10, 255);
} else { // then stop
analogWrite(9, 0); analogWrite(10, 0);
}
One running count with cumulative thresholds: pivot until 5, straight until 10, then stop.
Live experiment
Turn by the clock, or by the wheel
The rover pivots on its left wheel, either for 800 ms or until 5 ticks have come, on the bench, on carpet and on a flat battery. The timed turn shrinks as the wheel slows: 95°, 66°, 43°. The counted turn stays put at 97°, 95°, 93°: five ticks is a quarter turn, plus a little run-on.
“Turn by ticks” is lesson 5 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.
Close, not exact
The counted turn lands at 93° to 97°, not exactly 90°: the wheels take a moment to change speed after the count is reached, so every turn runs on a few degrees. The error is small and it stays small, unlike the timed turn's. To tighten it, slow the wheel for the last tick or two.
A spin turn, one wheel forward and one back, turns about the robot's centre: each wheel rolls round a circle of radius w ÷ 2, so it needs half the ticks per wheel for the same angle.
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.
“Turn by ticks” is lesson 5 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.
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: Less: the wheel rolls less in the same time. Carpet slows the wheel. Same time, less rolling, smaller angle. The ticks would have counted the shortfall.
- Why not “A full turn”? The timed turn gets shorter on carpet. Nothing makes it four times longer.
- Why not “More”? Carpet adds drag, so the wheel turns less in the same time, not more.
- Why not “The same”? Carpet slows the wheel, so in the same 800 ms it rolls a shorter arc and the turn is smaller.
Show the answer
Answer: Driving straight. 7 isn't under 5, so not pivoting; it is under 10, so straight. The thresholds are cumulative.
- Why not “Stopped”? The stop comes at 10 ticks. At 7 it's still on the straight leg.
- Why not “Pivoting”? The pivot lasts only while ticks is under 5. At 7 it's over.
- Why not “Reversing”? Nothing in this program reverses. At 7 ticks it's driving straight.
Show the answer
Answer: Cumulative: 5 for the pivot, then 10 for the end of the straight leg. One running count, so each leg ends at a total. The next unit resets per leg instead, so a list of lengths works.
- Why not “In seconds”? They count encoder ticks, not time. That's the whole point of turning by ticks.
- Why not “Per leg”? Per-leg counting needs the counter reset at each leg change. With one running count, each leg ends at a running total.
- Why not “Random”? The thresholds are fixed numbers compared with one steady count. Nothing is random.
“Turn by ticks” is lesson 5 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.
Questions people ask
How many encoder ticks make a 90° turn?
For a pivot round one stopped wheel, the moving wheel rolls a quarter of a circle whose radius is the track width w, so it needs (π ÷ 2 × w) ÷ distance per tick. With a 12 cm track and 3.77 cm per tick that's 5 ticks. A spin turn, wheels in opposite directions, needs half as many on each wheel.
Why is my encoder-counted turn still not exact?
Three usual reasons: the wheels run on a little after the motors stop, the wheels slip on smooth floors, and the wheel size or track width in your maths is slightly off. Slow down for the last few ticks, and calibrate by spinning ten full turns and adjusting the count.
Pivot turn or spin turn?
A pivot stops one wheel and drives the other, so the robot swings round the stopped wheel. A spin drives the wheels in opposite directions, turning on the spot about its centre. Spins turn in less room and take half the ticks per wheel, but need a motor driver that can reverse.
Related topics
- How a wheel encoder measures distanceCount the notches as the wheel turns, and you know how far the robot went.
- How a two-wheeled robot steers and sets its speedHow a two-wheeled robot turns with no steering wheel, and how PWM sets each motor's speed.
- How to make a two-wheeled robot drive straightEqual speeds don't mean straight. Measure each wheel and correct the difference.
- How a robot knows where it is with dead reckoningAdd up every step in the direction you face, and you know where you are, roughly.