Raspberry Pi 3a error using external rotary encoder with VideoLooper

Hi all,
Im currently working on a project to automatically play video using video looper on a Raspberry Pi 3a with an external audio rotary encoder (ec11) module.

I found a script that adjusts the volume using the encoder which works when run manually from the desktop. But i want it to work when video looper starts. Basically if someone wanted to adjust the volume while video looper was playing they could just use the rotary encoder.

I’ve added a boot script to the conf.d folder (/etc/supervisor/conf.d) which mentioned the python3 script to start the encoder script (same one i used manually in desktop mode). But each time i start the PI, video looper runs find but i cant get the encoder to work.

Checking the log file i can see that this error keeps appearing "ModuleNotFoundError: No module named ‘alsaaudio’

Does anyone have any ideas on how to resolve this issue?

3 Likes

Hi @Scott253186, Welcome to the forums!!!

Could you provide the script you are using as well as how you have the rotary encoder module hooked up?

I have a suspicion there may be something that happens during boot that isn’t happening before your script runs causing it to not detect the module.

2 Likes

Thanks for your response. Yeah im thinking the same thing.

The script im using is shown below. I didn’t create it but i did modify it a bit to suit my needs.

The rotary encoder im using is the DFROBOT EC11 Rotary Encoder.

#***************************************************************************************

Code for Controlling Pi Volume Using Rotary Encoder

Original Code: https://bit.ly/2OcaQGq

Re-Written by Sid for Sid’s E Classroom

https://www.youtube.com/c/SidsEClassroom

All text above must be included in any redistribution.

If you find this useful and want to make a donation → PayPal.Me

***************************************************************************************

from RPi import GPIO
from time import sleep
import alsaaudio

Change the following pins based on your application or HAT in use

encoder_clk = 4
encoder_data = 17
encoder_button = 27

GPIO.setmode(GPIO.BCM)
GPIO.setup(encoder_clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(encoder_data, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(encoder_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

m = alsaaudio.Mixer(‘PulseAudio’)

m = alsaaudio.Mixer(‘Master’,0)
for mixername in alsaaudio.mixers():
if str(mixername) == “Master” or str(mixername) == “PCM”:
m = alsaaudio.Mixer(mixername)

Set desired minimum and maximum values

min = 0
max = 100

Set the volume change step size

volume_step_size=5

is_Muted = m.getmute()[0]
volume = m.getvolume()[0]

if is_Muted == 0:
is_Muted=False
else:
is_Muted=True
print(“Mute State: " + str(is_Muted))
print(“Volume: " + str(volume))
print(””)
clkLastState = GPIO.input(encoder_clk)
btnLastState = GPIO.input(encoder_button)

try:
while True:
btnPushed = GPIO.input(encoder_button)
if ((not btnLastState) and btnPushed):
if is_Muted:
is_Muted = False
m.setmute(0)
print(“Mute State: " + str(is_Muted))
print(“Volume: " + str(int(volume)))
print(””)
else:
is_Muted = True
m.setmute(1)
print(“Mute State: " + str(is_Muted))
print(“Volume: " + str(int(volume)))
print(””)
sleep(0.05)
else:
clkState = GPIO.input(encoder_clk)
dtState = GPIO.input(encoder_data)
if clkState != clkLastState:
if dtState != clkState:
volume += volume_step_size/2
if volume > max:
volume = max
else:
volume -= volume_step_size/2
if volume < min:
volume = min
if clkState == 1:
print(“Mute State: " + str(is_Muted))
print(“Volume: " + str(int(volume)))
print(””)
m.setvolume(int(volume))
clkLastState = clkState
btnLastState = btnPushed
finally:
GPIO.cleanup()

Hey @Scott253186

Quick sanity check: is alsaaudio installed?

#assuming you installed with pip
pip list | grep alsaaudio
1 Like

Hi @Pixmusix

yes, it is. pyalsaaudion 0.8.4 installed.

1 Like

@Scott253186 you might find that it is more appropriate to run your script at boot by setting up a cron job instead.
I’m not exactly sure what the behaviour of running scripts at startup from /etc/supervisor/conf.d is, but using cron seems to be safest in general - especially when it seems you’re executing the same command but a module is not found.

The good news is it should be easy enough to try:

edit the cron table with
crontab -e
and append a line to run your script at reboot

@reboot python3 /[path to my script]/my_script.py &

& will run the script in the background.

(of course, remove your existing conf.d implementation first.)

1 Like