🔍
57 lessons

Encoders: Knowing Where You Are

Two light sensors and a slotted disc give a machine odometry — and the quarter-slot offset between them encodes direction.

lesson 3 of 4 in this unit

Builds on: 19.2 Servos & Steppers7.3 Memory & Counters11.2 Reading the Analog World

Odometry from a slotted disc

Bolt a slotted disc to a wheel and shine a light through it onto a sensor: every slot edge is one click of known distance. Count clicks, know how far you’ve rolled — odometry, the first sense any mobile robot grows. But one sensor has a blind spot: clicks look identical whether the wheel rolls forward or backward.

Quadrature: the quarter-slot trick

Add a second sensor, offset by a quarter of a slot, and the ambiguity dies. The two square waves — channels A and B — are 90° apart (quadrature, the same quarter-cycle idea as I/Q in 18.2!), and which one leads tells the direction:

on A’s rising edge: B low → one way, B high → the otherdecoded by edge detection + a flip-flop (7.3) — or four lines of interrupt code

Every mouse wheel, printer carriage and robot wheel uses this. Combine encoder feedback with the PID of 14.2 driving the H-bridge of 19.1 and you have a servo system of your own making — the industrial-grade version of the hobby servo’s little pot, good for unlimited rotation and real precision. That closed motion loop — sense position, compare, drive — is the beating heart of CNC machines, robot arms and self-balancing anythings.

The rest of the robot’s senses

Odometry drifts (wheels slip), so real robots fuse it with other senses: IMUs (accelerometer + gyroscope chips), distance sensors, cameras. Sensor fusion is a rich field — but every input still arrives as a voltage into an ADC (11.2) or bits on a bus (17.2). No new physics; just more of what you own.

⚡ Lab — The Quarter-Slot Trick

A slotted wheel, two offset sensors, and their quadrature waveforms live.

  • Spin forward: A leads B, and the count climbs. Reverse: B leads, count falls.
  • Stop the wheel mid-slot — the count holds. Position memory, no drift while still.
  • Watch the scope: 90° apart, always — the offset is mechanical, so it can’t lie.
1.20 rev/s
Digital odometry
each slot edge is a click of distance; which channel leads is the direction — flip-flop logic (7.3) decodes it
Where it lives
mouse wheels, printer carriages, robot wheels, factory arms — anything that must know where it is

Check your understanding

1. A single-channel encoder cannot tell you…

2. In quadrature encoding, direction is read by…

3. Encoder + PID + H-bridge together form…

4. Why do wheeled robots not rely on odometry alone?