The Keyes KY rotary encoder is a rotary input device as in knob that provides an indication of how much the knob has been rotated AND what direction it is rotating in. You could also use it to control devices like digital potentiometers. These are sold in several places, including the vendors below. Each of these vendor offers solid buyer protection through PayPal. A rotary encoder has a fixed number of positions per revolution. The Keyes module that I have has thirty of these positions.
Hey, thanks for the great example, saved me quite some time getting a KY running with python on a raspberry pi.
Here is the code adapted to python, maybe someone will find it useful: Thanks for taking the time to post this information! It came in handy while making an internet radio player with rotary encoders. The same code is available on my blog page, http: I am having many of the problems with a KY rotary encoder identified in these user comments.
I put together the following sketch to help me understand what sequence of interrupts were occurring. The sketch traps one or more detent turns of the encoder and when a button is pushed, a report is displayed on Serial Monitor.
This approach removes any Serial. I hope this helps sort out encoder issues. I use one of these in a signal generator designed by Richard Visokey. It causes the readout to change on every other click rather than on every click. Do you think this a function of the encoder or the associated sketch? I wrote a library that uses interrupts. B changes to LOW, no effect 4.
A and B are now both LOW 5. B changes to HIGH, no effect 7. B changes to LOW, no effect 3. A and B are now both LOW 6. B changes to HIGH, no effect 5. Removing all the Serialt. I used a sample code with tiva and connected encoder with parallel capacitor 10nf and series resistance 10k as they Say.
I removed the capacitor and it was better more and more than the two mentioned cases. Hi kbellco, Many thanks for the tip! I spend hours trying to debounce hardware, software, etc. I put it on my oscilloscope and studied the traces and discovered that no debouncing was needed.
Instead, the the logic finally became clear and a simple change to the if statement was all that was needed. At that point, check what bVal is reading. This immediately fixed all my problems and I retrieved the other 5 Rotary Encoders from the trash. Your code is forcing me to dig in and learn more than just the simple programming, as given on the Arduino site examples which is what teaching is all about!
Keyes KY-040 Arduino Rotary Encoder User Manual
Hi, do you happen to know how make it work on a Digispark by any chance? Thanks for useful information. Just to help more: Thank you for posting all this information in one place. Because of you i was able to complete my project. Your email address will not be published. Print Share on Tumblr.
Arduino rotary encoder hookup
Manjula Mmahindasiri May 17, Dear, I want to made rpm speed meter by using 4digit 7 segment. Anthony Brinkhuis April 20, Unlikely, the other methods failed, the counting rate was too fast. To escape reading Arduino's port programming, I thought it may be even faster just using the interrupt pins. Here is the code:. Uses both External Interrupt pins. Based on the Circuits home code. Notice that the Serial. This may change the behavior of the code when those statements are removed as some bounces and even some transitions may be missed if the interrupt routine takes a comparatively long time.
You need to choose pins that have Interrupt capability. Uses 1 Interrupt pin but misses half the state transitions.
Claims to be fast but uses digitalRead instead of direct port manipulation see http: After try all examples, I think that I have the fastest and working form for the SparkFun Rotary encoder. Only one thing more: Uses digitalRead instead of direct port manipulation in the interrupt routines see http: Few code lines, full encoder precision and achieve the Arduino max acquisition rate. Pins, left to right: I tried most of the above but found that they do not reliably count steps up and down.
Most have trouble with debouncing. While I use all the tricks regarding interrupt usage and efficiency above, I found this one to work best when precision counts I've been playing around with a pretty cheap mechanical encoder and found bouncing was a big issue when I experimented with the other code above.Arduino Basics Rotary Encoders
However, thanks to Hifiduino I managed to get my encoder working pretty well with a simple bit of software debouncing. The code uses an interrupt to detect any signal change from the encoder, but then waits 2 milliseconds before calculating the encoder position:. This tiny amount of code is focused on keeping the interrupts fast and totally responsible for encoder position.
This is an example that supports multiple encoders, and is designed for hand-turned encoders, not motors. This is meant for user interfacing, not hardware monitoring. You need two pins for each encoder, so on a Nano, theoretically, you could put 7 encoders on the digital pins and 3 more on the analog pins with some code rewriting. You might even be able to squeeze one more out of the left over digital and analog pins.
I have tested it only with two encoders all I needed , but it worked flawlessly and I turned both at the same time.
I could not detect misses through watching the output and feeling the detents tick by. It is good enough for a user interface. If you needed many encoder knobs, you could use a Nano to read in and track the values, and then send the updates to another microcontroller to do something about it. This could be improved by converting it to use PINx to read the values simultaneously instead of digitalRead.
It includes a software debouncer. It detects all four states of transition, so every knob turn gets registered. It does not use any external interrupts, which would limit the number of encoders to 1 full read or 2 every other position read. Since we are not using interrupts, the accuracy at speeds faster than hand turning would be awful, the faster it went the worse it would be.
The ATmegaP has two different kinds of interrupts: Most of the sketches and libraries given on this page use one or two of the External Interrupt pins. The interrupt is theoretically very quick because you can set the hardware to tell you how you want the interrupt to trigger.
Each pin can have a separate interrupt routine associated with it. On the other hand the pin change interrupts can be enabled on any or all of the ATmegap 's signal pins. This makes the job of resolving the action on a single interrupt even more complicated. By testing both outputs with a microcontroller we can determine the direction of turn and by counting the number of A pulses how far it has turned. Indeed, we could go one stage further and count the frequency of the pulses to determine how fast it is being turned.
We can see that the rotary encoder has a lot of advantages over a potentiometer. We will now use the rotary encoder in the simplest of applications, we will use it to control the brightness of an led by altering a pwm signal.
We will use the easiest method to read the encoder, that is the use of a timer interrupt to check on the values. We will use the sparkfun encoder as discussed above.
The first thing is to determine how fast we need our timer to operate. In reality its never likely to be this fast. As we need to detect both high and low values this gives us a minimum frequency of Hz. Lets go for Hz just to be sure.