For some of the Radiodan applications Richard, Anton and I discussed on thursday at the Bristol Hackspace, we need a rotary encoder. For example, this ‘Wrong (W)radio: Time Travel Radio can play audio from the last few days, navigating time by turning the right hand knob:
I’ve not done much work with the Raspi GPIO, but Richard found an Adafruit example, and then I found this one, that looked simpler on the face of it – except that my C isn’t exactly great, and doesn’t in fact exist. Anyway it had a nice simple example of how to wire a rotary encoder to the Raspi (plus an explanation of the other two legs on my rotary encoder – they’re for the built-in button).
After a bit of a struggle, I got it to work – the library needs wiringPi
git clone git://git.drogon.net/wiringPi git pull origin cd wiringPi git pull origin ./build
then
git clone https://github.com/astine/rotaryencoder cd rotaryencoder nano test.c
contents of test.c:
#include "stdio.h" #include "rotaryencoder.h" int main() { printf("Hello!\n"); wiringPiSetup () ; /*using pins 23/24*/ struct encoder *encoder = setupencoder(4,5); long value; while (1) { updateEncoders(); long l = encoder->value; if(l!=value) { printf("value: %d\n", (void *)l); value = l; } } return(0); }
Save and compile it:
gcc -lwiringPi program.c rotaryencoder.c -o test
Run it (GPIO stuff needs to be run as root): I was turning the rotary encoder anti-clockwise
sudo ./test Hello! value: -1 value: -2 value: -3 value: -4 value: -5 value: -6 value: -7 value: -8 value: -9 value: -10 value: -11 value: -12 value: -13 value: -14 value: -15
The wiring’s like this: