On my Libre Sweet Potato AML-S905X-CC-V2, I am trying to set up a rotary encoder volume control and shown in this github:
README.md
# Using a rotary encoder as a volume control
On my RetroPie machine I wanted a hardware volume knob — the games I play use a handful of emulators, and there's no unified software interface for controlling the volume. The speakers I got for my cabinet are great, but don't have their own hardware volume knob. So with a bunch of googling and trial and error, I figured out what I need to pull this off: a rotary encoder and a daemon that listens for the signals it sends.
## Rotary encoder
A rotary encoder is like the standard potentiometer (i.e., analog volume knob) we all know, except (a) you can keep turning it in either direction for as long as you want, and thus (b) it talks to the RPi differently than a potentiometer would.
I picked up [this one](https://www.adafruit.com/products/377) from Adafruit, but there are plenty others available. This rotary encoder also lets you push the knob in and treats that like a button press, so I figured that would be useful for toggling mute on and off.
This file has been truncated. show original
monitor-volume
#!/usr/bin/env python3
"""
The daemon responsible for changing the volume in response to a turn or press
of the volume knob.
The volume knob is a rotary encoder. It turns infinitely in either direction.
Turning it to the right will increase the volume; turning it to the left will
decrease the volume. The knob can also be pressed like a button in order to
turn muting on or off.
This file has been truncated. show original
monitor-volume.service
[Unit]
Description=Volume knob monitor
[Service]
User=pi
Group=pi
ExecStart=/home/pi/bin/monitor-volume
[Install]
WantedBy=multi-user.target
After attempting to run the script, I get the following:
" cumulus@CUMULUS:~ $ python ~/bin/monitor-volume
Traceback (most recent call last):
File “/home/cumulus/bin/monitor-volume”, line 22, in
from RPi import GPIO
File “/usr/lib/python3/dist-packages/RPi/GPIO/init .py”, line 23, in
from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!"
Apparently it works only for an actual Raspberry Pi board. Any idea how to make it work for a Libre? Thanks in advance.
Most rotary encoders can work with 2 GPIOs. Depending on which GPIO is triggered first on the rising edge, it provides the direction of change. This is simple to do with libgpiod so you can probably find a project on GitHub or have an AI tool generate the C code for you. Just assign the proper GPIO chip and line via libretech-wiring-tool tool for mappings (lgpio).