|Project Name||Stars||Downloads||Repos Using This||Packages Using This||Most Recent Commit||Total Releases||Latest Release||Open Issues||License||Language|
|Joycontrol||899||2 years ago||48||gpl-3.0||Python|
|Emulate Nintendo Switch Controllers over Bluetooth|
|Brad Homeassistant Config||209||5 years ago||7||HTML|
|Home Assistant configuration|
|Rcswitch Pi||195||2 years ago||22||C++|
|RCSwitch implementation for the Raspberry Pi|
|Raspberry Pi Safe Off Switch||159||2 years ago||Python|
|A set of scripts that can be used to provide a "safe power off switch" for a Raspberry Pi.|
|A node.js based home automation system based around the Raspberry Pi.|
|Dasdeployer||86||2 years ago||mit||Python|
|Raspberry Pi powered manual release approval gate for Azure Pipelines. Mit viele blinkenlighten|
|Raspberry PI based WebSDR server|
|Rascon_ns||59||3 years ago||gpl-3.0||Python|
|Connect to Nintendo Switch over Bluetooth, emulate amiibo and use script from the web.（蓝牙连接Nintendo Switch，并可通过网页控制和使用脚本与amiibo）|
|Pi Supply Switch||47||2 years ago||6||other||Shell|
|Pi Supply Switch On/Off Switch for Raspberry Pi|
|Rpi Matrix||30||9 months ago||18||mit||TypeScript|
|A simple RaspberryPi clock and browser simulator|
Add a switch to your Raspberry Pi to safely shut it down without pulling the power.
To keep prices down, the Raspberry Pi is missing something that most electronic devices come with: a switch to turn it on or off. That's okay, you say, we'll just pull the plug to turn it off. Unfortunately, this can lead to corruption problems with the SD card. All the instructions say you should run the shutdown command before pulling the plug, but this is not always possible, particularly when your Raspberry Pi is running headless without a connected keyboard and monitor, and possibly even without any network connection. So, what CAN a self-respecting DIY-er do? The answer, of course, is: add your own switch!
Lots of articles are available to tell you how to use a breadboard to connect a button or LED to a Raspberry Pi's GPIO pins. This article focuses on doing something useful with those switches and LEDs.
The safe off switch is complementary to a reset switch, which is the best method to start the Raspberry Pi back up again. Issue 52 of TheMagPi had an excellent article on how to connect a Reset Button.
(A version of this article appeared in Issue 57 of The MagPi.)
With the GPIO Zero library module, the Python code to deal with a button
press becomes extremely simple.
(If you are on a system that does not already have the gpiozero module, such as some of the lite distributions,
you will see a message such as
ModuleNotFoundError: No module named 'gpiozero'.
If that is true, then install the module using a command such as
sudo apt install python3-gpiozero.)
Assuming your button is connected between
GPIO 21 and Ground, the code can look like as easy as this:
#!/usr/bin/env python3 from gpiozero import Button import os Button(21).wait_for_press() os.system("sudo poweroff")
This code creates a button on GPIO 21, waits for it to be pressed, and then executes the system command to power down the Raspberry Pi. GPIO 21 is nice because it's on pin 40 of the 40-pin header and sits right next to a ground connection on pin 39. This combination makes it difficult for an off switch to get plugged in incorrectly. On a 26-pin header, GPIO 7 is similarly situated at the bottom there on pin 26, next to a ground connection on pin 25.
If you don't mind losing your I2C connectivity, an alternative choice would be GPIO 3, situated on pin 5. What is particularly nice about GPIO 3 is that it also acts as a reset pin when the computer is powered down. By using GPIO 3, you can use a single button for both an ON and OFF switch.
Create the script on your Raspberry Pi using your favorite text editor (e.g., nano, vim or emacs), and make certain that it's executable, as in
$ nano shutdown-press-simple.py $ chmod a+x shutdown-press-simple.py
Then add a line to the end of /etc/rc.local to run it at boot time:
$ sudo su # echo '~pi/shutdown-press-simple.py &' >> /etc/rc.local
If your /etc/rc.local ends with an
exit statement, this new line will be ignored because it is after the exit statement.
tail /etc/rc.local to check.
If so, use your favorite editor to move the lines around so that the invocation of the python script goes before the exit statement.)
$ tail /etc/rc.local $ sudo nano /etc/rc.local
Now after rebooting, your script will be running and listening for a button (connected between GPIO 21 on pin 40 and ground) to be pushed.
One major drawback of the previous code is that any accidental push of the button will shut your Raspberry Pi down. It would be better if you needed to hold the button down for several seconds before everything powers down.
#!/usr/bin/env python3 from gpiozero import Button from signal import pause import os, sys offGPIO = int(sys.argv) if len(sys.argv) >= 2 else 21 holdTime = int(sys.argv) if len(sys.argv) >= 3 else 6 # the function called to shut down the RPI def shutdown(): os.system("sudo poweroff") btn = Button(offGPIO, hold_time=holdTime) btn.when_held = shutdown pause() # handle the button presses in the background
Instead of hard-coding the GPIO number 21 and the hold time, this code
does a few things differently.
First, it defines variables to hold these numbers at the top of the code.
For a program this small, declaring the values at the top is not necessary,
but it is good practice to declare any configurable variables near the top
of the code.
When making changes later, you won't have to hunt through the code to find
Secondly, it allows the GPIO number and hold time to be overridden on the
command line, so that you can change them later without modifying the program.
We then define a function named
shutdown() to execute the
poweroff system command.
The button is also assigned to a variable for use in the next statement.
This time, we are also specifying that the button must be held down, and
when the hold time (6 seconds) has passed, any function assigned to the
when_held event will be executed. We then assign that event to the
shutdown() function we defined earlier.
The call to
pause() is needed to cause the script to wait for the button presses.
If you look at the examples that come with the GPIO Zero source, you'll find a script very similar to this one.
But, we can do better.
The major thing lacking with the above code is any sort of feedback -- it's hard to tell
that anything is really happening while you have the button pressed down.
Fortunately, GPIO Zero allows us to do much more with a button press, as well as controlling
For example, we can turn an LED on and off, or set it blinking, when the button is first
pressed by attaching to the button's
We need to ensure that the LED is turned off if the button is not held down for the entire
length of time.
This can be accomplished by attaching to the when_released event.
As before, the important work has been moved into functions named
when_released() and the same
shutdown() function we used before.
These are assigned to their corresponding button events.
Instead of wiring in your own LED, many versions of the Raspberry Pi come with several LEDs already on them that can be controlled. The Raspberry Pi A+, B+ and Pi2 boards have an Activity Status LED and a Power LED that can be accessed through the GPIO numbers 47 and 35, respectively. (Early versions of the Raspberry Pi used GPIO 16 for the Activity Status LED, but did not provide access to the Power LED.) The Raspberry Pi Zero and Computation Modules have the Activity Status LED on GPIO 47. (The GPIO Zero library does not yet have a way to control the LEDs on the Pi3.)
#!/usr/bin/env python3 from gpiozero import Button, LED from signal import pause import os, sys offGPIO = int(sys.argv) if len(sys.argv) >= 2 else 21 holdTime = int(sys.argv) if len(sys.argv) >= 3 else 6 ledGPIO = int(sys.argv) if len(sys.argv) >= 4 else 2 def when_pressed(): # start blinking with 1/2 second rate led.blink(on_time=0.5, off_time=0.5) def when_released(): # be sure to turn the LEDs off if we release early led.off() def shutdown(): os.system("sudo poweroff") led = LED(ledGPIO) btn = Button(offGPIO, hold_time=holdTime) btn.when_held = shutdown btn.when_pressed = when_pressed btn.when_released = when_released pause()
The GPIO Zero library will print a warning message if you try using either of these LEDs. The workaround for now is to turn off the warning message temporarily. With current versions of the GPIO Zero library you are using, you can use:
import warnings ... ledGPIO = 47 ... with warnings.catch_warnings(): warnings.simplefilter("ignore") led = LED(ledGPIO)
If you cannot use the on-board LEDs, you can connect an LED (with a small resistor) to the GPIO of your choice. See numerous other articles on how to connect an LED to a GPIO pin.
To make it more obvious what is happening, it is possible to be more dynamic in your feedback.
For example, how about starting with a slow blink, but progressively blink faster and faster?
The GPIO Zero makes this easy because it passes information to the event functions that lets
you make changes during the button press, and can be set up to call the button press event
function repeatedly instead of just once.
In this code, we switched to using GPIO Zero's LEDBoard() class to blink multiple LEDs together.
(Here, we're passing in the Activity Status and Power LED GPIO numbers.)
The constructor for
Button() has a new parameter (
hold_repeat=True) and we've set the hold
time to 1 second instead of the full hold time.
when_released() functions remain the same, but the
now declares its button parameter, and asks the button how long it's been pressed so far.
The blink rate is then updated accordingly.
When the maximum hold time is finally passed, only at that time is the
poweroff system command executed.
#!/usr/bin/env python3 from gpiozero import Button, LEDBoard from signal import pause import warnings, os, sys offGPIO = int(sys.argv) if len(sys.argv) >= 2 else 21 offtime = int(sys.argv) if len(sys.argv) >= 3 else 6 mintime = 1 # notice switch after mintime seconds actledGPIO = 47 # activity LED powerledGPIO = 35 # power LED def shutdown(b): # find how long the button has been held p = b.pressed_time # blink rate will increase the longer we hold # the button down. E.g., at 2 seconds, use 1/4 second rate. leds.blink(on_time=0.5/p, off_time=0.5/p) if p > offtime: os.system("sudo poweroff") def when_pressed(): # start blinking with 1/2 second rate leds.blink(on_time=0.5, off_time=0.5) def when_released(): # be sure to turn the LEDs off if we release early leds.off() with warnings.catch_warnings(): warnings.simplefilter("ignore") leds = LEDBoard(actledGPIO, powerledGPIO) btn = Button(offGPIO, hold_time=mintime, hold_repeat=True) btn.when_held = shutdown btn.when_pressed = when_pressed btn.when_released = when_released pause()
If you have a speaker connected to your Raspberry Pi, you could play audio clips
instead of blinking LEDs.
An easy way to do this is to use the
pygame.mixer.Sound class to play your audio clips.
For example, you could repeatedly play the audio clip that says "I'm melting, melting"
from the Wizard of Oz, and then play the audio clip that says "There's no place like home"
right before powering down the Raspberry Pi.
The basic structure of the code remains the same. At the beginning, we need to initialize the sound system, and then create the sound clips. When the button is pressed, we start playing the "I'm melting" clip, looping it enough times for it to last the hold time. If the button is released early or the hold time has elapsed, we need to stop that clip. When the hold time has elapsed, we then start playing the "There's no place like home" clip and power down.
#!/usr/bin/env python3 from gpiozero import Button from signal import pause import pygame.mixer from pygame.mixer import Sound import time, os holdTime = 10 offGPIO = 21 pygame.mixer.init() melting = Sound("ImMeltingMelting.ogg") nohome = Sound("NoPlaceLikeHome.ogg") def shutdown(): # stop playing one sound and switch to another melting.stop() nohome.play() # give it a chance to play time.sleep(1) # and shutdown for real os.system("sudo poweroff") def when_pressed(): # start playing melting.play(loops=holdTime/melting.get_length()) def when_released(): # stop playing if released early melting.stop() btn = Button(offGPIO, hold_time=holdTime) btn.when_held = shutdown btn.when_pressed = when_pressed btn.when_released = when_released pause()
Can you think of other ways to provide feedback while pressing the hold button? How about using a buzzer, or popping up a message on a screen? Let your imagination run wild. :)
Can you think of other ways to be signaled that it is time to turn off? How about watching the "low battery" signal from a battery pack? What else can be used to trigger a shutdown?
Now, which of your projects are you going to add shutdown and reset buttons to?