📜 ⬆️ ⬇️

Off Button for Raspberry Pi

GPIO ports are one of the main advantages of the Raspberry Pi , it’s not for nothing that in later versions of the board their number was increased from 26 to 40. Unfortunately, most of the cards connected to them (touch screens, LED arrays, expansion cards, etc.), For compatibility with all models “Malinki”, use only the first 26 contacts, leaving the remaining 14 “overboard”. But we will not give the abyss of good! In this article, I will tell you how to make a shutdown or reset button on two unused ports. In fact, the functionality of the button can be any, depending on your imagination and needs.




')
This is my first experience with GPIO, so it was important to understand the logic. Almost any GPIO pin (there are several reserved ones) can be set to one of two states: “output” (aka OUT or logical 1) or “input” (IN or logical 0). The output voltage is 3.3V. You can work with GPIO ports either directly from the terminal or from any programming language. It is described in detail here .



For my button I decided to use the two rightmost contacts in the top row. Connector # 38 (GPIO20) will be set to “output”, and connector # 40 (GPIO21) to “input”. Then the cycle repeating once a second will “listen” to connector # 40 and, as soon as it receives a signal, the used ports will “clear” and a console command will be launched to turn off. As mentioned above, the team can be any other. For the sake of interest, I performed this task in two ways: in Python and a bash script. Below is the code for both options:

shutdown.py
import RPi.GPIO as GPIO from time import sleep import os GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) # Turn off warnings output GPIO.setup(38, GPIO.OUT) # Set pin #38 (GPIO20) to output GPIO.setup(40, GPIO.IN) # Set pin #40 (GPIO21) to input while True: buttonIn = GPIO.input(40) if buttonIn == True: print 'System shuts down' GPIO.cleanup() os.system("sudo shutdown -h now") break sleep(1) 

shutdown.sh
 #! /bin/bash # Set up GPIO20 and set to output echo "20" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio20/direction echo "1" > /sys/class/gpio/gpio20/value # Set up GPIO21 and set to input echo "21" > /sys/class/gpio/export echo "in" > /sys/class/gpio/gpio21/direction while ( true ) do # check if the pin is connected to GND and, if so, halt the system if [ $(</sys/class/gpio/gpio21/value) == 1 ] then echo "20" > /sys/class/gpio/unexport echo "21" > /sys/class/gpio/unexport shutdown -h now "System halted by a GPIO action" fi sleep 1 done 


I repeat, both scripts do the same thing, for the operation of the button you need one of them (any). They can also be downloaded:


In the case of the Python script, there is one nuance: the work requires the RPi.GPIO class, which must be downloaded and installed separately. This is done like this:

 wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz tar zxf RPi.GPIO-0.5.11.tar.gz cd RPi.GPIO-0.5.11 sudo python setup.py install 


Actually, the last thing left to do is add the script to autoload. There are several ways to do this too, I chose cron . To do this, run the command sudo crontab -e and in the opened file add one of the following lines:

 @reboot python /home/pi/lentyay/poweroff.py & @reboot sudo /home/pi/lentyay/shutdown.sh & 




That's all. From myself I will say that using such a button is very convenient.

Source: https://habr.com/ru/post/254723/


All Articles