How to run a shutdown script to manage GPIO pins

Hi, I am sort of new to Volumio and need a little help with this:

I have Khadas Tone Board DAC connected to my RPI 4B with Volumio via USB. This DAC sounds great but sends a loud pop into the speakers when it is turned ON or turned OFF. I got around the Turn-ON pop when starting the RPI by using external switch triggered by GPIO pin on startup in the userconfig.txt. As the RPI first turns on the USB devices and then the GPIO pins based on userconfig.txt the pop is not there as the amplifier is turned on after the DAC. I am trying to figure out how to do the same in reverse when the shutdown is triggered (either from Volumio by clicking “Power off” or by grounding the PIN using one of the plugins). I simply need to turn off a PIN = the amplifier before the system turns off the USB DAC so that I don’t hear the pop in my speakers.

Can you help, please. Many thanks!

If you have already wired up a relay to a GPIO, have you considered using the AmpSwitch plugin to turn on/off your amplifier only when you have music playing?

That was the first thing I did, it works perfectly fine and I would have used it, except it does not cooperate with Roon endpoint plugin. I understand it reads states of volumio and if the state=“playing” it turns on and when it stops it turns off. But when Roon is playing, it does not recognize it as “playing” and does not turn on… I use Roon very often, so this is a major issue…

Ah, gotcha - not looked into Roon, but does it also have a an API to hook into?

Simplest alternative would be to just use systemd to run your GPIO command just before the usual shutdown sequences.
You might want to readup at systemd-halt.service on some caveats and other alternatives such as /usr/lib/systemd/system-shutdown/my_script.sh

[Unit]
Description=Switch Amplifer Off
DefaultDependencies=no
After=final.target # This will trigger right before normal shutdown/reboot/halt etc sequences 

[Service]
Type=oneshot
ExecStart=/path/to/script_toggle_gpio.sh

[Install]
WantedBy=final.target

Great, thanks!! Will look into it.

I am new to RPI and scripts so I had to learn a lot during today - from reading various posts on the subject - I am still not able to make it work. I did the following:

created the following script:

[Unit]
Description=Switch Amplifer Off
DefaultDependencies=no
Before=shutdown.target poweroff.target reboot.target halt.target

[Service]
ExecStart=/etc/myscripts/script_toggle_gpio.sh
Type=oneshot

[Install]
WantedBy=shutdown.target poweroff.target reboot.target halt.target

and saved it to /usr/lib/systemd/system-shutdown/ and made the script executable although I am not sure it is needed in this case

script_toggle_gpio.sh was saved to /etc/myscripts/ and looks like this:

#!/bin/bash

echo “turning off GPIO 17 - amplifier switch”

if [ ! -e /sys/class/gpio/gpio17 ]; then
echo “17” > /sys/class/gpio/export
fi

echo “out” > /sys/class/gpio/gpio17/direction

echo “0” > /sys/class/gpio/gpio17/value

sleep 1

I made the script executable and tried running it using sudo sh and it works

I guess the first part is somehow broken but cannot figure out what’s wrong. I am new to this so it might be some little detail. I also read about enabling the service (meaning system-shutdown) but don’t really know if is it needed in this case or how to do it.

Also, I am not sure if all this is in danger of being overwritten at the next Volumio Update…

Any help would be greatly appreciated!

Glad your reading up, we all started in similar boats :wink:

Some tips:
– Your service file (the one with [Service] parts) is best called something like amplifier.service or something along those lines, and saved to /etc/systemd/system/amplifier.service

You then need to tell systemd where to find it, and enable it

systemctl daemon-reload
# Check if it has found your new service 
systemctl list-unit-files amplifier.service

# Enable it now
systemctl enable amplifier

By default systemd will run it as root, but you can switch this out using the User= and Group= variables.

Super! Thanks! Will try it.

Thank you!
I followed your advice and it works perfectly! This simplifies so many things in the design of my streaming amplifier.

One last thing - will this survive next volumio update? I have no idea what is changed during an update process…

Great work! :slight_smile:

Hmm, I am not very sure, it should but you never know :wink:

I have a small setup script that I run on each new install, to customise it to my liking. You could create a small script that creates this unit file, and keep it as a backup in case it doesn’t survive an update.