There is risk to corrupt the SD card of Raspberry Pi when it isn’t proper shutdown and I do power down my audio equipment when not in use. The Soft Power Off module, based on a ATtiny85, provides a solution for it.
There are some power down solutions for the Pi available, but none was suitable for my needs:
- One button for power on and off.
- Soft power down, that waits until the OS is also stopped.
- Identification that the Pi is booting (blinking led)
- identification that the Pi is shutting down (blinked led)
- No power use when off, also no standby power.
The idea to use a small controller the ATtiny85 to manage the soft power control process. It controls a relay in combination with a double pole momentary switch.
When the power switch is pressed one pole is used to bypass the relay, before the switch is released the controller is booted and activates the relay to ensure the power stays on.
At the same time the power LED in the switch start blinking and the Raspberry starts booting. After a while the Pi will toggle a GPIO pin to shows it is booted. In response the ATTiny stops blink the led and turns is on.
When you power down by pressing the momentary switch again, the second pole is connected to the soft power down circuit. That delegates the power down request to the Raspberry Pi, and the LED in the switch starts blinking. When the Pi confirms the power down by toggling a GPIO pin to 0, the power circuit inactive the relay power relay.
When the Pi shutdown is initiated from the Pi is self, the same process is used. Only there is no blinking LED to provide feedback.
The process is shown in the following state machine:
Proof of Concept
Proof of concept is first created in with tinkercad , due it’s online a simulation, including with the programming of the tiny with Arduino.
Instead of a Pi a switch and LEDs are to simulate the sensor and actuators. After the successful Tinkercad test a breadboard is used for further testing.
During the testing on testing breadboard it was clear that Arduino bootlader is to slow. It would require that you hold the switch button for several seconds. After you remove the bootloader it boots in a blink, you will not notice it when pressing the button. The downside is that instead of cheap USB board, now you will need an In System Programmer. Fortunately you can transform an other Arduino, like an Uno, into an ISP .
The sketch is directly available in the Arduino IDE under examples with the name ArduinoISP.
Finally we can build the real electronics. It exists out of an relay board with a controller on experimental PCB board next ot it. It’s on the to do list to create PCB to replace the relay and power down PCB, for now it’s good enough.
Raspberry Pi configuration
The solution provided here works of Raspberry with a OS derived from Raspbian Buster Lite 10.
To let the Raspberry Pi place nice with the soft power off module it requires 2 GPIO pins:
- output pin; 1 is booted
- input pin; 1 is request power off
In my system GPIO17 is used for the power state and GPIO22 for the request power off.
Of course we could write a little daemon for it, but the Raspberry hardware overlays already provide functionality for it.
The gpi-shutdown overlay provides a way to shutdown the Pi based on the toggle of an GPIO pin. And the overlay gpio-poweroff provide a way to show the current state with a GPIO pin.
If you add the following to lines to /boot/config.txt, after the next reboot this functionality will be active:
The power down request works fine. Only I don’t like that the power led stops blink direct after the start of the Pi. I would like that it stops blinking when the system is fully booted and ready to play music.
For that we needed to delay the activation of GPIO 17. Just using a cmd line command is attractive, but this gives problems with setting the power down state to 0. That should be as late as possible, preferable after the filesystem is umounted, which isn’t possible if you need to load command.
There is an alternative by loading the overlay ‘manually’. We just need to remove or comment the gpio-poweroff line in the /boot/config.txt and add a systemd service that loads the overlay instead.
Create a file in /home/pi with the name pipowerstate.service with the content:
[Unit] Description=External softpower on/off After=multi-user.target [Service] Type=idle ExecStart=/usr/bin/dtoverlay gpio-poweroff gpiopin=17 active_low [Install] WantedBy=multi-user.target
To create the service:
sudo cp pipowerstate.service /etc/systemd/system sudo systemctl enable pipowerstate sudo systemctl start pipowerstate
Afterward restart your system to see if everything work as expected.
Update: I did build a custom PCB to replace the relay board by power distribution PCB with a solid state relay. See the post AC Power Distribution.