Average Ticker

How to Assemble the On/Off Power Switch from Pi Supply

How To Assemble The On/Off Power Switch From Pi SupplyThe completed switch from Pi-Supply.com

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 Switch
The On/Off switch from Pi Supply comes as a bag of parts to be soldered to the supplied PCB. 

Switch parts

The parts – let’s hope you’ve got enough solder!


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.

Switch PCB

The bare PCB with labelled sections


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:

Fit Resistors

Try and keep the gold bands the same way – makes it look tidier

Snip the ends then move on to the next step:
Snip component legs

Snip the legs after soldering – do this for each component


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:

Fit the transistors

Sit the transistors flush with the PCB

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.

Fit the Capacitors

Note the long capacitor leg to the left in this picture

Capacitor side view

From another angle, note the white stripe towards the edge of the board

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.

Fit the LED

Fit the LED with the short leg towards the transistors


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.

Fit the relay

The relay’s leg pattern means you can’t really go wrong here


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.

Fit the diodes

Fit the diodes with the white stripe to the left


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.

Fit switches and headers

The switches and header are an easy obvious fit

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.

Fit USB port

Solder all the legs of the USB connector for a solid fit


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:

 cd  


Then use the ‘mkdir’ command to make a new directory (folder), just as I have done below: (case sensitive)

 mkdir PiSupply  


To test that the dirctory has been created, use the following command and look for ‘PiSupply’:

 ls  


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:

 cd PiSupply  


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’:

 python /home/pi/PiSupply/softshut.py


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:

 sudo reboot  


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.

Not Working?

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.


Average Man

12 Comments on "How to Assemble the On/Off Power Switch from Pi Supply"

  1. David Allen | 01/07/2015 at 14:59 | Reply

    The board just wont work.I purchased two from pisupply and both have the same problem.When I press the power button the red led lights up and the pi begins to boot.Take my finger off the on button and the board “dies” immediately.I am at a loss because I really want this little gem to work.

    • Hi David. What model of Pi are you using? Have you tried adding the ‘&’ at the end of the ‘python /home/pi/PiSupply/softshut.py’ line?

      Another suggestion would be to start with a fresh copy of Raspbian with nothing else attached (as in GPIO add-on boards).

  2. Hi,

    I made ordered the hardware solder it all together.
    When i want to make the directory as told the problems begins.
    I running openelec RPi2.arm-5.95.2 XBMC) on my Pi2 and only the storage directory is writable.
    So i put the directory and script in the storage directory an wanted to edit the rc.local file. and yes another problem : ) because this file isn’t exits on openelec.
    So i can’t start the script automatically, maybe you can tell me how to solve this problem so i can use the on/off switch.

    • I’m not sure about OpenELEC, I haven’t used this with my media centre Pi. I remember trying something similar with RaspBMC, and it was a little different to Raspbian so caused a few confusions – I imagine you’re having similar issues. Try asking over on the PiSupply product page comments, they seem to reply to most. Or ask them on twitter.

  3. Hello,

    I just purchased this On/Off switch and even if it seems it’s working, I get a coloured square on the right top of my screen. Googleing a bit I found this is caused by a bad power supply.

    I was wondering if my switch is not correctly soldered or if it’s not prepared for the B+ model. Did you test the switch with a B+ model? Did you find similar issue?

    Best regards.

    • Hi Joseba. I haven’t tried this on any of my newer Pis (I don’t think) as I got this a long time ago. Have you tried changing the power supply that goes into the switch – maybe if you have an Android charger spare or something like that? I’ve not had that issue before though.

  4. JD FortWorth | 10/09/2015 at 19:25 | Reply

    Hi,

    Being a newbie to this whole Raspberry Pi computer thing it became apparent to me that if I unplugged and replugged the micro usb cable enough that something would get broken.

    So i bought one the pcb switches and am going to attempt to put it together.

    I do have a question, there don’t appear to be any holes with I could mount the pcb in an enclosure, is this right?

    If so do you have any suggestions on how the pcb could be secured in an enclosure?

    Thanks

    JD

    • Hi JD

      There are two holes on the PCB which I have used to mount the switch on a panel previously. It was a bit of a bodge…I just glued two nylon cheese head screws upside down to a panel, slotted on the switch and then added nuts to keep it in place.

      You could do the same, and an even better job, by drilling holes in a panel/case.

      Don’t forget though – you need those jumper wires connected to your GPIO to use all the features of the switch.

  5. Stephanie Switzer | 28/11/2017 at 21:13 | Reply

    I’m running ubuntu-mate on my RPi3B, booting it with Berryboot. When I did a search to the “rc.local” file it listed, two both with the same name. The difference is:

    One located in /media/sps/disk/etc contains:

    ———————————————————-
    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will “exit 0” on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.

    exit 0
    ————————————————————–

    The other, located at /media/sps/disk/etc/init.d contains:
    ————————————————————–
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides: rc.local
    # Required-Start: $all
    # Required-Stop:
    # Default-Start: 2 3 4 5
    # Default-Stop:
    # Short-Description: Run /etc/rc.local if it exist
    ### END INIT INFO

    PATH=/sbin:/usr/sbin:/bin:/usr/bin

    . /lib/init/vars.sh
    . /lib/lsb/init-functions

    do_start() {
    if [ -x /etc/rc.local ]; then
    [ “$VERBOSE” != no ] && log_begin_msg “Running local boot scripts (/etc/rc.local)”
    /etc/rc.local
    ES=$?
    [ “$VERBOSE” != no ] && log_end_msg $ES
    return $ES
    fi
    }

    case “$1” in
    start)
    do_start
    ;;
    restart|reload|force-reload)
    echo “Error: argument ‘$1’ not supported” >&2
    exit 3
    ;;
    stop|status)
    # No-op
    exit 0
    ;;
    *)
    echo “Usage: $0 start|stop” >&2
    exit 3
    ;;
    esac
    —————————————————————
    Which rc.local do I modify???

  6. Stephanie Switzer | 30/11/2017 at 15:03 | Reply

    Good idea. I’ll try that and see what happens. I don’t have anything of importance stored on it so if I trash the system all I’ll loose is time.

  7. Stephanie Switzer | 03/12/2017 at 18:35 | Reply

    Hello.

    Well I followed your great assembly and code instructions, connected my RPi3 and voila, it worked. This was my first project and my first go at soldering and although the soldering could be neater and burning my finger I’m really happy with the outcome. The one question I have is the the soft-off button function. Do I logout first then press it as the on and off buttons work properly.

Leave a comment

Your email address will not be published.


*


This website uses cookies. Please visit our privacy policy page for more information. Privacy policy

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close