Home > BeagleBone, C++ > BeagleBone with a Rotary Encoder

BeagleBone with a Rotary Encoder

Rotary Encoder

PEC11 Incremental Encoder

I’ve been looking at a variety of potential input devices for my BeagleBone. adaruit packages a PEC11 Series – 12 mm Incremental Encoder and I decide to give it a try.

This encoder requires ground on the center lead and provides “output” on the two outside leads. As you turn (click) the encoder knob from one detent to another the output leads will signal either high or low. Combining the output from those leads gives you the relative position of the knob. Monitoring the changes in those output tells you if the encoder has been turned clockwise or counter clockwise.

BeagleBone with Rotary Encoder

BeagleBone with Rotary Encoder

I grounded the encoder using a ground pin from the BeagleBones expansion header and wired the two output leads to a GPIO pin of their own. I could now read the value (signal) from each output lead. All this is pretty straight forward stuff when using a BeagleBone. Now for the fun stuff.

I’m using the sysfs GPIO interface to read output from the encoder. This means that I have to set the direction of the GPIO to “in”. Polling the GPIO’s value returns either a “1” or “0”. As you turn the encoder the value of each GPIO will change from 1 to 0 and then back to zero. But they transition from one value to another at different positions as the encoder turns. This is called a quadrature outputs.

Quadrature Output Table

Quadrature Output Table

This quadrature output table is taken from the PEC11’s data sheet. For the untrained (like me) this diagram is a bit tough to understand at first. When the encoder is positioned on a detent both the A and B leads will signal low. As the encoder is turned clockwise (CW) the A lead will start to signal high and then B lead will follow with a high signal. As the encoder continues to turn the A signal goes low and the B signal follows as the encoder arrives at the next detent. The opposite occurs if the encoder is turned counter clockwise (CCW). These signal pairs are called Gray code.

Another twist is that low signal from an encoder lead results in the GPIO’s value returning 1 (ON). A high signal returns a GPIO value of 0 (OFF). These tables describes the Gray code sent from the encoder and the associated values from the GPIO’s.

Clockwise
Encoder Gray Code GPIO Values
Phase A B A B
1 0 0 1 1
2 1 0 0 1
3 1 1 0 0
4 0 1 1 0
Counter Clockwise
Encoder Gray Code GPIO Values
Phase A B A B
1 0 0 1 1
2 0 1 1 0
3 1 1 0 0
4 1 0 1 1

An incremental rotary encoder such as this cannot represent it’s absolute position.  It just signals it’s position relative to the last detent it was on. Tracking the signal changes as it transitions from one detent to another will identify when the encoder travels clockwise or counter clockwise. This encoder has 24 detents (segments).

I wrote a program that constantly polls the two GPIO’s connected to the encoder’s A/B leads. With a very simple finite state machine (a switch statement) I was able to track the rotary of the encoder’s knob. Here’s some of the C++ code.

Rotary::Rotary(unsigned a, unsigned b) : _position(0) {
        A = new GPIO(a);
        B = new GPIO(b);

        A->direction("in");
        B->direction("in");
}

unsigned Rotary::phase() {
    unsigned a = A->value();
    unsigned b = B->value();

    a = (a == 0) ? 1 : 0;
    b = (b == 0) ? 1 : 0;

    return (a << 1) | b;
}

#define DETENT   0
#define CW_1     1
#define CW_2     2
#define CW_3     3
#define CCW_1   -1
#define CCW_2   -2
#define CCW_3   -3

void *Rotary::monitor(void *arg) {
        Rotary *rotary = static_cast<Rotary *>(arg);
        int state = DETENT;
        int last = -1;

        while (rotary->run_state) {
                int value = rotary->phase();

                if (value != last) {
                        switch (state) {
                        case DETENT:
                                if (value == 2) {
                                        state = CW_1;
                                }
                                else if (value == 1) {
                                        state = CCW_1;
                                }
                                break;
                        case CW_1:
                                if (value == 0) {
                                        state = DETENT;
                                }
                                else if (value == 3) {
                                        state = CW_2;
                                }
                                break;
                        case CW_2:
                                if (value == 2) {
                                        state = CW_1;
                                }
                                else if (value == 1) {
                                        state = CW_3;
                                }
                                break;
                        case CW_3:
                                if (value == 1) {
                                        state = CW_2;
                                }
                                else if (value == 0) {
                                        state = DETENT;
                                        rotary->_position++;
                                }
                                break;
                        case CCW_1:
                                if (value == 0) {
                                        state = DETENT;
                                }
                                else if (value == 3) {
                                        state = CCW_2;
                                }
                                break;
                        case CCW_2:
                                if (value == 1) {
                                        state = CCW_1;
                                }
                                else if (value == 2) {
                                        state = CCW_3;
                                }
                                break;
                         case CCW_3:
                                if (value == 3) {
                                         state = CCW_2;
                                }
                                else if (value == 0) {
                                         state = DETENT;
                                         rotary->_position--;
                                }
                          }

                          last = value;
                  }
        }
}

This source code is available at github.

When run in a hard loop the monitor code will consistently track the knobs rotation. I spun the knob as quickly as I could and was unable to make the program miscount detents (clicks). Later when I ran the code in a thread I did see some missed detents.

Finally, you can plug this encoder into a breadboard but it doesn’t go in easily and is prone to pop out.

Advertisements
Categories: BeagleBone, C++ Tags: , ,
  1. April 12, 2012 at 1:52 pm

    Hi. Just want to mention that linux has built-in support for rotary encoders connected via GPIO already. Here is the documentation:

    http://www.kernel.org/doc/Documentation/input/rotary-encoder.txt

    Best thing of it: it is interrupt/event-driven, so the driver will only eat cpu-cycles if the user rotates it..

    • April 12, 2012 at 8:36 pm

      Thanks for the info. I think that any practical use of an input or output device would have to go through a kernel driver such as this. While you can wrap similar functionality into user mode libraries the overhead associated with that is a killer. At some point soon I plan to get into some kernel examples. This mod would be a good one to try out.

    • Difool
      August 20, 2013 at 12:07 pm

      Hi Nils.
      Thanks for this info. Do you have any usage examples of this driver ?

  2. grrfx
    April 12, 2012 at 3:18 pm

    The TI3359 SoC has the quadrature encoding functionality in hardware. It’s described in section 15.4 “Enhanced Quadrature Encoder Pulse (eQEP) Module” of the Tech Reference Manual.

    Has anyone figured out how to use it? I’ll have a go next week.

    • Bali
      January 17, 2013 at 3:50 pm

      Did you figure out the eQEP interface on the beaglebone?

      • January 30, 2013 at 6:51 am

        Yes, I had it working very well. We tested it at up to 2Mhz and it didn’t skip a beat. Unfortunately I’ve no spare time to blog about it at the moment.

        Read the TRM section mentioned above, the details there were sufficient.

  3. edwardschien
    June 17, 2013 at 7:02 am

    grrfx :
    Yes, I had it working very well. We tested it at up to 2Mhz and it didn’t skip a beat. Unfortunately I’ve no spare time to blog about it at the moment.
    Read the TRM section mentioned above, the details there were sufficient.

    Hi grrfx,

    I also use TI3359 eQEP Module, to decode quadrature encoding pluse. After so many tries, I still can’t get it work.

    Can you figured out how to use it?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: