Use a rotary encoder and button on Raspberry Pi with device overlays

For my HappyPlayer project I needed to control the audio volume using a rotary encoder and GPIOs. I used device tree overlays for this.

The encoder used is a standard type with an additional momentary switch when you press it. It needs 4 pins:

  • Common GND -> Pi GND
  • Momentary switch -> GPIO 6
  • Rotary switch A -> GPIO 5
  • Rotary switch B -> GPIO 13

To support the devices you could write a program in C or Python that polls the GPIOs or waits for an interrupt when a GPIO changes, but there’s an easier way. You can enable a device overlay in the linux kernel for them. The pins will automatically be pulled up and will issue an input event if their state changes. Add this to your “/boot/config.txt”:

# Overlays for buttons and rotary encoder
# For overlay parameters see:
# For key codes and axes see:
# Rotary axis, REL_HWHEEL
# Rotary push button, KEY_MUTE

and reboot. This will map the rotary encoder and rotary switch to two input devices “rotary@5” and “button@6” which you can test with “evtest”. Select the input device and turn / press the control and you should see events coming in.

You can now handle those events in your own program or write a custom kernel module for it. Here you can find code for a volume controller kernel module using the rotary encoder.

Published by HorstBaerbel

Software developer by trade and interest, but I venture into the electronics- and diy-world from time to time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at
Get started
%d bloggers like this: