Turning the Pi on and off is a bit of a hassle. So let's add a push button to turn it off and on.

Having a simple switch to turn off the Pi is first and foremost just very handy. It's a pain to shut down through ssh, and removing the power can easily lead to a corrupted SD card.

And I generally just leave the system on for long periods. By cycling the power more often, dangling resource programming errors should be identified quicker. Fixing the problems a bit closer to the introduction of the problem. So the camera software will start and stop gracefully.

On off toggle button

Adding a power button is dead simple. This guide lays the process out.

Simply add a button to pins 5 and 6:

Button connected to pin 5 and 6 and LED connected to pins 8 and 14

Note, if you are using pin 5 for something else, this may help you.

Shorting pins 5 and 6 when shutdown should boot the Pi.

To shut down, we need a script watching for this event. Install the following script:

git clone https://github.com/Howchoo/pi-power-button.git
./pi-power-button/script/install

After installing this script the Pi indeed shuts down when pressed. However, the fan turns on and does not turn off even when the Pi is "shutdown".

Fan shim lift off

To use the Pi 4 you really need and active cooling solution. I'm using the Pimoroni Fan SHIM. This is working splendid, except it starts blasting air when shutdown and it does not turn off. The fan goes max speed when either shutting down with the power button or from a sudo shutdown now command.

I haven't found a way to turn off the fan when the shutdown sequence has completed. So let's try changing some settings so it does not go to max speed when the shutdown command is issued in the first place.

TLDR: No solution found, best fix right now is sudo shutdown now followed by a power cable disconnect...

Problem identification

For installation, I followed "Installing the Fan SHIM software" section of the guide.

My assumption is that stopping this script makes the fan go full speed. Let's test that assumption. First let's find the PID for the script: ps aux | grep -i automatic.py. After killing the process (sudo kill <PID>) the fan indeed spun up. Enabling the daemon made the fan go back to normal: sudo systemctl start pimoroni-fanshim.service.

Stopping fan programmatically

The install-service.sh script that I used uses automatic.py as a daemon src. One possible solution would be to let this script shut down the fan cleanly on exit.

Before diving into trapping shutdown signals let's see if shutting down the fan through python will fix the issue.

I killed the running automatic.py program like before. This turned the fan on.

Now using the python REPL, turn of the fan:

from fanshim import FanShim
fanshim = FanShim()
fanshim.set_fan(False)

The fan is turned off at this point. Now the hope is that the fan will stay off when the board is shut down. So let's try: sudo shutdown now and... No joy.

The fan turns on even when shut down. So this seems to be a hardware issue.

Stopping fan by editing /boot/config.txt

Somebody else opened an issue about this problem. One possible solution might be to add the line dtoverlay=gpio-poweroff,gpiopin=18 to /boot/config.txt.

But just like the other person, this caused boot issues. Meaning this hardware configuration solution also does not work.

Smooth shutdown: no joy

So, right now we can't stop the fan when putting the system in a halt state. The only workaround I know is shutting down gracefully (sudo shutdown now) and disconnecting the power after shutdown.

It's a messy solution, but will do for now.

Power on not working out of the box (Pi 4)

The Pi should boot when shorting pins 5 and 6. Mine did not do that out of the box.

I probably got one of the early batches of Raspberry Pi 4s that did not have the wake up feature enabled.

A EEPROM update should fix that, before following the steps to update the WAKE_ON_GPIO value 0 (off):

pi@picam1:~ $ vcgencmd bootloader_config
BOOT_UART=0
WAKE_ON_GPIO=0
pi@picam1:~ $ vcgencmd bootloader_version
May 10 2019 19:40:36
version d2402c53cdeb0f072ff05d52987b1b6b6d474691 (release)
timestamp 0

I don't want the EEPROM to update on it's own, potentially breaking something unexpectedly. So let's first disable auto upgrade:

sudo systemctl mask rpi-eeprom-update

Next lets install the EEPROM update software:

sudo apt update
sudo apt full-upgrade
sudo apt install rpi-eeprom

Finally, let's do the actual bootloader install:

pi@picam1:~ $ sudo rpi-eeprom-update -a
*** INSTALLING EEPROM UPDATES ***
BOOTLOADER: update required
CURRENT: Fri 10 May 2019 06:40:36 PM UTC (1557513636)
 LATEST: Tue 10 Sep 2019 10:41:50 AM UTC (1568112110)
VL805: update required
CURRENT: 00013701
 LATEST: 000137ab
EEPROM updates pending. Please reboot to apply the update.

Result:

pi@picam1:~ $ vcgencmd bootloader_config
BOOT_UART=0
WAKE_ON_GPIO=1
POWER_OFF_ON_HALT=0
FREEZE_VERSION=0

... lot of empty lines ...

pi@picam1:~ $ vcgencmd bootloader_version
Sep 10 2019 10:41:50
version f626c772b15ba1b7e0532a8d50a761b3ccbdf3bb (release)
timestamp 1568112110

So now the WAKE_ON_GPIO is 1 (on). And indeed by shorting pins 5 and 6 with the push button the Pi boots!

System on LED

The power LED is a bit hard to see in the current Pi enclosure. Luckily it is again dead simple to add an external power indicating LED.

Just add the cathode (short leg) to ground pin 14 and the anode (long leg) to pin 8.

The LED is connected to the serial transmission pin, so it may flicker a lot depending on your application. But for now, in this project it should simply be continuously lit after boot and turn off when the power can be disconnected.