Balena Sound on Raspberry Pi4b


I have just set up my rapberry Pi 4b and to get the audio output to work I have used an earlier version, [balenaOS 3.2.7+rev1]. I tried various suggestions but none worked.

Everything works now but I wish to use an off button to shut down the device.

What I need to know is how to implement this code

git clone GitHub - Howchoo/pi-power-button: Scripts useful for the Pi power button


I cannot SSH into the device with Putty and the Host terminal shows root@balena: ~#

Any clues would be most grateful.



First things first: connecting to your balena device.
Usually you do this through the balena-cli.
You can install it with npm install -g balena-cli, after which you can run balena login --web to authenticate and balena ssh <device uuid> <service> to connect to a specific container on a specific deivce.

Second: how do you install it?
After logging in to your service container, you can simply grab the git repository and run the script.
You may have to run chmod +x ./pi-power-button/script/install first to make it executable.

Note that this will install the listen/shutdown script to be run on /etc/init.d style init, this is not compatible with systemd. I’m not 100% sure which init system you get in your container.
This part of the documentation suggest you may have to install it yourself.

Just out of curiousity, is there a reason you are looking at this approach, instead of the devicetree overlay?
See for example here.

The gpio-shutdown overlay can be used to issue the shutdown command when a GPIO input is triggered.
The gpio-poweroff overlay can be used to set a GPIO output when the device is ready to be powered off.

This requires no extra installation steps, just a modification to the configuration, which can be done through the balenaCloud dashboard or by editing /boot/config.txt before the first boot.
more details.

Thanks for the detailed instructions.

The only reason to try it this way is because I have re-used an existing device which had a switch on it.


I actually meant that last question as in “why try it with this script, instead of devicetree overlays?”.
Strictly the software configuration bit.

It could’ve been you wanted to do more than “just shutdown”, in which case a custom script is probably a better approach.

Either way, hope you get the behavior you’re looking for :slight_smile:
Let us know if you need more help.

1 Like


I’ve used dtoverlay=gpio-shutdown,gpio_pin=3 but it doesn’t work.

My switch is connected between GPIO3 and GND (pin 5 and 6 on the 40-pin header).


You have to look at the complete configuration.
GPIO3 is also an I2C pin (SCL).
If the I2C is enabled, it will override what the GPIO configuration is trying to do.
I2C is enabled with something like dtparam=arm_i2c=on; removing that entry should solve it.

You can use this site to get a nice overview of the GPIOs and their alternate functions.