This example project will show you how a rotary encoder works and how to make a tactile volume control for Windows.

A binary rotary encoder with Gray code output is a very cleverly made rotating mechanical dual switch. It is a passive component, meaning that it does not need a power supply to work.

This is what a typical rotary encoder looks like on the outside:

An encoder like the one above is only a couple of US dollars and can be found at most electronics dealers or on-line market places and stores where electronics is sold.

Inside the body of the device, two spindles are sliding along a rotating metal plate in such a way that the two switches can only change state (open or close) one at a time. There are no endpoints in either direction, i.e. the knob can be rotated continuously without ever blocking.

Basic working principle
As you can see from the above image, there are three pins coming out of the base. The middle pin is common and the other two are the output from the two internal switches. 

When the knob is rotated in one direction, one of the two switches will close slightly before the other and when the knob is turned in the opposite direction, the other switch will close first.

As the knob is turned, the switches will open and close repeatedly creating a two differently phased pulse trains. Also, the speed at which the switches toggle is proportional to the speed at which the knob is being turned.

To read the encoder, you apply voltage to the middle pin and then analyze the chronology of the output from the other two pins. This means that if we can detect a voltage on the left pin before the right pin, someone is turning the knob clockwise. On the other hand, if the right pin shows voltage first, we know that the knob is rotating counter clockwise.

To use a rotary encoder in an electronic design, we can write a piece of software to analyze the chronology in order to inform the other logic in which direction the knob is currently rotating and at which speed.

Before doing the wiring we must configure the pins that we will be using:

  1. Put the PeekyPokey power supply in the 5V position
  2. Plug the USB cable into your PC. 
  3. Launch the PeekyPokey Dashboard application
  4. In the profiles tab, make sure gp0 and gp1 are inputs (see image below)
  5. Save the configuration but leave the Dashboard running
  6. Unplug the USB cable

With the USB cable unplugged, first stick the rotary encoder down your breadboard. Since these encoders are meant to be soldered on a PCB, it can be a little tricky. You also might have to use a rubber band, tape or
a piece of string to keep the encoder sitting steadily and keep it from breaking loose when turning the knob.

This is how to wire it up:

PeekyPokey  Encoder Description
+5V Middle pin Common voltage for the switches
gp0 Right pin Output from encoder switch #1
gp1 Left pin Output from encoder switch #2

When done, your setup should look a little something like this:

In the above picture, I've put a plastic cap on the encoder knob to make turning it easier.

Now, plug the USB cable back into your PC and turn to the Dashboard application that you left running before (or launch it again as necessary). Click the analyzer tab and watch closely what happens with gp0 and gp1 as you slowly turn the knob clockwise and then counter clockwise. Also look at the LEDs on the physical board.

If the encoder behaves as expected, you will see something like this in the analyzer window:

In the left rectangle, gp1 is clearly rising before gp0 and in the right rectangle, it's the other way around. The timeline runs from left (earlier) to right (later).

You should be able to verify the working principle as explained earlier and as outlined by the two rectangles respectively. If you can't, check your wiring and possibly swap wires until you get it right.

Writing some code
You will now do a little bit of coding to implement a volume control for your PC. As it turns out, the PeekyPokey API toolkit contains a ready made driver class for rotary encoders so we go a head and use that. Here's what to do next:

  1. Start Visual Studio
  2. Choose to create a new Windows Forms application
  3. Add a reference to the PeekyPokey API .DLL file
  4. Double click the Program.cs file
  5. Remove all existing code
  6. Copy and paste the code snippet below into the code window
  7. Save and run the application
using System;
using PeekyPokey;
using System.Threading;

namespace RotaryVolume
    class Program
        static void Main(string[] args)
            // create a new instance of the rotary encode driver class
            var rotary = new PeekyPokey.Toolkit.Rotary(Device.Pin.Gpio0, Device.Pin.Gpio1);

            // attach an event handler to sense knob rotation
            rotary.OnRotation += new PeekyPokey.Toolkit.Rotary.Rotation(rotary_OnRotation);

            // main thread waits here for ever...

        // called upon rotation of the knob
        static void rotary_OnRotation(bool upDown)
            // use media keys to adjust Windows volume accordingly
            if (upDown)

As you can see, there's not much code and it should be fairly easy to follow.

To try out your new volume control, start your favorite music player and have it play a song while turning the rotary encoder knob in either direction to adjust the volume. You can also bring up the Windows volume mixer control window and watch the slider move up and down as you turn the knob:

Tip: On some rotary encoders, there's a third switch that you operate by pushing down the knob. You could use such an encoder with a digital input to sense the knob being pushed and then use the corresponding event to mute and unmute the sound of your PC. Similarly, you could add some push buttons to use for play/pause, skip, previous etc.

Experiment, explore and have fun!

Last edited Sep 5, 2013 at 12:13 AM by hanzibal, version 14