How To Use A Quadrature Encoder - Let's Make Robots!
A quadrature encoder measures the speed and direction of a rotating shaft using two sensors that are 90 degrees out of phase. It outputs two square waves that can be used to determine the direction of rotation. The encoder readings are converted to binary numbers that change in a predictable pattern depending on the direction of rotation. These can be read and used to determine speed and direction of rotation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
385 views6 pages
How To Use A Quadrature Encoder - Let's Make Robots!
A quadrature encoder measures the speed and direction of a rotating shaft using two sensors that are 90 degrees out of phase. It outputs two square waves that can be used to determine the direction of rotation. The encoder readings are converted to binary numbers that change in a predictable pattern depending on the direction of rotation. These can be read and used to determine speed and direction of rotation.
By OddBot @ January 3, 2011 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 90out of phase as shown below. Start Here Rules TopTips Robots Something else Blogs Reviews 3D print club Challenges Forums Recent About My Account My stuff Home Search User login Username: * Password: * Create new account Request new password Log in using OpenID Recent blog posts super fast robotic arm! Using the Arduino PID Library for precise position control of X and Y axis on RepScrap printer A simple HTTP communication protocol between wifi and Arduino Further Progress on framing my RepScrap 3D printer. Arduissimo: MultiCore Arduino indiegogo project Yahmez Goodies! Robots Ready to Rumble! Xinjiang, robots and bombs! My Repscrap: DC motors and rotary encoders for Z-Axis too? Why I Respect my Robot Elders more Recently submitted links: RC Tank Chassis Crawler Intelligent Barrowload Tractor Obstacle Caterpillar Wall-e Infrared Ultrasonic Patrol 2D Ruben tube Control Your Robot With Node.js - Raspberry PI How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031 1 of 6 5/15/2014 2:24 AM 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 or 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. and Arduino Poppy Project - Open Source Humanoid robot RoboRium - The Robot Emporium Hackaday in Shanghai Kickstarter: MicroView: Chip-sized Arduino with built-in OLED Display! A comprehensive path-finding library. (links to python and javascript source) Conductive Ink ... is your Bath Sponge Safe FoxyTronics Who's online There are currently 27 users and 21 guests online. Online users Polar Vortex mintvelt Dan M Duane Degn lamorak24 unix_guru lazd Hackgar hoff70 gamborg kariloy HaZZa Roxanna77 fritsl lukeyes jscottb Ladvien Gromer birdmun Photolong TinHead bstag hoff70 ossipee Ladvien mogul webmaster Quick links LMR on Google+ LMR on Facebook LMR on Flicker LMR on Twitter LMR Scrapbook User list Unread posts RSS feeds Spam Control How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031 2 of 6 5/15/2014 2:24 AM 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: How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031 3 of 6 5/15/2014 2:24 AM 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. By Hyvok @ Tue, 2012-07-10 15:03 Tool for creating optical encoder discs Login or register to post comments 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. By chrisc @ Sun, 2012-05-20 12:51 thx for the help! Login or register to post comments 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: http://robotblogging.blogspot.co.uk/2012/05/quadrature-encoder.html By afeibig @ Mon, 2012-02-20 03:59 Im I allowed to use the article elsewhere? Login or register to post comments 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.
By OddBot @ Mon, 2012-02-20 12:55 No problem. The information Login or register to post comments No problem. The information is posted here for everyone to learn from. By Anas @ Sun, 2012-01-08 16:56 How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031 4 of 6 5/15/2014 2:24 AM the XOR will give 1/2 the Login or register to post comments 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)
By Superus @ Wed, 2012-01-11 19:30 It worked! Login or register to post comments 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=) By OddBot @ Fri, 2011-12-30 13:26 I have had that problem when Login or register to post comments 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. By Superus @ Fri, 2011-12-30 19:38 It's only one interrupt Login or register to post comments 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=) By OddBot @ Sun, 2012-01-01 07:25 I cannot say.The encoder Login or register to post comments 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.
How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031 5 of 6 5/15/2014 2:24 AM
By OddBot @ Fri, 2011-12-23 16:39 Check your data types Login or register to post comments 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.
1 2 3 next last ALL LMR ARE BELONG TO US! Let's make robots! How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031 6 of 6 5/15/2014 2:24 AM