Shutdown your Raspberry Pi using a paper clip

paper-clip

When you’re running your Raspberry Pi headless, one of the first things you should concern yourself with is how to shut it down safely. Shutting it down by simply pulling the power out can result in a corrupt SD card or damaged files. If you had it connected to a network, you could of course just SSH into it and issue the ‘halt’ command. However, what if you couldn’t connect to it? Or what if you just wanted a way to simply and easily shut it down without touching a keyboard at all?

Here’s where Adafruit steps in and gives us a simple script that will monitor a GPIO pin and shut down if it detects a change in state. What use is that? Well, it means you just simply need to connect two pins on the GPIO together with a paper clip (or anything conductive) to issue the halt command. I’ve taken the Adafruit tutorial and some other guides online and put together the following instructions.

Please note: the current version of the software provided by Adafruit takes a second parameter – this is how long you need to “hold” the connection in order to trigger the shutdown. For consistency with Adafruit, I’ve made this “hold time” 3000 milliseconds (3 seconds).

Installation

First of all, you will need a Pi with Git installed on it.

sudo apt-get install git

Next, you download the script from Adafruit’s Github:

git clone https://github.com/adafruit/Adafruit-GPIO-Halt

If this doesn’t work, try:

git clone git://github.com/adafruit/Adafruit-GPIO-Halt

An aside: The script is written in C, so is able to be modified if you want to, say, light up an LED when the halt command is issued. You could re-write it as a Python program, of course, which might make it easier for some to adapt.

Change into the new directory, compile and install:

cd Adafruit-GPIO-Halt
make
sudo make install

This installs the script to /usr/local/bin/gpio-halt. You then need to run it as a service.

Please note

In the following sections, if you’re using an older Pi with a 26-pin GPIO header, use GPIO pin 7 instead of 21.

Run it automatically on Jessie/Buster

If you’re running Raspbian Jessie, you will need to do it via systemd:

sudo nano /lib/systemd/system/gpio-halt.service

This will create a file and open it. The following is the contents of that file:

[Unit]
Description=Short pins 21 and ground to shutdown the Pi
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/local/bin/gpio-halt 21 3000 &

[Install]
WantedBy=multi-user.target

Then, make the script executable by the right users:

sudo chmod 644 /lib/systemd/system/gpio-halt.service

Then, tell systemd to use the script:

sudo systemctl daemon-reload
sudo systemctl enable gpio-halt.service

And reboot your Pi

sudo reboot

When the Pi comes back up, you can check the status of the service by doing:

sudo systemctl status gpio-halt.service

Run it automatically on Wheezy

If you’re running the older Raspbian Wheezy, you will need to do it via rc.local:

sudo nano /etc/rc.local

Before ‘exit 0’, add the line:

/usr/local/bin/gpio-halt 21 3000 &

Now reboot your Pi:

sudo reboot

Now use it

Take a paper clip (or other conductive object) and touch the last two vertical GPIO pins at the same time and hold for at least 3 seconds. If you’re using a 26-pin Pi, it will be GND and GPIO7. If you’re using a 40-pin Pi, it will be GND and GPIO21. See the diagram below. The first time you do this, it might be worth having a monitor connected so that you can see it happening. Wait about 15 seconds for the halt procedure to complete and then unplug your Pi safely.

raspberry_pi_gpio-shutdown-pins

Acknowledgements

Thanks to Alex Eames for spotting the Adafruit shutdown script. Thanks to Adafruit, obviously. Thanks to Matt Hawkins for the systemd instructions. Thanks to commenter Bartwick for noticing that my instructions were out-of-date.

Raspberry Pi powered video binoculars

Josh Williams has taken a Raspberry Pi, a camera module, a pair of binoculars and some laser-cut pieces of wood and fashioned the PiNoculars. You can see from the picture what it is – a pair of binoculars with a video feed mounted above it generated from the camera module which is fixed to one of the eyepieces. It’s a lovely idea and you can read how it was done over on Instructables.

Make an audiophile music player with Volumio on a Raspberry Pi

Trevor Appleton has written a great tutorial about using the Volumio operating system to create a music player with a Raspberry Pi. This is a great piece of software and easy enough to get going. If you’re an audiophile, you can use it with the excellent HATs sold by IQaudIO which give you superior sound quality from the Pi. Read Trevor’s tutorial here.

First Raspberry Jam in Kent to be held – 30th January

JamLogo

There is to be a Raspberry Jam in Gravesend, Kent on 30th January, the first to be held in the county. It will be held at the Gravesend Old Town Hall on the High Street from 10am-1pm and will feature workshops, including Sonic Pi, and talks including details of the recent International Space Station Astro Pi mission. For more details, and to get free tickets, go to this page.

gravesend

The Raspberry Pi Foundation at Bett 2016

At the end of January (20th-23rd), the Raspberry Pi Education Team will be at the Bett Show at ExCel, London. They will be near the STEAM Village on stand 463 and they are offering the opportunity to take part in STEAM (the new word for STEM… but with art added) workshops based around the Pi. On Saturday 23rd, they are holding their very own Raspberry Jam in the “Technology in HE Summit Space” and on Friday 22nd, Sam Aaron will be demonstrating Sonic Pi live coding in the Bett Arena. Various other talks and activities will be taking place across the various stages and spaces.

Myself, Tim and Jamie (from The Pi Hut) attended last year’s event and found it really worthwhile. There’s a great buzz around the place and, with the Foundation’s increased exposure this year, the Pi is certainly going to be highly visible. Definitely worth a visit if you’re at all interested in Computing in Education.

Tim and I will be attending again on the Saturday – maybe we’ll see you there! (We’ll probably be wearing our purple Cambridge Raspberry Jam polo shirts).

View the timetable of the Foundation’s workshops and talks here.

If you want to attend the Raspberry Jam, get your free ticket here.

To attend anything, you will also need to register for Bett itself. You can do that on the Bett website.