Over a million developers have joined DZone.

Making Your Own RPi Power Button

DZone's Guide to

Making Your Own RPi Power Button

While not a true power button, you can hook up a button to your Raspberry Pi that puts your device to sleep so you can save energy while not losing data.

· IoT Zone
Free Resource

Download Red Hat’s blueprint for building an open IoT platform—open source from cloud to gateways to devices.

This post describes an easy way to add a power button to a Raspberry Pi that:

  • Only needs a button and wires, no other components.
  • Allows for both a graceful shutdown and start up.
  • Only needs a modification of config files and does not need a dedicated daemon to read GPIO pins.

There are two caveats:

  • This shuts down in the same way as shutdown -h now or halt does. It does not completely cut the power (like some hardware add-ons do).
  • To allow powerup, the I²C SCL pin (aka GPIO3) must be used, conflicting with externally added I²C devices.

If you want to get started right away, skip to the "Setting this up" section below.

Powering Down Your Pi

To gracefully power down a Raspberry Pi (or any Linux system), it has to be shut down before the cutting power. On the RPi, which has no power button, this means using SSH to log in and running a shutdown command. Or, as everybody usually does, cutting the power and accepting the risk of data corruption.

Googling around shows a ton of solutions to this problem where a button is connected to a GPIO pin. The GPIO pin is monitored and, when it changes, a shutdown command is given.

However, all of these seem to involve a custom daemon, usually written in Python, to monitor the GPIO pin. A separate daemon just for handling the power button does not sound great, and if it is not apt-get installable, it seems too fragile for my tastes. Ideally, this should be handled by standard system components only.

Another thing is that a lot of these tutorials recommend wiring a pullup or pulldown resistor along with the button, though the Raspberry Pi has built-in pullups and pulldowns on all of its I/O pins that can just as easily be used.

Shutdown Using systemd-logind

Having a shutdown button connected to a GPIO pin is probably not so uncommon, so I Googled some more. I came across the 70-power-switch.rules udev file from systemd-logind, which adds the "power-switch" tag to some kernel event sources representing dedicated power buttons. Since v225 does this for selected GPIO sources as well (commit), a bit of digging shows that:

  • The udev rules add the "power-switch" tag to selected devices. Udev does not use these tags itself, but other programs can.
  • The "power-switch" udev tag causes systemd-logind to open the event source as a possible source of power events.
  • When a KEY_POWER keypress is received, systemd-logind initiates a shutdown. It also handles some suspend-related keys.
  • Since last week, 70-power-switch.rules tags all event sources as "power-switch" (commit) and lets systemd-logind figure out which could potentially fire KEY_POWER events (commit) and monitor only those (though this change does not really change things for this use case).

Linux gpio-keys Driver

So, how do we let a GPIO pin generate a KEY_POWER event? The first logind commit linked above has an example device tree snippet that sets up the kernel gpio-keys driver, which does exactly that: Generate key events based on GPIO changes.

Normally, the devicetree is fixed when compiling the kernel, but using a devicetree overlay (see the RPi device tree docs) we can insert extra configuration at boot time. I found an example that ties multiple buttons to GPIO pins and configures them as a tiny keyboard. Then I found an example in the gpio-button package that does the same with a single button and also shows how to configure a pulldown on the pin.

I've created a devicetree overlay based on that example that sets up the gpio-keys driver with a single GPIO to trigger a KEY_POWER event. The overlay is just a text file, and I added elaborate comments, so look inside if you are curious. Instructions on installing this overlay are at the end of this post.

Waking Up

With the above, you get a shutdown button: Press it to initiate a clean shutdown procedure, after which the Raspberry Pi will turn off (note that it does not completely cut power, that would require additional hardware. Instead, the system goes into a very deep sleep configuration while still drawing some current).

After shutdown, starting the system back up means removing and reinserting the USB cable to momentarily cut power. This begs the question: Can you not start the system again through one of the GPIO pins?

I found one post that pointed out the RUN pin, present on RPi 2 and up, which can be shorted to GND to reset the CPU (without a clean shutdown!), but can also power the system up. However, having two buttons, along with the risk of accidentally pressing reset instead of shutdown did not seem appealing to me.

After more digging, I found a forum post saying that shorting GPIO3 to GND wakes up the RPi after shutdown. It also says GPIO1 should work, but I do not think any RPi makes that pin available, so I haven't tested that. Official docs on this feature seem to be lacking, I found one RPi makes that pin available, so I haven't tested that. Official docs on this feature seem to be lacking, I found one note on the elinux.org wiki saying bootcode.bin (version 12/04/2012 and later) puts the system in sleep mode and starts the system when GPIO3 goes low.

Since GPIO3 is also a normal IO pin, this is perfect: By configuring the shutdown handling on GPIO3, the same button can be used to shutdown and wake up. Note that GPIO3 is also an I²C pin, so if you need to also connect I²C-devices, another pin must be used for shutdown and you cannot use the wakeup feature. Also note that GPIO3 (and GPIO2) have external pullups, at least on the RPi Zero.

Setting This Up

  1. Get and build the DT overlay. If a file /boot/overlays/gpio-shutdown.dtbo is already available on your system, you can skip this step. Download the devicetree overlay file. The easiest is to run wget on the Raspberry Pi itself:
    $ wget http://www.stderr.nl/static/files/Hardware/RaspberryPi/gpio-shutdown-overlay.dts

    Compile the devicetreefile:
    $ dtc -@ -I dts -O dtb -o gpio-shutdown.dtbo gpio-shutdown-overlay.dts

    Ignore any "Warning (unit_address_vs_reg): Node /fragment@0 has a unit name,
    but no reg property
    " messages you might get. Copy the compiled file to /boot/overlays where the loader can find it:
    $ sudo cp gpio-shutdown.dtbo /boot/overlays/
  2. Enable the overlay by adding a line to /boot/config.txt:

    If you need to use a different GPIO, or different settings, see the DTS file for available options and the RPi devicetree docs for setting them.
  3. If running a systemd older than v225 (check with systemd --version), create a file called /etc/udev/rules.d/99-gpio-power.rules containing these lines
    ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", SUBSYSTEMS=="platform", \
        DRIVERS=="gpio-keys", ATTRS{keys}=="116", TAG+="power-switch"
    On systemd v225 and above, this should not be needed, but I have not tested this.
  4. Reboot

Then, if you connect a button to GPIO3 and GND (pin 5 and 6 on the 40-pin header), you can let your Raspberry shutdown and startup using this button.

All this was tested on an RPi Zero W, RPi B, and RPi B+.

This overlay was merged into the official kernel repository, so in the future, step 1 above should no longer be needed. When the stretch is released for the RPi with a newer systemd version, that would only leave a simple modification to /boot/config.txt to set this up!

Build an open IoT platform with Red Hat—keep it flexible with open source software.

raspberry pi ,iot ,power ,gpio ,tutorial

Published at DZone with permission of Matthijs Kooijman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}