Skip to content
RoboLearn
Not completed

Wheel encoders

SensorsIntermediate25 minBefore this: Wheels and drivetrains, What is a sensor?

What you’ll learn

  • How optical and magnetic encoders count rotation
  • How to turn counts into distance and speed
  • Why interrupts are needed, and what quadrature means

Counting turns

An encoder produces pulses as a wheel or motor shaft turns.

  • Optical: a slotted disc spins between an IR LED and a sensor; each slot makes a pulse. Common in kits with 20 slots.
  • Magnetic (Hall effect): a magnet on the motor shaft passes Hall sensors. Common on gear motors, often with hundreds of counts per wheel turn because they sit before the gearbox.

Counts to distance

distance = (counts ÷ counts per revolution) × wheel circumference

With a 20-slot disc and 65 mm wheels (204 mm circumference), each count is about 10 mm.

Interrupts

Pulses can arrive faster than loop() checks the pin, so encoders use interrupts: the Arduino pauses and runs a tiny function the instant the pin changes. On an UNO, pins 2 and 3 support attachInterrupt().

Quadrature

A single channel counts but can’t tell direction. Quadrature encoders have two channels (A and B) a quarter-step apart; which one changes first tells you the direction.

Why robots need them

  • Drive an exact distance
  • Turn an exact angle
  • Drive straight: if the left wheel counts faster, slow it down

Finished this lesson?