Show by Label

Monday, November 14, 2016

Demistifying Rotary Encoders (some more)

For an Arduino based project, I wanted to use a rotary decoder to control a menu based structure.

As you are probably aware, there are dozens of solutions available, typically one more complex than the other, in an attempt to make it reliable and fast.

There are two schools of thought. You use the recognoition is the main loop construct, or you use interrupts. In the first case, you need to carefully design the main loop, because if you don't get the timing right, the recognition of a twis of the decoder will be slow, or can be missed. This makes for a very poor user interface (U/I).

Using an interupt to recognise movement can be more responsive, unfortunately, almost all interrupt based solutions use two interrupts. On the Arduino Nano or Mini-Pro, there are only two external interrupts available, so using both can be a problem. The good news is that you really don't need two interrupts.

If you look at the datasheet, you are presented with perfectly modeled wave forms of the two switches that are the heart of the decoder. Here is a picture of a typical in-expensive decoder switch, and the waveforms.

Image result for rotary encoder switchImage result for rotary encoder switch

First off, the real wave-forms are not perfectly symmetrical, the output is depending on the mechanical construction and the rotation speed. The other important bit of information is that practically, you rotate the switch from indent to indent.

Here is a screen shot from a one indent move forward made with a Logic Analyzer:
And here is the screen shot of moving one indent anti-clockwise, or back.

Notice the different pulse width of the A and the B switch in both cases.

The challenge is to not only detect a rotation movement, but also the direction, and then in such a way that you can also rotate the switch very fast and be correct.

In the following Arduino sketch, I use one interrupt on the rising edge of the A-switch, and then sample the level of the B-switch. As you can see above, a clock-wise (to the right) rotation will cause the A-switch to become High before the B-switch. If you turn the other way, the B-switch is already high when the A-switch becomes high.

In order to track and visualize what is going on, I added some statements in the code that will generate a trigger pulse so the Logic Analyzer or scope will help us with the timing relationships.

Here is the sketch:

/* Software Debouncing - Mechanical Rotary Encoder */

#include <FaBoLCD_PCF8574.h>             //include the i2c bus interface and LCD driver code

//---- initialize the i2c/LCD library
FaBoLCD_PCF8574 lcd;                     //with this, there are no further code changes writing to the LCD

#define encoderPinA 2                    //encoder switch A
#define encoderPinB 4                    //encoder switch B
#define encoderPushButton  5             //encoder push button switch
#define Trigger 6                        //Trigger port for Logic Analyzer or Scope

volatile int encoderPos = 0;
volatile int oldencoderPos = 0;

void setup() {
  pinMode(Trigger, OUTPUT);
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  pinMode(encoderPushButton, INPUT); 
  attachInterrupt(digitalPinToInterrupt(encoderPinA), rotEncoder, RISING); //int 0 
  lcd.begin(16, 2);                      //set up the LCD's number of columns and rows
  lcd.clear();                           //clear dislay
  lcd.setCursor(0,0);                    //set LCD cursor to column 0, row O (start of first line)
  lcd.print("Rotary Encoder");
  lcd.setCursor(0,1);                    //set LCD cursor to column 0, row 1 (start of second line)

void rotEncoder(){
  boolean rotate;
  delayMicroseconds(300);                //approx. 0.75 mSec to get past any bounce
                                         //delay() does not work in an ISR
  //send entry Trigger pulse
  digitalWrite(Trigger, HIGH);
  digitalWrite(Trigger, LOW);
  rotate = digitalRead(encoderPinA);           //Read the A-switch again
  if (rotate == HIGH) {                        //if still High, knob was really turned
    if (rotate == digitalRead(encoderPinB)) {  //determine the direction by looking at B
    } else {                                  
  //send exit Trigger pulse
  digitalWrite(Trigger, HIGH);
  digitalWrite(Trigger, LOW); 

void loop() {
  //loop until we get an interrupt that will change the encoder position counter
  if (encoderPos != oldencoderPos) {
    lcd.print("      ");
    oldencoderPos = encoderPos;

And here is a screen shot of a forward indent with the trigger pulses:

As you can see from this data, it takes the Arduino 755 uSec from the A-switch rising edge recognition to the entry in the Interrupt Service Routine (ISR). It then only needs 14.2 uSec to do the rotation recognition.

To put this into perspective, so you get an idea of the relative "blinding" speed of a 16MHz Arduino in relation to slow moving switches:

 Turning the knob as fast as I can produces this picture:

More later...