Wrong counts usually come from noise on the signal or from reading too slowly. Use both channels and interrupt-capable pins.
What the message means
A quadrature encoder gives two pulse trains, A and B, a quarter cycle apart. The order of the changes tells the direction. Missed or false edges add up as drift.
Fix it in this order
- Connect both channels A and B, plus power and ground at the right voltage.
- On an Uno use pins 2 and 3 for interrupts. On an ESP32 most pins can take interrupts.
- Keep ISR code short: update a counter and return.
- Mark the shared counter volatile, and copy it with interrupts off if it is more than 8 bits.
- Add pull-ups if the encoder has open-collector outputs, and a small capacitor on noisy lines.
- Run the encoder wires away from motor wires, and twist or shield them.
- Check the pulse rate against what the board can handle at full motor speed.
Why it happens
A quadrature encoder produces two square waves, A and B, offset by a quarter cycle. The order of their edges indicates direction, and each edge counts one step. If the code misses an edge, or sees a false edge from noise or contact bounce, the count drifts away from the real position. Interrupts catch edges as they happen, which is why the interrupt handler must stay short.
A counter shared with an interrupt should be declared volatile, or the compiler may keep a stale copy in a register. Reading a multi-byte value while interrupts can still change it may return a half-updated number. Noise from motor wiring can couple into the signal lines, so keeping encoder wires short, twisted, or shielded reduces false edges. Pull-up resistors help open-collector outputs settle cleanly.
If you see this, do this
| You see | Do this |
|---|---|
| Counts go up at a standstill | Noise on the signal. |
| Counts too low at speed | Missed pulses. Use interrupts or a hardware counter. |
Confirm the fix
- Rotate the encoder slowly by one detent at a time and confirm the printed count changes by exactly one per step.
- Spin the encoder quickly and compare the final count with the number of detents turned; a mismatch points to missed edges.
- With the motor off, watch the count for thirty seconds; any change means noise or bounce on the signal lines.
Common mistake: Assuming the encoder is faulty when the real cause is noise on the wiring or a slow interrupt handler that misses edges.
Related
encoder counts calculator, wheel speed calculator.
Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.