encoder2pi

USING ROTARY ENCODERS ON THE PI

Rotary encoders are an inexpensive input device, and with the rotary-encoder kernel module included in Raspbian they are easy to use!

HARDWARE

I've designed a small circuit board so that the encoder can be plugged directly into the Pi. Order the circuit board from OSH Park here. You can also use your PCB vendor of choice using the source files in this project's files section.

Order the rotary encoder from AliExpress or your favorite supplier. It should be under a dollar. The search term to use is "rotary encoder ec11". Pick the type of shaft you want. You might want to order a matching knob too.

Order two 2 pin single row 2.54mm female header connectors. AliExpress has them for around a dollar for ten pieces, or you can get them in smaller quantities from DigiKey for a bit more.

To make soldering easier plug the connectors into something with a two row header such as an old CD/floppy/hard drive. (You can use your Pi instead, but there's a chance it will be damaged, especially if you're using an un-grounded soldering iron.) The connectors should be spaced so that there are three rows of unused pins between them. Like this:

Put the circuit board on the connectors, with the enc2pi writing writing facing down.Solder the two connectors into the board. It should look like this when you're done:

Remove the assembly from whatever you plugged it into and solder the encoder to the board. It will only go in one way. It should look like this when you're done:

Plug the encoder assembly into the Pi. The connector that is closest to the three pin side of the encoder goes on GPIO pins 39 and 40, the last two. Like this:

SOFTWARE

Put the following lines into /boot/config.txt and reboot:

    dtoverlay=rotary-encoder,pin_a=6,pin_b=21,steps=20

    dtoverlay=gpio-key,gpio=12,keycode=30

For testing you'll need to install the evtest program and run it using the following commands:

    apt-get install evtest

    evtest

It will display the various event devices and ask you to choose one. Find the encoder device and enter it's number. You only need to type the number such as 0, 1, etc. It will probably be 3 if you have a keyboard and mouse attached. Then press enter.

Turn the knob and you should see events displayed on screen. Turn it the other way and watch what happens. Note that the values only range from 0 to 20. This is set in the line you entered into /boot/config.txt, and is adjustable. You have to reboot for the changes to take effect though.

Press ctrl-c to exit evtest. Then re-run it and select the button device, probably 4. Push the knob down and watch what happens. Note that there are separate key up and key down events. The key event that is sent is entered into /boot/config.txt, and is adjustable. In this case it is 12, a letter "a". You have to reboot for the changes to take effect though.

Exit evtest by typing ctrl-c. Press the knob again and note that an "a" is typed on the screen. The knob functions no differently than the "a" key on your keyboard, and can be monitored by any program that can read keyboard events.

The knob can also work more like a mouse by using the following line in /boot/config.txt instead of the one used previously:

    dtoverlay=rotary-encoder,pin_a=6,pin_b=21,relative_axis

Make this change and reboot the Pi. After reboot, turn the knob while you're displaying the graphical desktop. Notice how the mouse moves left and right.

You can also specify that it will move up and down instead. See if you can figure out how!