Friday, April 13, 2018

Raspberry Pi on-off switch

For a complete index of all my stories click this line

Lately I have been intensively being playing with my Raspberry Pi's. Indeed I have several Raspberry Pi-2's, Pi-3's and Zero's as well as Zero-W's. Many of them being put to use as an internet radio, domotics server, printer server, photo cam, control-system for my 3d printer etc. etc.

And the more I use them the more I miss an on-off button. So on my headless systems (systems without a monitor, keyboard and mouse) there is no easy way to shut-down the Pi. I have to SSH into the Raspberry and then use the shutdown command. Sometimes that is no problem however for my stand-alone internet radio that is annoying.

Beware that just pulling the power-plug from the Raspberry might corrupt your SD card, therefore that is no option.

So let's make an on-off button for the Raspberry.

The hardware

The hardware is very straightforward and identical for the Raspberry Pi 2, 3 and Zero.
 


Above is the breadboard layout for the Raspberry Pi 2 & 3



 And this is the layout for the Raspberry Pi Zero / Zero W



The Raspberry Pi-Zero has no I/O headers. So you could solder the wires direct to the board. However I urge you to solder headers and use Dupont wires to attach them to your breadboard. More projects with the Pi-Zero will follow and then a header will make it easier to experiment.
 

Just put a switch on a breadboard and attach one lead to GND and the other lead to GPIO3
That's all.

Switch ON

This is the easy part and achieved automatically.
As soon as you push the button the Raspberry will boot.
This is build-in in the Raspberry-Pi. As soon as GPIO-3 is momentarily connected to GND the Raspberry is triggered to switch on.

Switch OFF

This is a bit more complicated and involves some Python programming.
You could use the Geany program editor. However to keep things simple just open the nano editor with the name of the program we are going to write.



First open the terminal window and then make sure you are in the home directory by issuing the following command:

pi@raspberrypi:/ $ cd ~

and then type the next command to start the nano editor:

sudo nano onoff.py

Type in (or copy/paste) the next program lines.



#!/usr/bin/python3

import os
import sys
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def onoff(switchno):
 os.system('sudo shutdown now') 
 
GPIO.add_event_detect(3, GPIO.FALLING, callback=onoff, bouncetime=300)

try:
 while True:
  pass
   
except KeyboardInterrupt:
 GPIO.cleanup()


When finished press CTRL-X and answer yes at the question wether the file needs to be saved and look if the right name (onoff.py) is being used.
   
Let's have a look at the program.

I started by importing the needed libraries. Without the RPi.GPIO library the I/O ports can not be used by Python. And the OS library is needed to shutdown the system.
The GPIO mode is set to BCM so we can call the I/O ports by name rather then by pin number. And then GPIO 3 (where the button is attached) is defined as an input with a Pull-Up resistor.

Next a function is defined that calls the os.system library to shutdown the Pi
And then an interrupt is defined that tests if the GPIO port is connected to GND (that is when the switch is pressed). If that happens the function is called that switches the PI off.

The last part just makes sure the program runs indefinitely.

Now there is just one last thing to do.

Make sure the Python program runs at booting.

As we are going to use our Pi for multiple purposes we need the Pi to be started up fully. Meaning the GUI needs to be running before our program starts. We can do that by altering the file autostart in the /home/pi/.config/lxsession/LXDE-pi/ directory.
So in the terminal window type:

sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

and alter the file by adding the line @python3 /home/pi/onoff.py &

Enter this line before the line that starts the xscreensaver otherwise it might have problems with autorunning. My autostart file looks like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@python3 /home/pi/onoff.py &
@xscreensaver -no-splash
@point-rpi


The & at the end of the line makes sure the Python program runs in the background and does not open a terminal window. So you will start with a clean desktop.

How to use the on/off button

Reboot your Raspberry and the program will be activated.
If you press the button the Raspberry will shut down. Pressing the button again makes the Raspberry start-up again.
No keyboard, mouse or screen needed to accomplish this anymore.

This uses some of the Pi's resources and therefore will slow down the Pi. This is most noticeable on a Pi Zero.
Using this as a stand-alone program therefore is overkill. I will be incorporating this method however in several projects (stand-alone internet radio, PI-camera, Pi-security camera etc) which makes it very usefull.

So keep returning to these pages for more projects. 

Till next time: have fun.

Luc Volders