TL;DR:
An encoder counts how far a wheel has turned. Ticks times distance-per-tick gives distance travelled.
Comparing the two wheels of a robot also gives how much it has turned.
Slip, uneven wheels and counting limits add error, so encoders are best combined with other sensors.
You tell your robot to drive 2 meters forward and it stops somewhere near 1.8. Without any way to measure its own motion, it has no idea. A wheel encoder gives it that sense, by counting how much each wheel has turned. With a little arithmetic, those counts become distance and even direction.
How an encoder counts
A common design has a disc with slots or marks attached to the wheel shaft, and a sensor that gives a pulse each time a slot passes. Each pulse is a tick. A disc with 360 slots gives 360 ticks for one full turn of the wheel, if the encoder sits on the wheel shaft. Many encoders have two sensors offset from each other (quadrature), so the controller can tell the direction of rotation and can count four times as many edges per slot. Check your encoder's data sheet to see what its "counts per revolution" figure includes.
Worked example: 360 ticks per turn on a 0.2 m wheel
Say the wheel has a diameter of 0.2 m and the encoder gives 360 ticks per full turn of the wheel.
Circumference:
C = π × d = π × 0.2 = 0.6283 m. This is how far the robot moves in one full turn.Distance per tick:
0.6283 ÷ 360 = 0.001745 m, about 1.75 mm.Say the encoder reads 1200 ticks. Turns:
1200 ÷ 360 = 3.333.Distance:
1200 × 0.001745 = 2.094 m. Check:3.333 × 0.6283 = 2.094 m. Same.
To drive exactly 2.000 m, the controller counts 2.000 ÷ 0.001745 = 1146 ticks, then stops. That is a good plan on a clean floor. The resolution is 1.75 mm per tick, so the counting itself is not the limit on accuracy.
Turning: using both wheels
On a two-wheeled robot, called a differential drive, the difference between the two wheels says how much it has turned. If the wheels are a distance b apart (the wheelbase), then the change in heading in radians is Δθ = (d_right − d_left) ÷ b.
Say the left wheel reads 1000 ticks and the right wheel 1200, with b = 0.4 m:
Left distance:
1000 × 0.001745 = 1.745 m.Right distance:
1200 × 0.001745 = 2.094 m.Forward distance of the robot's centre is the average:
(1.745 + 2.094) ÷ 2 = 1.92 m.Turn:
(2.094 − 1.745) ÷ 0.4 = 0.349 ÷ 0.4 = 0.873 rad, which is0.873 × 180 ÷ π ≈ 50°.
So the robot moved about 1.92 m along a curve and turned 50° to the left. Doing this update every few milliseconds and adding up the small motions is called odometry. It gives a running estimate of position and heading.
Resolution
Resolution is the smallest movement you can detect. Here it is one tick, 1.75 mm of rolling distance. More ticks per turn or a smaller wheel gives finer resolution. Speed is measured by counting ticks in a fixed time: 36 ticks in 0.1 s is 360 ticks per second, one wheel turn per second, or 0.628 m/s.
Slip and other errors
The conversion assumes that every tick of the wheel moves the robot by the same distance on the ground. This breaks down when:
Wheels slip on a smooth floor, in a hard acceleration, or when pushed sideways. The wheel turns but the robot does not move as far.
The wheel diameter is wrong. A tyre that compresses under load is smaller than the number you measured. A 1 mm error in 200 mm changes every distance by a small fixed fraction.
The wheelbase is wrong. Turn angles depend on
b, and the effective value is often a little different from the one you measure.The robot is lifted or pushed. Encoders count wheel motion, not robot motion.
These errors add up and never reset, so odometry drifts over time. Robots correct for this by combining encoders with other sensors, such as a gyroscope for heading and a camera or LiDAR for position.
Tip: Calibrate on the real floor. Drive the robot a measured 2 m, compare the encoder distance, and adjust the wheel diameter in your code until they agree. Do the same for a measured 360° spin to adjust the wheelbase.
Common mistakes
Using the quadrature count as if it were the slot count, which gives a factor-of-four error.
Mixing the units of metres and millimetres.
Reading the counter in the main loop so slowly that ticks are missed. Use interrupts or a hardware counter.
Forgetting that the counter can overflow.
Your plan for this week
Roll the wheel by hand along a metre stick and note the number of ticks. Divide the distance by the ticks to get your own distance-per-tick, and compare with the calculation above. If you want to check the arithmetic with a diagram, DuckyHelper can sketch the two-wheel turn on the blackboard with you.
Frequently asked questions
Why do some encoders have two output channels?
Two channels, offset from one another, let the controller work out which way the wheel is turning, and give more counts per turn.
Can encoders give absolute position?
Most simple ones are incremental and only count change. Absolute encoders exist and report a position directly, but they are more complex.
