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

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:

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.

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

 RPG A B C 1 2 mbed p21 p22 GND p23 GND

### 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
}
}

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

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

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.

Sangean's WiFi radio uses only a single RPG for user input

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.

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.

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

05 Feb 2018

I found another RPG at Digikey that also seems to fit OK in a breadboard:

20 Oct 2022 This post is awaiting moderation

Question, I recently purchased the Sparkfun Qwiic Twist RPG. In the notes, it says the code will work with a 2-channel RPG including the push button. Is it intuitive to reason that the two channels are the SDA and SCL on the breakout board? The INT port is the push button? Any help would be really appreciated as I want to use this specific RPG for my college project. Thank you.