Raspberry PI Power Switch

A circuit to provid a power on and off switch for a Raspberry PI that safely shuts down the PI and then removes all power offering 100% power saving.

A couple of years ago I was working on a few projects that made use of the most excellent Raspberry PI which, if you’ve never heard of it, is a credit card sized computer that was designed by the Raspberry Pi Foundation in order to advance computer education for adults and children. The PI can be bought for less than £30 and has a nice range of general purpose I/O built into it for controlling pretty much anything you want.

What it doesn’t have, however, is a power switch! There are quite a few people who have addressed this problem with designs of their own, and some of them are selling ready built modules to do it for you.

There are two things to consider when designing a power switch. The first and most important is to make sure the PI shuts down safely. Whilst you can just pull the power on the unit, it’s not a good idea. If the file system is not shut down correctly you risk corrupting it, which means your PI won’t turn back on as expected, and you might be forced to reformat the SD card and install all your software and applications on it again. Once the PI has shutdown safely, you want to remove the power from it. The second thing to consider is that, after you have shut it down, you want to be able to turn it on again!

The modules I saw all work on the same principle: detect then the PI has been shut down and disconnect the power to it with a relay. Turning the power back on is just a case of turning the relay on, which enables the PI to boot, and then the circuit that detects that the PI is running keeps the relay on. You need some time delay to make sure the relay stays on until the PI has booted.

Shutting the PI down safely involves a small script running on the PI monitoring one of the GPIO pins: when the script detects a press of the power button, it safely shuts down the PI.

Ideally, we want maximum power saving when the PI is turned off. However, all the modules I found online required the 5V power supply to be on to power the switch curcuit. They will happily turn off the PI, which saves the majority of the current, but not all of it.

Since I like solving problems and building things, I decided to design a max power-saving switch circuit. This is what it should do:

  • Be able to switch the PI on wihtout any initial power supply active.
  • Know when our main application on the PI was up and running, and switch on an auxillary power supply for the externaly connected hardware.
  • When the user wants to shutdown the system, switch off the auxillary power supply, then safely shut down the PI and the main supply.
  • Provide visual indication to the user about what state the system is in.

The circuit uses a small 8 pin PIC microprocessor, a couple of relays (if you don’t want to switch to the auxiliary power supply then you would only need one) and a handful of components to hook it all together.  The relay switches the power on the mains side of the 5V power supply, so that when the relay is off, all of the power is off (as we wanted).

The microprocessor controls the relay, and on powering up the first thing it does is switch the relay on. In order to power up the unit we simply have our power switch bypass the relay contacts, so the power comes on, and then the processor holds the power on for us and we can let go of the power button. Because the processor turns the relay on immediately, you only need the briefest of pushes to power the system up.

It’s important that you use a power switch capable of switching mains voltage at a current rating equal to or greater than the inrush current of your chosen power supply. 

The following circuit diagram shows how to hook up the microprocessor. If you don’t need the aux relay then you don’t need any components from pin 8 on the processor onwards. The 2 GPIO pins have been left wihtout identification so you can pick any suitable ones that are free on your PI. The GPIO pin connected to pin 4 on the processor is a ‘ready’ signal. This is used to tell the processor to switch the aux relay on, and also to indicate to the user that the PI is booted and ready to rock & roll via the LED.

Since we are talking about the LED, we used a nice power switch that had a blue LED built in, and had 2 sets of momentary contacts (DPDT or DPCO) so one push button is used for both the power on and off and LED components from the circuit diagram.

When choosing your relay, please make sure its contacts are rated for switching mains voltage at a current rating equal to or greater than the inrush current of your chosen power supply. 

The main power supply for our application was 24V, and we had a secondary 24v to 5v DC to DC convertor running of this to power the PI. So we chose relays with a coil voltage of 24V so they could be driven directly from the main power supply. If you’re just using your 5V pi power supply, then connect the “Supply” to the +5V line, and choose a relay with a 5V coil voltage.

You will notice that the pull up resistor for the Off switch is connected to the 3.3V output on the PI. This is important because the GPIO lines on the PI use 3.3v CMOS levels and not 5v. You will damage the GPIO input stage if you connect it to 5v.

User Operation

With everything off the User presses the power button and the LED flashes rapidly until it detects a voltage on the Raspberry PI TX pin. This tends to be a few seconds after power is applied to the PI.  The LED then flashed slowly until the PI outputs on the “Ready” GPIO pin connected to pin 4 on the processor. This causes the LED to be constantly lit, and if the Aux relay is present it will turn on as well.

When the user wishes to shutdown the PI, a simple press of the Off switch will cause the Aux relay to turn off and the led to flash slowly. The shutdown script on the PI also detects the button press and initiates shutdown. Once the PI is shutdown it stops outputing on the TX pin, and the processor detects this. It rapidly flashes the LED for 5 seconds, and then shuts down the Power relay. This 5 second delay  allows for the PI to be happily rebooted from the command line without the power being switched off.

If the processor detects that the PI has switched off but not because of the power button being pressed, it will turn off the AUX relay immediately, and then rapidly flashes the LED for 5 seconds before removing the power.

Shutdown script

The shutdown script is a simple python script that needs to be run as soon as the PI starts. We used PIN 15 for the Off switch, and PIN 13 for the READY output.

import RPi.GPIO as gpio

import os

gpio.setmode(gpio.BOARD)

gpio.setwarnings(False)

gpio.setup(15, gpio.IN, pull_up_down=gpio.PUD_OFF)

gpio.wait_for_edge(15, gpio.FALLING)

os.system('shutdown now -h')

We set the ready GPIO pin from within the application, so the AUX relay only comes on when we want it to, but you could easily add it into the shutdown script with the following

gpi.setup(13, gpio.OUT)

gpi.output(13, True)

To run the script at startup the easiest way is to edit a file called /etc/rc.local and add the following before the exit 0 at the end

/usr/bin/python /home/pi/shutdown.py > /dev/null 2>&1 &

This assumes you called the script shutdown.py and have it in the home directory of the pi user.

Leave a Reply

Your email address will not be published. Required fields are marked *