Skip to content

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:

ticks for 90° = (π ÷ 2) × w over distance per tick

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.

Try it yourself: free lesson

“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.

Try it yourself: free lesson

“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 needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3A pivot of 800 ms gives a quarter turn on the bench. On carpet it gives…
    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.
  2. Question 2 of 3Pivot while ticks < 5, straight while ticks < 10, else stop. At tick 7 the rover is…
    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.
  3. Question 3 of 3The ticks counter is never reset, so the thresholds are…
    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.
Keep practising: free lesson

“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.

Every topic