Rotary Pulse Generator Library

Rotary Pulse Generators (RPGs), also known as rotary encoders, are a simple easy to use input device similar to that of a potentiometer (pot) with a few subtle differences. First of all an RPG does not have a lower or upper mechanical bound, where as with a pot there is a clearly defined min and max to how far you can turn it. RPGs also commonly have a push button beneath the knob allowing for another user input without any additional space being used. While slightly more expensive than a pot, RPGs are much more versatile and still relatively inexpensive.

The RPG used to develop this Library is available from Sparkfun

https://dlnmh9ip6v2uc.cloudfront.net/images/products/9/1/1/7/09117-03-L.jpg

This is a picture of a simple RPG.

Explanation

RPGs work by generating two quadrature square wave signals. Quadrature means that the two signals are 90 degrees out of phase. An example of this is shown below: http://upload.wikimedia.org/wikipedia/en/thumb/6/68/Quadrature_Diagram.svg/500px-Quadrature_Diagram.svg.png

The most common use of RPGs is to determine the direction of rotation, either clockwise, counter-clockwise, or no change. This is done by comparing two instances of the two signals to each other to determine the direction. If the two instances equal to each other than there has been no rotation. However if there has been some change then a more detailed comparison must be made.

The digital reads of each channel result in these series of inputs for clockwise and counter-clockwire rotation:

Clockwise

PhaseAB
100
201
311
410

Counter-clockwise

PhaseAB
110
211
301
400

The easiest way to determine the direction of rotation is to compare the older input from channel A to the newer input from channel B. If they are different the RPG is rotating clockwise if they equal each other it is rotating counter-clockwise.

API

Import library

Public Member Functions

  RPG (PinName pA, PinName pB, PinName pPB)
  ============================================================================= Rotary Pulse Generator class (Version 0.0.1) ============================================================================= Copyright (c) 2012 Christopher Anderson
  ~RPG ()
  Destructor.
int  dir ()
  Determines direction of rotation returns: 1 for clockwise -1 for counter-clockwise 0 for no rotation.
bool  pb ()
  reads and debounces push button returns bool result

Demo

Pin Connections

RPGABC12
mbedp21p22GNDp23GND

Code

main.cpp

#include "mbed.h"
#include "RPG.h"
#include "TextLCD.h"
 
RPG rpg1(p21,p22,p23); //Set up RPG
TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7


int count = 0;
int dirt = 0;
 
int main()
{
    //lcd.printf("Hello");
    while(1)
    {
        dirt = rpg1.dir(); //Get Dir
        count = count + dirt; //Ad Dir to count
        if (rpg1.pb())
        {
            count = 0;  //Reset count if PB pressed
        }
        lcd.cls();
        lcd.printf("count: %i  ", count); //Print out Count
    }
}

Video Demo

Notes

Does not quite fit in the bread board a simple breakout board would fix this problem. Should work with any two channel RPG with a push button. This includes the lighted RPGs sold by Sparkfun.


Report

2 comments on Rotary Pulse Generator Library:

19 Oct 2012

RPGs are great for LCD menu selection. RPGs have been used for years on electronic test instruments such as the Tektronix Logic Analyzer seem below.

/media/uploads/4180_1/tla6400-logic-analyzer.gif
Tektronix TLA6400 Logic Analyzer with an RPG knob

More recently they are even showing up in household consumer items. The Sangean WiFi Internet Radio seen below uses an RPG for all user input. It navigates through LCD menus by rotating the RPG and the pushbutton selects a menu item.

/media/uploads/4180_1/irad.jpg
Sangean's WiFi radio uses only a single RPG for user input

Next
The new Nest home thermostat in another such example.

In the case of the Nest, the outer ring turns the RPG for menus and pushing the display selects an item. A former iPod designer from Apple setup the user interface for the nest.

Other Sparkfun RPGs

The large metal tabs on the sides need to be cutoff on the basic RPG from Sparkfun (seen in photos earlier) and it will then fit in a breadboard as shown. It is still a bit loose on most breadboards.

Sparkfun also has another RPG with an LED in the knob as seen below and a breakout board is available for this one. A breakout board is needed as the pin spacing is not breadboard friendly.

RPGlighted
Sparkfun's RPG with an LED

RPGbreakout
Sparkfun's Breakout board for the Lighted RPG

A slight twist of the pins is needed to fit the breakout board on a breadboard and the RPG without LEDs will not fit on this breakout. It does not come with a knob, but knobs that fit the RPGs are also available from Sparkfun. Clear knobs would typically be used on the RPGs with LEDs.

RPGRGB

There is even a newer Sparkfun RPG with an RGB LED, but there are not breakout boards for it yet and the pin spacing is not breadboard friendly.

01 Mar 2014

http://mbed.org/users/pyeh9/notebook/lcd-menu/ is a project that adds an LCD menu setup using this RPG driver.

Please log in to post comments.