Interesting timing for this blog post. The announcement of the Model B+ Raspberry Pi continues to flood Twitter and Tech news sites, yet this average blogger has resisted the purchase as I simply have too many Pis already.
One thing that was still missing for me on the B+ was the inclusion of some kind of power switch. Considering how many common RPi projects could make use of this functionality, I was surprised that the foundation hadn’t added it to the new board.
Still, we’ve lived with this problem with the old version for a long time, and there are plenty of switch products out there to get around it. Today I’ve been putting together the On/Off switch from Pi-Supply.com, one of the original switches to be released for the Raspberry Pi.
As Pi Supply don’t have any images in their guide, and as the switch has a lot of parts to solder, I thought it might be useful to write a guide with images to give my readers a bit of confidence building theirs.
My soldering iron is ready…let’s go…
Why Do I Need A Switch?
Well, you don’t. You also don’t NEED a Raspberry Pi, a TV, or even that chocolate bar you’re eating as you read this.
A switch for the Raspberry Pi is a “nice to have”. It means you don’t have to plug and un-plug the micro USB power cable each time you use your Pi – hopefully increasing the life of that delicate connection. It also saves you having to bend down and un-plug that RaspBMC media centre, making the experience that whole lot slicker. That’s just 2 examples – there are loads more.
There’s also a learning element here – this example from Pi-Supply.com requires full assembly, and there’s a lot of parts to solder. I personally enjoy this side of projects, but less confident ‘makers’ may opt for a ready-made solution.
So it’s up to you if you want to add a switch to your Raspberry Pi – but I certainly want one!
The On/Off switch from Pi Supply comes as a bag of parts to be soldered to the supplied PCB.
Once assembled, the switch has 3 buttons for you to control your Pi’s power. You have a button to turn power on, a button to cut the power, and the clever middle button that starts the shutdown sequence for you. This is dead handy for headless setups – no more having to log in and manually run ‘sudo halt’. Once shut down, you use the off button to cut the power off completely.
The switch has a few mounting holes allowing you to fix it to your project. I’m simply mounting mine to a perspex plate, next to my prototyping Pi.
How To Solder the Switch
There is a guide on the Pi-Supply site but no images are available – this is where I come in.
Although it looks a little daunting at first, soldering this switch is reasonably easy if you take your time and double check everything before applying the hot stuff.
The PCB has well labelled sections that also show an outline of each part – so it’s hard to get things in the wrong place.
Step 1: Solder the resistors
You have 6 resistors in the bag – 3 different sets of 2 resistor values, 22k, 10k and 1k – but which ones go where?
First, here are the band colours for each:
22k = Red-Red-Orange-Gold
10k = Brown-Black-Orange-Gold
1k = Brown-Black-Red-Gold
And here is where they go on the PCB: (check the picture below too)
R1 = 22k resistor
R2 = 22k resistor
R3 = 10k resistor
R4 = 10k resistor
R5 = 1k resistor
R6 = 1k resistor
The matching resistors aren’t necessarily next to each other, so watch for that. Again, see the picture below for guidance. Resistors can be any way around (no specific polarity) – but I put the gold ends all facing the same direction to keep things tidy:
Step 2: Solder the Transistors
The transistors are the black ‘D’ shaped components which fit on to sections Q1 and Q2 on the board. The PCB clearly shows a D shaped outline too, so you can’t go wrong. The transistors are the same so it doesn’t matter which one goes where.
Push the transistor down so that it’s flush with the PCB and solder the legs on the reverse of the board:
Step 3: Solder the Capacitor
There’s only one of these, and it’s one of the largest components in the bag. The capacitor does have a set polarity however, so you must ensure you solder the legs in the right holes. This one is going in the C1 position on the PCB.
The longer leg (positive) should go in the hole next to the resistors. You can also tell the negative leg as it has a wide white stripe on the capacitor.
Push the legs through to make the capacitor sit flush with the PCB, and solder as usual.
Step 4: Solder the LED
There’s only one LED on this switch, and LEDs also have a set polarity so make sure you get the legs the right way around. The LED goes into position D3. The shorter negative leg goes towards the transistors, longer leg towards the Pi-Supply print on the PCB.
Step 5: Solder the Relay
Cool – my first ever relay. Not soldered one of these before – but really it’s just 6 very easy straight legs to secure. This one goes in the very obvious position RL1. You can’t enter this the wrong way due to the position of the legs, but just in case, the white stripe goes next to the switch holes.
Step 6: Solder the Diodes
Next up are the 2 diodes. These go into D1 and D2, and have a polarity to follow. The white stripe on these should be the same end as where the USB connector will be fitted. Dark ends goes towards the micro-USB connection.
Step 7: Solder the buttons and pin header
The buttons go in the very obvious positions next to the relay. The slight rectangular shape means they won’t go in the wrong way, so this is one of the easier parts to fit.
Just make sure the buttons are flat before soldering (doing opposite legs first, and then correcting, is a good way to achieve this). I managed to solder my first button a bit wonky – oops – but it does still work.
The pin header goes in at the end after the buttons – smaller legs go through the board, leaving you the long parts to fit jumper wires to.
Step 8: Solder the USB Connector
I challenge you to get this bit wrong – it’s VERY obvious where this goes, but if in doubt see the picture below. I wasn’t sure if I had to solder the large supporting legs – but I did anyway as it stopped it being ‘wobbly’ and it works fine – so go for it.
Step 9: Complete!
There is no step 9 – that’s it, you’ve made the switch!
How to Set Up the Switch
It would be pointless to spend your money on this thing and not take advantage of all its features – so let’s now set up the switch code in your Raspberry Pi so that you can use all 3 buttons.
I used the guide over at Pi-Supply’s site – although I think writing the steps in a more ‘average’ way may help, so let’s get started. We’ll be using Interrupts rather than a While Loop, as it seems (from the comments on the site) that Interrupts use less resources.
Important: make sure you’ve connected the two jumper cables to your Pi. There are two pins on the switch labelled 7 and 8. These connect to physical pins 7 and 8 on your Pi.
Step 1: Create a new directory for the switch script
Let’s make a new folder (I mean directory…) for the switch control script to live in.
Log in to a Terminal session and use the following command to make sure you’re in the main directory:
Then use the ‘mkdir’ command to make a new directory (folder), just as I have done below: (case sensitive)
To test that the dirctory has been created, use the following command and look for ‘PiSupply’:
Step 2: Create the control script and paste the code
Now we’re going to enter that new folder, and create a python script that will control the switch.
Use this command to enter the PiSupply folder:
Now that we’re in the right folder, use the nano command to make a new Python script called softshut.py:
sudo nano softshut.py
This will open a very dull empty python script screen. So, let’s add the code. Copy the code below and paste it into the file:
# Import the modules to send commands to the system and access GPIO pins from subprocess import call import RPi.GPIO as gpio # Define a function to keep script running def loop(): raw_input() # Define a function to run when an interrupt is called def shutdown(pin): call('halt', shell=False) gpio.setmode(gpio.BOARD) # Set pin numbering to board numbering gpio.setup(7, gpio.IN) # Set up pin 7 as an input gpio.add_event_detect(7, gpio.RISING, callback=shutdown, bouncetime=200) # Interrupt loop() # Run the loop function to keep script running
Once you have added the code, press ‘Ctrl+X’ and select to save the file under the same name.
Step 3: Make the Script Load on Start-up
That script isn’t much good unless it runs when we turn on our Pi, so we’ll now make it start on boot automatically.
Use the following command to go into the rc.local file:
sudo nano /etc/rc.local
Then add this line of text before the line that says ‘exit 0’:
NOTE: The guide from PiSupply says to add ‘&’ to the end of that line, but mine didn’t work for me with that included (using a Rev1 Pi). Try it both ways if your switch doesn’t work as expected)
Save and exit the same way as before – press ‘Ctrl+X’ and select to save the file under the same name.
Step 4: Reboot to make the magic happen
Let’s reboot and make sure the switch works. Use the usual reboot command below to restart your Raspberry Pi:
Once it has rebooted, you should see the switch’s LED light up. Test the switch first by using the ‘soft-off’ button (middle one) – your Pi should go through the safe shutdown sequence. Once it has done that, use the ‘off’ button to cut power from the Pi altogether.
Finally, after a few seconds, try the ‘on’ button to make sure that works as well – you Pi should start up as normal.
If your switch isn’t behaving as expected, double check everything above. If still no joy, it may be due to any previous use your Pi has had. I had already used a similar switch setup on my Pi and I think the code was conflicting – a fresh Raspbian install fixed this for me (because I didn’t know how to undo my previous changes!)
Want one? Get your on/off switch from Pi-Supply.com
Come and talk to me on Twitter if you get stuck, and/or leave a comment below.