Skip to main content

Arduino Rotary Encoder Library with Velocity Sense


While working on the constant current load project I found that while nice, the multiple turns required to turn up the power up was a little annoying when you just wanted to hurry up and get to a high value. The standard solution is to have a "fine" and "course" knob. Since I designed the input in the digital realm it seemed like software was the obvious solution. Why not sense the speed the user is turning the knob and extrapolate the pace of change based on that input. It seems intuitive to me. I implemented it as a little C++ library that you can drop into your arduino/libraries directory.

Here is the most trivial implementation of the library.
#include <RotaryEncoder.h>;
RotaryEncoder encoder(A0,A1,5,6,1000);
void setup()
{  
  Serial.begin(57600);
}
void loop()
{
  int enc = encoder.readEncoder();
  if(enc != 0) {
    Serial.println(enc);
  } 
  delayMicroseconds(5);
}

Inside the library, the code counts the number of the sequential clicks in one direction within less than the time constant you pass in when you instantiate the object. In the above example, the selected time constant is 1000µs (the 5th variable). If the user waits longer than that amount of time the counter resets. If the user changes direction the counter resets. To determine the return value I divide the counter by the 4th variable you pass in, 6 in this case, and then multiply that by 5 (the 3rd  variable) and return that value. Turning anti-clockwise is the same except that I make it negative (multiply value by -1). So, if you turn the knob 30 times in a clockwise direction I will return (30/6)*5 or 25. Bear in mind that 30 times doesn't mean 30 detents of the encoder. Each encoder is different. The ones I have in my drawer return 4 times per detent, YMMV.

The library is available here.

In order for this to work, you need to have clean reads of the encoder (or it will reset the counter). To do this effectively, you really need to "debouce" the switch. I've never been able to effectively do this in software. I use 10000pF capacitors between the A pin and GND and B pin GND. This makes the circuit very well protected from bad reads. Furthermore, you need to check the value more frequently than it can change otherwise the quadrature signal will become out of sync and you will not know what direction you are turning.

Comments

Popular posts from this blog

A Capacitive-Touch Janko Keyboard: What I Did at the 2017 Georgia Tech Moog Hackathon

Last weekend (February 10-12, 2017) I made a Janko-layout capacitive-touch keyboard for the Moog Werkstatt at the Georgia Tech Moog Hackathon. The day after (Monday the 13th), I made this short video of the keyboard being played: "Capacitive Touch Janko Keyboard for Moog Werkstatt" (Text from the video doobly doo) This is a Janko-layout touch keyboard I made at the 2017 Moog Hackathon at Georgia Tech, February 10-12. I'm playing a few classic bass and melody lines from popular and classic tunes. I only have one octave (13 notes) connected so far. The capacitive touch sensors use MPR121 capacitive-touch chips, on breakout boards from Adafruit (Moog Hackathon sponsor Sparkfun makes a similar board for the same chip). The example code from Adafruit was modified to read four boards (using the Adafruit library and making four sensor objects and initializing each to one of the four I2C addresses is remarkably easy for anyone with moderate familiarity with C++), and ...

Atlanta Cosplay Meetup: Group Build Update #3

It's been a while since we posted a progress report for the Atlanta Cosplay Meetup's ongoing project, and with Dragon Con right around the corner, we're nearing the finish line. Let's take a look and see what's been going on the last few months! Check out our previous progress reports here: Progress update #1 Progress update #2 Read on to see where we're at now...

What to Do With a Stack of Picture Frames?

When You Have Too Much Free Stuff! Our newest member Raul got his hands on a stack of about 40 picture frames that were being junked. On a general note Freeside tends to discourage large piles of objects randomly appearing as it tends to collect in corners. Raul got permission from our projects team with a time limit of a few weeks. In this case unnecessary, as the membership more or less attacked the pile of boxes and rapidly rendered them into things. Unfortunately starting off all the frames looked something like this: Not terribly useful. We don't even have any idea who these guys are. After a few passes through the planer, however, we get something like this: A perfectly good picture frame useful for stuff. First idea was to push a couple of these through a the laser cutter. Concept good, aim.... Aim was a little off. Also we had just rebuilt the laser computer and electronics so there were a couple of kinks to work out in CamBam's post processor: &nbsp ...