Published using Google Docs
Rotary Encoder via GPIO (EN)
Updated automatically every 5 minutes

Rotary Encoder via GPIO (EN)

(This brief guide and python script is adapted from savetheclocktower’s project found on GitHub.)

Purpose

This script is meant for whom that want a physical hardware volume knob on a Recalbox project, such as arcade machines. This script is useful for arcade cabinets with speakers that don't have their own hardware volume knob, or would have difficulty moving the speaker control knob near the user.

This script utilizes a standard 5 pin rotary encoder and has been tested on the encoder from Adafruit. 5 wires are required for this rotary encoder: three for the knob part (A, B, and ground), and two for the button part (common and ground). Here’s a reference for the Raspberry pi’s GPIO pins.

Description

BCM #

Board #

knob A

GPIO 26

37

knob B

GPIO 19

35

knob ground

ground pin below GPIO 26

39

button common

GPIO 13

33

button ground

ground pin opposite GPIO 13

34

You can use whichever pins you want; just update the volume-monitor.sh script if you change them. If you lack the push button in your rotary encoder then leave pins unoccupied.

Volume daemon

The script below works as the following: it listens on the specified pins, and when the knob is turned one way or another, it uses the states of the A and B pins to figure out whether the knob was turned to the left or to the right. That way it knows whether to increase or decrease the system volume in response, which it does with the command-line program amixer.

To install the script into your recalbox:

  1. First you need to connect to your recalbox via ssh.
  2. Remount partition on read-write:

mount -o remount, rw /boot

  1. Create/Edit your volume-monitor.py script in /recalbox/scripts via nano:

                                        nano /recalbox/scripts/volume-monitor.py

  1. Copy the script into the file by copy and pasting the following, then using ctrl+x to save the file:

                This is a note for Substring or others. Please make one of those nice script boxes here with the contents of the monitor-volume script from savetheclocktower’s GitHub. BUT!!!!!! Change line one from #!/usr/bin/env python3 to: #!/usr/bin/env python2, and change line #25 from from queue import Queue to from multiprocessing import Queue

        

  1. Mark script as an executable file:

                                        chmod +x /recalbox/scripts/volume-monitor.py

  1.  To make the volume-monitor script start up with your system, do the following:

                                        touch ~/custom.sh && chmod u+x ~/custom.sh

  1. Open the custom.sh script in the nano editor:

                                        nano ~/custom.sh  

  1. Finally copy and paste the following into the custom.sh file and save with ctrl+x:

                                        python /recalbox/scripts/volume-monitor.py

  1. Restart your Recalbox using the reboot command:

reboot

This is a note for Substring or others. I tried to make this guide to not use the change directory (ie: cd command), or any other unnecessary commands, so it's as simple as “copy, paste, enter” all the way through. Please test on your recalbox to make sure this guide works as I have written it. If not, please make the modifications needed. Also, please convert bolded sections and other, “it’s meant to be terminal commands” to the write formating in the mini how-to guide. Thank you.