Let's Make Robots!

How to use a quadrature encoder

measures speed / distance

A quadrature encoder, also known as an incremental rotary encoder measures the speed and direction of a rotating shaft. Quadrature encoders can use different types of sensors, optical and hall effect are both commonly used. The photo shows inside of a Rover 5 gearbox. There are two IR sensors on the PCB that look at the black and white pattern on one of the gears. No matter what type of sensors are used the output is typically two square waveforms 90° out of phase as shown below.

If you only wish to monitor the speed of rotation then you can use either output and simply measure the frequency. The reason for having two outputs is that you can also determine the direction of shaft rotation by looking at the pattern of binary numbers generated by the two outputs.

Depending on the direction of rotation you will get either:

  00 = 0
  01 = 1
  11 = 3
  10 = 2


  00 = 0
  10 = 2
  11 = 3
  01 = 1

By feeding both outputs into an XOR gate (exclusive OR) you will get a square wave with twice the frequency regardless of direction. This can be useful as it allows one interrupt pin to monitor both encoder inputs.

I was looking at how to write efficient code to convert these binary inputs into a simple "forward or backward" output. I ended up with a 2 dimensional array (matrix) that made the code quick and easy.

The binary values above convert to 0,1,3,2 or 0,2,3,1 depending on the direction. This pattern repeats continuously. By using the current value from the encoder to index one dimension of the array and the previous value to index the other dimension you can quickly get a -1, 0, or +1 output. My array looks like this.

As you can see, if the value has not changed then the output is 0.
The sequence of  0, 1, 3, 2 gives an output of -1.
The sequence of  0, 2, 3, 1 gives an output of +1.

X represents a disallowed state and would most likely occur if the encoder outputs are changing too quickly for your code to keep up. Normally this should not happen. In my code I put a 2 here. When I get an output of 2 I know that I got an error, perhaps due to electrical noise or my code being too slow. If you replace X with 0 then the disallowed state will be ignored.

In my Arduino code I make this a 1 dimensional array. that looks like this:

int QEM [16] = {0,-1,1,2,1,0,2,-1,-1,2,0,1,2,1,-1,0};               // Quadrature Encoder Matrix


To read the array my index is: Old * 4 + New
So my code reads like this:

Old = New;
New = digitalRead (inputA) * 2 + digitalRead (inputB);           // Convert binary input to decimal value
Out = QEM [Old * 4 + New];

Good luck and enjoy.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thanks for the tutorial! If someone wants a simple way to make encoder discs check out my post about an extension that I wrote for Inkscape: http://www.dgkelectronics.com/inkscape-extension-for-creating-optical-rotary-encoder-discs/

You can make single- and dual-track (quadrature) encoders with it, in any size/number of segments.

I've been building my own quadrature encoder and this was awesome! Thanks OddBot! 

Info is here if anyone wants a look - it covers building the encoder and then uses the work OddBot did above to read it:


Hi, I'm new in the site, and i like to use some parts of this article to write another one, about my own encoders for a personal projecto, obviously respecting you as the owner.


Thanks in advice.



No problem. The information is posted here for everyone to learn from.

the XOR will give 1/2 the encoder rate(2X). To get the full rate(4X) you should use transitions on both A and B. 

the LS7183/LS7184 are CMOS QUADRATURE CLOCK CONVERTER that may help in this.(http://www.lsicsi.com/pdfs/Data_Sheets/LS7183_LS7184.pdf)

For Ardunio users,  A high performance Encoder library is now available, with 4X counting and very efficient interrupt routines. Uses the External Interrupt pins of the MCU.(download it here http://www.pjrc.com/teensy/td_libs_Encoder.html)


Thank you very much! You saved my day=)

With this library I'm able to get correct results from my encoder and it works beautifully. Thank you=)

I have had that problem when I have had more than 1 interrupt enabled at once. It seems as though the processor is having trouble keeping up with multiple interrupts. I found that changing my interrupt mode from "Changing" to "Rising" helped as it halved the number of interrupt requests.

Thank you for that quick answer yet again, but..(here it comes)... it is only one interrupt in use and it is already in rising mode. I have cut the sketch down into only the necessary code and tried with an RoMeo board too, without luck. I have probed the outputs of the encoders with an oscilloscope and changed the XOR chip, but I found no irregularities. I have tested the "sampling rate" of the interrupt and found out that it could handle much higher speeds.

Too be honest, I have no clue of why this is happening. I appreciate any kind of help, and thank you very much if you help me=)

I cannot say.

The encoder boards are factory tested on an oscilloscope to calibrate the sensor. They are adjusted to give two 50% dutycycle square waves 90 degrees out of phase.

I would rule out electrical noise from the motor as the test with the oscilloscope is done with the motor running and there is no noise on the display.

When I get back to the office I can do a test.



I've had weird results before due to not using the correct variable types. In one case where I used an integer instead of a byte I had my code work perfectly when I first tested it but later give me weird results.