Tuesday, September 25, 2018

Shutdown Button with Device Tree Overlays

I want to create shutdown button for raspberry pi.
I found gpio-shutdown parameter for enable Device Tree Overlays on new firmware.

Name:   gpio-shutdown
Info:   Initiates a shutdown when GPIO pin changes. The given GPIO pin
        is configured as an input key that generates KEY_POWER events.
        This event is handled by systemd-logind by initiating a
        shutdown. Systemd versions older than 225 need an udev rule
        enable listening to the input device:

                ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"

        This overlay only handles shutdown. After shutdown, the system
        can be powered up again by driving GPIO3 low. The default
        configuration uses GPIO3 with a pullup, so if you connect a
        button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
        you get a shutdown and power-up button.
Load:   dtoverlay=gpio-shutdown,<param>=<val>
Params: gpio_pin                GPIO pin to trigger on (default 3)

        active_low              When this is 1 (active low), a falling
                                edge generates a key down event and a
                                rising edge generates a key up event.
                                When this is 0 (active high), this is
                                reversed. The default is 1 (active low).

        gpio_pull               Desired pull-up/down state (off, down, up)
                                Default is "up".

                                Note that the default pin (GPIO3) has an
                                external pullup.
Reference: https://github.com/raspberrypi/firmware/tree/master/boot/overlays


Step to create shutdown switch

  • Connect switch to GPIO3.





  • Add config device tree overlays on /boot/config.txt.


dtoverlay=gpio-shutdown,gpio_pin=3,active_low=1,gpio_pull=up


  •  Create file /etc/udev/rules.d/99-gpio-power.rules


ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"


  • And restart raspberry pi.


After reboot success. I try to push switch.
It works ;). My raspberry pi is shutdown.

Hope to help you.

*** I2C must be deactivated if you use pin 3. (Thank you @Irmoreno007)

2 comments: