Jump to content
  • 0

Shut down OPi Zero using a push button on GPIO


Aditya
 Share

Question

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

Link to comment
Share on other sites

19 answers to this question

Recommended Posts

Order Armbian Linux merch!

  • 0
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" ...

Link to comment
Share on other sites

  • 0
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

 

Link to comment
Share on other sites

  • 0
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" ...

Link to comment
Share on other sites

  • 0
21 minutes ago, Aditya said:

Can another button be hooked similar to this for a simple reboot also?

The <0x74> is KEY_POWER, I've tried <0x198> which seem to be for KEY_RESTART, but it doesn't seem to work ... I will try to dig kernel sources to figure out ...

Link to comment
Share on other sites

  • 0
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

Link to comment
Share on other sites

  • 0

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?

Link to comment
Share on other sites

  • 0
10 minutes ago, Aditya said:

I short the PA10 pin to ground and its not shutting down.

Strange ... Which kernel are you using ?

Did you loaded the overlay ?

If you try "evtest /dev/input/event0", what are you seeing while putting PA10 to GND ?

Link to comment
Share on other sites

  • 0

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.

Link to comment
Share on other sites

  • 0
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.")
 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...