Combined Restart / Shutdown Button for Raspberry Pi

A very simple systemd service for Raspberry Pi that provides a software-controlled restart / shutdown button. Code: scruss/shutdown_button

Use

Default behaviour is:

  • your Raspberry Pi will reset if the button is held for more than two seconds but fewer than five seconds;
  • your Raspberry Pi will shut down if the button is held for more than five seconds.

By default, the software assumes the switch is connected to pin BCM 27. Both the pin and the timing can be changed in the Python source file.

Requirements

Hardware

  • A Raspberry Pi (tested on a model 2B, 3B and Zero)
  • A normally open, momentary contact button. I use surplus ATX power buttons (as used on desktop PCs), as they’re cheap and come with a handy set of wires and header connectors. Virtually any button will do the job, though. Just make sure it’s normally open (push to close).

Software

  • A Debian-based operating system that uses systemd (tested on Wheezy [RetroPie] and Stretch)
  • the python3-gpiozero package to provide GPIO Zero (tested on version 1.4.0)

Installation

Hardware

Connect the button between GPIO 27 and GND. If you use an ATX power button and a Raspberry Pi with a 40-pin GPIO header, connect it across the seventh column from the left:

            -
· · · · · ·|·|· · · · · · · · · · · · · 
· · · · · ·|·|· · · · · · · · · · · · · 
            -

This shorts GPIO 27 (physical pin 13) to ground (physical pin 14) when the button is pressed.

Software

The software is installed with the following commands:

sudo apt-install python3-gpiozero
sudo mkdir -p /usr/local/bin
chmod +x shutdown_button.py
sudo cp shutdown_button.py /usr/local/bin
sudo cp shutdown_button.service /etc/systemd/system
sudo systemctl enable shutdown_button.service
sudo systemctl start shutdown_button.service

Troubleshooting

Enabling the service should produce output very similar to:

Created symlink /etc/systemd/system/multi-user.target.wants/shutdown_button.service → /etc/systemd/system/shutdown_button.service.

You can check the status of the program at any time with the command:

systemctl status shutdown_button.service

This should produce output similar to:

● shutdown_button.service - GPIO shutdown button
   Loaded: loaded (/etc/systemd/system/shutdown_button.service; enabled; vendor 
   Active: active (running) since Sat 2017-10-21 11:20:56 EDT; 27s ago
 Main PID: 3157 (python3)
   CGroup: /system.slice/shutdown_button.service
           └─3157 /usr/bin/python3 /usr/local/bin/shutdown_button.py

Oct 21 11:20:56 naan systemd[1]: Started GPIO shutdown button.

If you’re seeing anything other than Active: active (running), it’s not working. Does the Python script have the right permissions? Is it in the right place? If you modified the script, did you check it for syntax errors?

The output from dmesg will show you any error messages generated by the service.

Modifications

If you use a HAT/pHAT/Bonnet/etc. with your Raspberry Pi, check pinout.xyz to see if it uses BCM 27. If you do need to change the pin, best to pick one that doesn’t have a useful system service like serial I/O or SPI. If you’re using an ATX button with a two pin connector, make sure you choose a pin physically adjacent to a ground pin.

If you modify the timing, please ensure that you keep the shutdown button press duration longer than the reboot one. Otherwise you’ll only be able to shut down.

Notes

You should not need to reboot to enable the service. One machine of mine — a Raspberry Pi Zero running Raspbian Stretch — did need a reboot before the button worked.

The reboot code is based on the Shutdown button example from the GPIO Zero documentation.

This is not the only combined shutdown/reset button project to use GPIO Zero. gilyes/pi-shutdown also does so, but pre-dates the implementation of the various hold time functions in GPIO Zero.

GPIO 27 was used, as it’s broken out onto a physical button on the Adafruit PiTFT+ display I own.

This is my first systemd service, and I’m still at the “amazed it works at all” stage. The service file may not contain the ideal configuration.

Installing the Pimoroni OnOff Shim the hard way

Hey! This process permanently modifies your Raspberry Pi and may prevent it from working with many HATs and add-ons … It also has some really fiddly soldering. If in doubt, don’t.

Pimoroni’s OnOff SHIM adds what the Raspberry Pi should have had all along: a power button. While there are lots of soft power switches out there, the OnOff Shim adds circuitry to cut power to the Raspberry Pi after shutdown.

While the shim comes with a 12-socket header, that prevents you from using any other device that uses the leftmost GPIO pins. I wanted access to all the pins, and not have the shim create an unsightly bump on the pins. So this is what I did …

Regular GPIO header in place on a Raspberry Pi Zero W
Test-fit the socket header to mark where you’re going to cut the pin support block
Very carefully, cut the pin support block between (physical) pins 11-13 and 12-14. I used a mix of a sharp craft knife and nibbling with fine diagonal cutters. Try not to bend the GPIO pins either as you cut the block or lever the block out of the way
Apply non-conductive tape to the back of the OnOff Shim. I used Kapton, but electrical/insulating tape will do. Just make sure you don’t block any of the GPIO holes
Test-fit the shim. You may have to pare away at the support block a bit to get it to fit level with the rest of the pins
Now solder the shim in place. You want the solder joints as small as possible. I used extra liquid flux and very fine silver solder to just fill the pin holes. Too much solder left on the pins will stop HATs and socket headers fitting, so you may have to desolder as I had to do on the rightmost pin
Example HAT fitted with OnOff shim underneath. Note that his particular HAT — the Pimoroni Inky pHAT — will not work with this shim.

The OnOff SHIM uses GPIO pin BCM 17 (physical pin 11) as the power button sensor and BCM 4 as the power off signal. Any device that also uses BCM 17 (and possibly BCM 4) will likely cause the reset process to be triggered. This means that I can’t use the shim with my Inky pHAT EPD. You would have thought that Pimoroni might’ve considered that, since they made both. Consulting pinout.xyz suggests that 41 boards that likely may not work with the OnOff shim: Cirrus Logic Audio Card, Display-o-Tron 3000, DOTs, Enviro pHAT, ESP IoT pHAT, Explorer HAT, Explorer HAT Pro, Flex, GertVGA 666, High-Precision AD/DA Board, Hyperpixel, Inky pHAT, IoT pHAT, LEDBorg, MotoZero, Navio2 Autopilot, PaPiRus HAT, PaPiRus Zero, Piano HAT, Pibrella, Picade HAT, Pi Cap, PiGRRL Gamepad, Pi-LITE-r, Pi-mote, Pi PoE Switch HAT, PiStep2 Dual, PiStep2 Quad, Pi Stop, Propeller HAT, RoboHat, RTK Motor Controller, Servo PWM Pi Zero, Skywriter HAT, Ultimate GPS HAT, Voice HAT, Witty Pi, Witty Pi 2, Zero2Go, Zero LiPo and ZeroSeg.

While I like the OnOff SHIM, check carefully that it will work with your application.

our robust power system

I’m writing this during a blackout. Our little enclave, just southeast of Kennedy and Eglinton, can have the power out when the nearby streets are fine. I can see that the (formerly derelict) Coffee Time has power, as has the block the other side of Kennedy. I wonder what makes us so special?

All the printers I’ve ever owned …

bird you can see: hp print test

  • An ancient (even in 1985) Centronics serial dot-matrix printer that we never got working with the CPC464. The print head was driven along a rack, and when it hit the right margin, an idler gear was wedged in place, forcing the carriage to return. Crude, noisy but effective.
  • Amstrad DMP-2000. Plasticky but remarkably good 9-pin printer. Had an open-loop ribbon that we used to re-ink with thick oily endorsing ink until the ribbons wore through.
  • NEC Pinwriter P20. A potentially lovely 24-pin printer ruined by a design flaw. Print head pins would get caught in the ribbon, and snap off. It didn’t help that the dealer that sold it to me wouldn’t refund my money, and required gentle persuasion from a lawyer to do so.
  • Kodak-Diconix 300 inkjet printer. I got this to review for Amiga Computing, and the dealer never wanted it back. It used HP ThinkJet print gear which used tiny cartridges that sucked ink like no tomorrow; you could hear the droplets hit the page.
  • HP DeskJet 500. I got this for my MSc thesis. Approximately the shape of Torness nuclear power station (and only slightly smaller), last I heard it was still running.
  • Canon BJ 200. A little mono inkjet printer that ran to 360dpi, or 720 if you had all the time in the world and an unlimited ink budget.
  • Epson Stylus Colour. My first colour printer. It definitely couldn’t print photos very well.
  • HP LaserJet II. Big, heavy, slow, and crackling with ozone, this was retired from Glasgow University. Made the lights dim when it started to print. Came with a clone PostScript cartridge that turned it into the world’s second-slowest PS printer. We did all our Canadian visa paperwork on it.
  • Epson Stylus C80. This one could print photos tolerably well, but the cartridges dried out quickly, runing the quality and making it expensive to run.
  • Okidata OL-410e PS. The world’s slowest PostScript printer. Sold by someone on tortech who should’ve known better (and bought by someone who also should’ve known better), this printer jams on every sheet fed into it due to a damaged paper path. Unusually, it uses an LED imaging system instead of laser xerography, and has a weird open-hopper toner system that makes transporting a part-used print cartridge a hazard.
  • HP LaserJet 4M Plus. With its duplexer and extra paper tray it’s huge and heavy, but it still produces crisp pages after nearly 1,000,000 page impressions. I actually have two of these; one was bought for $99 refurbished, and the other (which doesn’t print nearly so well) was got on eBay for $45, including duplexer and 500-sheet tray. Combining the two (and judiciously adding a bunch of RAM) has given me a monster network printer which lets you know it’s running by dimming the lights from here to Etobicoke.
  • IBM Wheelwriter typewriter/ daisywheel printer. I’ve only ever produced a couple of pages on this, but this is the ultimate letter-quality printer. It also sounds like someone slowly machine-gunning the neighbourhood, so mostly lives under wraps.
  • HP PhotoSmart C5180. It’s a network photo printer/scanner that I bought yesterday. Really does print indistinguishably from photos, and prints direct from memory cards. When first installed, makes an amusing array of howls, boinks, squeals, beeps and sproings as it primes the print heads.

in the running

Almost ‘Best of The Year’ time. In the running are:

A Hawk and a Hacksaw – The Way the Wind Blows
A.C. Newman – Souvenir of Canada – EP
Beck – The Information
Calexico – Garden Ruin
Casper & the Cookies – The Optimist’s Club
Colin Meloy – Colin Meloy Sings Shirley Collins
Eels with Strings – Live At Town Hall
Elf Power – Back To The Web
Erynn Marshall – Calico
Faun Fables – The Transit Rider
Grandaddy – Just Like The Fambly Cat
Grant-Lee Phillips – nineteeneighties
Hidden Cameras – Awoo
Joanna Newsom – Ys
Jolie Holland – Springtime Can Kill You
King Biscuit Time – Black Gold
Mayor McCa – Cue Are Es Tea You
Peter Stampfel – The Jig Is Up
Robyn Hitchcock & The Venus 3 – Olé! Tarantula
Sufjan Stevens – Songs For Christmas – Volume V: Peace
Sufjan Stevens – The Avalanche – Outtakes And Extras From The Illinois Album
The Be Good Tanyas – Hello Love
The Decemberists – The Crane Wife
The Essex Green – Cannibal Sea
The Flaming Lips – At War With The Mystics
The Handsome Family – Last Days of Wonder
The Instruments – Cast A Half Shadow
The Sadies – In Concert Vol. 1
The Wailin’ Jennys – Firecracker
Thom Yorke – The Eraser
Thomas Dolby – The Sole Inhabitant
Wendy Arrowsmith – Crying Out
Yo La Tengo – I Am Not Afraid Of You And I Will Beat Your Ass

Miraculously, all of them fit on my iPod Nano, so they’ll be in heavy rotation over the next week or so while I decide.

kaboom!

From Apple’s Battery Exchange Program iBook G4 and PowerBook G4, it looks like I’ve got one of the defective ones. It’s good that I’m getting a new battery, as I’ve noticed this one doesn’t have the life it used to.

the computer does not work

My 4 year old Athlon XP box finally gave up this week. It had been acting ropily for a few months, and now it won’t even boot. Don’t really need to replace it with anything powerful; maybe just a cheapo Sempron box. We’ll see what Canada Computers has to offer.

a joy forever

a thing of beauty

I finished fixing up the brakes on the Super Galaxy, and put new handlebar tape on the bars. I still suck at fitting bar tape; should’ve stuck to my old standard Benotto tape, which, while almost useless for shock absorption, is cheap and easy to fit.

Once all was fitted, I took it for a spin. The new brakes are a delight; very positive and extremely powerful. I will enjoy riding again.

(And yes, you bike nerds, there is no straddle cable in that picture.)

scissors, shears, geddit?!

where alpha is scissors

The presenter of this paper claimed that PowerPoint changed α, the wind shear coefficient, to ✂. We laughed, briefly.

Your source for “Atomkraft? Nej Tak” badges

nuclear power? no thanks

You can still buy these here: WISE – Nuclear issues information service. I just ordered a bunch.
(The above image belongs to OOA and is used without their express permission.)

Find out more at the smiling sun, nuclear power no thanks site.

Happy Danish Sockets

Am I losing it, or do Danish power sockets look like smileys?

Two happy people:

happy danish sockets 1

Happy person with a chef’s hat (isometric view):

happy danish sockets 2

Do these remind you of anything?

Energy Saving Tips for Canadians, #1: a name thing

Canadians are remarkably profligate in their energy use, and I think I know why. It’s not to do with the oft-cited scale of the country, the size of our houses, our cold winters or our hot summers, it’s something simpler than that; it’s what we call our electricity.

Power here is generally known as hydro, and with it comes images of tree-lined rivers with bears happily fishing for salmon. Local electricity companies tend to have that watery thing in their name: Toronto Hydro, Hamilton Hydro, London Hydro (Crieff Hydro is something quite different, though). Some happy green images, eh?

I propose that we stop using the term hydro, and replace it with the snappier smog belching, nuke leaking, only fractionally hydro. It’d certainly make yer average Kathy or Doug drop their double-double (or donut, or dumaurier) when they got their smog belching, nuke leaking, only fractionally hydro bill in. Energy use would plummet, and at no cost to anyone!