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 ...

Onboard Firmware of the Human Brain

Freesiders are continually tinkering with robotics and other such machinery .  Many of these embedded processors and firmware are becoming open source and every-more diversified in the wake of the modern Maker movement . One notable boost to the hackerspace arsenal is the Arduino (an like platforms).  This offers designers an incredible power to devise not just individual devices but even the emergence of complex, integrated systems . This evolutionary pace of modern technological systems may be significantly faster the biologic system development, but there may be a few well learned tricks yet to be mastered.  It seems that studying how nature has managed to solve many development challenges will aid in designing robotics, where efficiently counts just as much. One  challenge, that is particularly interesting, is data processing.  Artificial intelligence is labored with processing data and producing a meaningful and useful output.  When consid...

Freesiders Hackers Collaborate in Medical / Surgical Research

Published in the May issue of the Journal of Foot and Ankle Surgery : " A Novel Combination of Printed 3-Dimensional Anatomic Templates and Computer-assisted Surgical Simulation for Virtual Preoperative Planning in Charcot Foot Reconstruction ." This collaboration of specialties represents an undertaking by members of Freeside Atlanta , Southern Arizona Limb Salvage Alliance , and The Podiatry Institute .  Charcot foot reconstruction remains on of the most challenging procedures in foot and ankle surgery.  These procedures are often lengthy procedures which can be riddled with complications. With the help of Freeside Atlanta Members, institutional researchers used open source Osirix Image viewer and 3D Software such as Newtek's Lightwave or Blender to create simulated surgical reductions as well as 3D printed templates.  Freeside Atlanta members assisted in providing 3D printing solutions and know-how to the project. Experimental test prints were done on a M...