Robotics
How a wheel encoder measures distance
A wheel encoder turns the wheel's rotation into ticks: a disc with notches or magnets turns with the wheel, and a sensor gives a pulse each time one passes. Count the ticks and you know how far the wheel has turned; multiply by the distance per tick and you know how far it rolled. Divide by time and you have its speed.
From ticks to distance
Each tick is one notch, a fixed fraction of a turn. One turn rolls the wheel its circumference, π times its diameter:
A 6 cm wheel with 5 notches has a circumference of 18.85 cm, so each tick is about 3.77 cm, and ten ticks are 37.7 cm. More notches, finer steps: hobby encoders have 20 slots, and the encoders on small gear motors give hundreds of counts per wheel turn.
Count the edge, not the level
The encoder's pin goes LOW while a notch passes the sensor and HIGH between notches. A pulse might last 25 ms, and loop() comes round hundreds of times in that, so counting every pass that reads LOW counts one notch hundreds of times. Count the change from HIGH to LOW instead, which happens exactly once per notch:
bool now = digitalRead(2);
if (now == LOW && last == HIGH) ticks = ticks + 1;
last = now; // every pass, outside the if
last has to be updated on every pass, or the pattern HIGH then LOW never shows up again. And now != last would count both ends of each pulse, doubling every tick.
Live experiment
Count the ticks, two ways
The rover drives until its count reaches 10, with five notches round a 6 cm wheel: a tick every 3.77 cm. Count every pass that reads LOW and one notch counts hundreds of times, so it stops at once. Count the HIGH-to-LOW edge and it stops after ten notches, 37.7 cm.
“Count the ticks” is lesson 3 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 9 before it first.
Speed, and interrupts
Count ticks over a fixed window and you have speed. With 20 ticks a turn, a count of 20 in 50 ms is 20 turns a second, 1200 rpm. A count can only be whole, so over 50 ms each tick is worth 60 rpm: a longer window gives finer steps but an older reading.
For fast wheels, let the hardware watch the pin: attachInterrupt(digitalPinToInterrupt(2), onTick, FALLING) runs a tiny function on every falling edge, even while loop() is busy. Make the count volatile so the main code sees each change.
Which way?
A single sensor can't tell forwards from backwards. A quadrature encoder has two sensors, A and B, a quarter of a cycle apart: whichever changes first gives the direction, and counting every edge of both gives four counts per slot. Robots, 3D printers and the volume knob on a car stereo all use them.
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.
“Count the ticks” is lesson 3 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 9 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: now == LOW && last == HIGH. A tick starts when the pin goes from HIGH to LOW. now != last would also count the end of the pulse, doubling every tick.
- Why not “now == LOW”? The pin stays LOW for the whole pulse, many passes of loop(), so counting the level counts one tick many times.
- Why not “last == LOW”? last == LOW is still true after the pulse starts, on every pass while it lasts, so it overcounts too.
- Why not “now != last”? now != last is true at both ends of a pulse, going LOW and coming back HIGH, so every tick counts twice.
Show the answer
Answer: Four full turns of the wheel. One notch per tick, five notches round the wheel: 20 ÷ 5 is four turns. Distance is turns times the wheel's circumference: four turns of a 6 cm wheel is about 75 cm.
- Why not “Twenty turns”? Each tick is one notch, a fifth of a turn, not a whole turn.
- Why not “Five turns”? Five is the notches per turn, not the number of turns: 20 ÷ 5 is 4.
- Why not “One turn”? One turn is five ticks, one per notch. Twenty ticks is four of those.
Show the answer
Answer: Slightly short: the wheel turned but the rover didn't move as far. Ticks count the wheel, not the floor. Slip is the one thing an encoder can't see, which is why robots that need precise angles add a gyro.
- Why not “Unchanged by slip”? Slip is exactly what an encoder can't see, which is why robots that need precise angles add a gyro.
- Why not “Longer”? A slipping wheel turns without moving the robot as far, so the turn comes out shorter, not longer.
- Why not “Exactly right”? Encoders count the wheel's turning, not the robot's movement, so slip goes unseen and the turn falls short.
“Count the ticks” is lesson 3 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 9 before it first.
Questions people ask
How does a wheel encoder work?
A disc with slots or magnets turns with the wheel, and a sensor beside it, an optical gap sensor or a Hall effect sensor, gives a pulse each time a slot or magnet passes. Counting pulses counts fractions of a turn.
How do I calculate distance from encoder ticks?
Distance = ticks × wheel circumference ÷ ticks per revolution. The circumference is π times the wheel's diameter. A 6 cm wheel with 5 ticks a turn moves about 3.77 cm per tick.
What is a quadrature encoder?
An encoder with two outputs, A and B, a quarter of a cycle apart. Which one changes first tells you the direction of rotation, and counting every edge of both gives four counts per slot, for finer resolution.
Should I use interrupts to read an encoder?
For fast wheels, yes. attachInterrupt() runs a short function on every edge, so no tick is missed even while loop() is busy. For a slow wheel and a loop() with no delays, checking for the edge on every pass works too.
Related topics
- 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.
- How to make a two-wheeled robot drive straightEqual speeds don't mean straight. Measure each wheel and correct the difference.
- 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.
- What is Gray code, and why do encoders use it?A count where only one bit changes per step, and why encoders depend on it.