Saturday, November 3, 2018

An interrupt free quadrature decoder library for rotary incremental encoders

- updated to version 02 -

A rotary incremental encoder, also called a shaft encoder, is an electro-mechanical device that converts the angular position or motion of a shaft or axle to analog or digital output signals (

That kind of encoders may use mechanical or electronic sensors to read the rotation steps.
They have got two output channel, let's call that A and B.
When the rotary move at a fixed speed, they produce a quadrature-encoded output, that means the duty cycle of each pulse is 50%.

This library is an implementation of a quadrature decoder, that is a device that reads A and B channel output from an encoder and produce a direction and step count signals.

Rotary encoders may be of two types, one for hand use, with fixed rotation point that the user can feel, other for motors or rotative devices, with no rotation fisical step. This driver does not work well with rotary encoder with fixed point on step change, however that types of encoder is not common.

To decode the encoder status we have to build a state machine that implements the quadrature decoder table you find below.

Once that table is implemented, to avoid step missing we need to read the channels status with an appropriate speed. That speed might be 127 times more than the channels changing speed. For a manual rotary encoders 1ms reading time is enought, it means that we can read the channel change status at the frequency of 1kHz/127 = 8Hz.

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 1MHz.

  • 02: encoders process function can be called inside an external timer to prevent interrupt disabling, fixed initialize function problem
  • 01: first release


  • read risk disclaimer
  • excuse my bad english