Skip to content
View in the app

A better way to browse. Learn more.

Armbian Community Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Shut down OPi Zero using a push button on GPIO

Featured Replies

Hi there.

I want to use a push button on a GPIO to shutdown O Pi Zero safely.  Though the acpid way runs fine on an O Pi One, I wonder if there is a way to make it work on a O Pi Zero. Please direct me if its documented somewhere already. Thanks

  • Author

Yeah thats fine for me. Just want to implement that for safe shutdown as I would want to disconnect power after that. So is there simple way to implement that??

12 hours ago, Aditya said:

So is there simple way to implement that??

Sure !

Take this overlay source and save it as "gpio-keys.dts" :

 

 

 


/dts-v1/;
/plugin/;

/ {
   compatible = "allwinner,sun8i-h3";

   fragment@0 {
      target = <&pio>;
      __overlay__ {
         keypad_pins: keypad_pins {
            allwinner,pins = "PA10";
            allwinner,function = "gpio_in";
            allwinner,pull = <1>;
         };
      };
   };

   fragment@1 {
      target-path = "/"; // "/soc";
      __overlay__ {
         mykeypad: mykeypad {
            compatible = "gpio-keys";
            #address-cells = <1>;
            #size-cells = <0>;
            pinctrl-names = "default";
            pinctrl-0 = <&keypad_pins>;
            status = "okay";
            button@10 {
               label = "GPIO KEY_POWEROFF";
               linux,code = <0x74>;
               gpios = <&pio 0 10 1>; // PA10, active_low
            };
         };
      };
   };

   __overrides__ {
      mykeypad =     <&mykeypad>,"status";
   };
};
 

 

Compile it as an overlay DTBO using this command :

git-work/dtc/dtc -@ -I dts -O dtb -o /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo gpio-keys.dts

Then, add this overlay either in /boot/armbianEnv.txt with "overlays=gpio-keys" or by loading it dynamically using :

mkdir /sys/kernel/config/device-tree/overlays/gpiokeys
cat /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo > /sys/kernel/config/device-tree/overlays/gpiokeys/dtbo

 

As soon as you connect the PA10 pin to GND, it will trigger a "shutdown" ...

  • Author

Great ! I would try that right away.
A small query though. Can another button be hooked similar to this for a simple reboot also?

By the way thanks a lot for this. You guys just rock.

  • Author
17 minutes ago, martinayotte said:

Compile it as an overlay DTBO using this command :


git-work/dtc/dtc -@ -I dts -O dtb -o /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo gpio-keys.dts

I am sorry but I am really a noob. Do I need to install something for this purpose?? Because it says this
-bash: git-work/dtc/dtc: No such file or directory

 

1 minute ago, Aditya said:

-bash: git-work/dtc/dtc: No such file or directory

Right ! You need to install DT compiler from here : http://ftp.debian.org/debian/pool/main/d/device-tree-compiler/device-tree-compiler_1.4.7-3_armhf.deb

And do not provide this path "git-work/dtc/dtc" but simply "dtc -@ -I dts -O dtb -o /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo gpio-keys.dts" ...

  • Author
9 minutes ago, martinayotte said:

Right ! You need to install DT compiler from here : http://ftp.debian.org/debian/pool/main/d/device-tree-compiler/device-tree-compiler_1.4.7-3_armhf.deb

And do not provide this path "git-work/dtc/dtc" but simply "dtc -@ -I dts -O dtb -o /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo gpio-keys.dts" ...

After installing that and compiling, it gave a couple of warnings
 

/boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo: Warning (unit_address_vs_reg): /fragment@1/__overlay__/mykeypad/button@10: node has a unit name, but no reg property
/boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo: Warning (avoid_unnecessary_addr_size): /fragment@1/__overlay__/mykeypad: unnecessary #address-cells/#size-cells without "ranges" or child "reg" property

Is that okay

  • Author

I short the PA10 pin to ground and its not shutting down.
I have installed wiring pi and by giving command

gpio readall

I can see that PA10 is an input and voltage is high(pullup).
But not shutting down. What could be the reason?

  • Author

I did but it had asked for permission which I missed somehow. I mean I used sudo but it again said permission denied for half of the command.

 

Later used su command first and then gave loaded it easily.

  • Author

Thanks a lot for helping me out. Can you please take a look into the reboot button also.

1 hour ago, Aditya said:

Can you please take a look into the reboot button also.

I've look during more than an hour, it deosn't seems to be implemented ...

 

The only other way I would see is to run this python script and add a " os.system("reboot") " after the "button pressed" print :

 

 


#!/usr/bin/env python
"""Read button.

Make gpio input and enable pull-up resistor.
"""

import os
import sys
import time
import logging

if not os.getegid() == 0:
    sys.exit('Script must be run as root')

from pyA20.gpio import gpio
from pyA20.gpio import connector
from pyA20.gpio import port


button = port.PA10

"""Init gpio module"""
gpio.init()

"""Set directions"""
gpio.setcfg(button, gpio.INPUT)

"""Enable pullup resistor"""
gpio.pullup(button, gpio.PULLUP)
#gpio.pullup(button, gpio.PULLDOWN)     # Optionally you can use pull-down resistor


try:
    while True:
        state = gpio.input(button)      # Read button state
#        print (state)
        if state == 0:
            print("button pressed")
            logging.info("button pressed")
        time.sleep(0.2)

except KeyboardInterrupt:
    print ("Goodbye.")
 

 

Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.