Pi2RMini – a proper power switch for the RaspberryPi

This text specifically refers to the v1.0 boards. The files are all available on GitHub.

In order to make my RasPi MPD server more “living room” (read as you will…) and user friendly, it really needs to have a proper controlled power on/off switch. Additionally, it’s not good for the integrity of the SD card to have a slow switch off, which you get from turning off a normal supply or from trying to pull the lead out. Ideally, the system should be shutdown in software first, and then have the power removed. There are a two other projects which appear to do the same thing (by no means exhaustive!), but they also end up with more cables. Everyone using a RasPi surely already has too many cables..!

In order to do both for a small system, the Pi2RMini fits onto the GPIO header directly. There’s a switch and bicolour status LED onboard to control, so there’s no need for any wires if you don’t want them. Power connections are the usual micro USB B, or provision for a direct input potentially from a higher power supply. The power input can by fused with a resettable/non-resettable fuse depending on preference. Power to the RasPi is through a P-channel MOSFET acting as a switch, so only dissipating P = IRPi2Rds from a small SOT-23. The N-channel MOSFET is there to ensure the gate of the P-channel is switched fully off at 5V, rather than the 3.3V the microcontroller is at. As the board powers the GPIO header directly, it’s prudent to fit a polyfuse in F1 so there is some protection for the RasPi.

The board could be smaller, however it I wanted to keep it reasonably sized so people could build it themselves, and also it provides some mechanical support with the screw, useful if the power flex is heavy. The standard 6-pin AVR programming header is in the centre. If you are mounted in a case, the switch and bicolour LED are broken out on a separate header. The board is very easy to build up (trickiest is the USB header), and would actually be a good first SMD project. It’s also single layer, so you can etch it easily at home. I’ve included a correctly scaled PDF of the copper.

On the RasPi side, there’s a small script that runs in the background and checks for signals on the GPIO 7 pin and communicates (very simply) back to the Pi2RMini. On power up, the Pi2RMini will wait 20 seconds for a response from the RasPi; if this is not met, it will go into error mode (LED flashing red) awaiting a hard reset. When a signal to shutdown is received from the Pi2RMini, the script issues

sudo shutdown -h now

to shut the system down cleanly. The Pi2RMini will pause for 20 seconds after receiving acknowledgement of the shutdown signal and then switch off power to the RasPi. If no confirmation is received from the RasPi, the Pi2RMini will go into error mode (LED flashing red), and will require a complete reset. Pushing the button when the system is on for longer than 4 s (the LED will flash green then red continuously) will lead to a reset, and cut off power completely and instantly. It’s important to note, that this is less likely to corrupt your SD card than simply pulling the cord out, as the MOSFET switch-off is very quick. All the timings are easily altered in the firmware and flashed to the board. The code (I think!) is well commented, so it should be fairly obvious. There is approximately 47% of the flash space left on the ATTiny13, so there’s plenty of room for modifications – it would be great to see some!

To get the board up and running, you will need to run the following commands from your home directory:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install github
git clone git://git.drogon.net/wiringPi
cd wiringPi
cd ~
wget https://raw.github.com/awjlogan/Pi2RMini/master/pi2rmonitor
sudo chmod 755 pi2rmonitor

In order, this updates your RasPi and installs GitHub (steps 1-3), then installs wiringPi (steps 4-6), then gets the RasPi script for the Pi2RMini and changes it to executable. Finally, you need to add the following line to /etc/rc.local just before the final line (exit 0). This will start up the script when you first start your RasPi, so you don’t have to start the script up manually each time.

(cd /home/pi && exec ./pi2rmonitor) &

GitHub contents:

  • Changes.txt contains a summary of completed and future changes
  • Pi2RMini v1.0.brd Layout in Eagle format, v6.1.0
  • Pi2RMini v1.0.sch Schematic in Eagle format, v6.1.0
  • Pi2RMini v1.0.pdf Processed and properly scaled PDF for those wanting to etch their own boards. Not mirrored.
  • bom.xlsx full BOM, including Farnell part codes. These are for 100 quantities, approximately multiply by 2.5 to get the unit price. Total price is quoted for the Farnell parts, inclusive and exclusive of VAT. Not up to date with latest prices, only up to last modified date.
  • Pi2RMini.c Written in AVR GCC using AVR Studio v5. The compiled release binary is pi2rmini.elf and pi2rmini.hex

Released as free, open-source hardware. All the files are available to download from GitHub. Free to use, modify, and distribute. I will be offering these as the v1.1 board with some modifications as both kits and fully constructed in the near future. This will, of course, also be free, open-source hardware. Please subscribe to be kept up to date!


9 thoughts on “Pi2RMini – a proper power switch for the RaspberryPi

  1. Gus, I have 2 questions about your Pi2RMini project:
    1) ever thought about to extend your project to a safe shutdown at powerfails? I have seen many websites but found no really good solution (besides an UPS for same money as the RPi itselfs 😉 …. Adding some goldcaps and a trigger of failing power …
    2) is a pcb available?

    thanks Martin

    • Hi Martin – I hadn’t looked into making it a UPS, but really it wouldn’t be too difficult. Without running the numbers quickly, I would think that most of the backup caps are too small (even 1 F ones) to give you enough time to shut down. However, a reasonably sized Li ion/poly battery with a boost converter would do the trick. Cost wise, the biggest cost is in the battery. Definitely worth looking in to – I’ve got a couple of weeks of non-fun travel coming up, so I can have a quick go. Re PCBs, if you send me an email (awjlogan ^at^ gmail.com) or other direct message, I can send you a completed v2 board.

    • I had a little think about the UPS option. It would actually be very easy to do. A capacitor wouldn’t work for a Pi, assuming it runs about 2.5 W (500 mA, 5 V). A typical 1 F capacitor at 5 V has only 25 J of energy (Ec = 0.5*C*V^2), so would only last 10 s, which isn’t long enough for the RPi to shut down. However, a small sealed lead acid battery would be perfect: cheap and slightly higher voltage to be stepped down. I’d be happy to put together a small design for that, if you’re interested.

  2. I am also thinking about a “Super capacitor backup”.
    But I’m thinking 10F ($10 @ ebay) or perhaps 100F ($20 @ ebay) since a capacitor is maintenance free as far as I know.

    • Still not a lot of time (approx. 15 mins) for a UPS at 100 F. Electronically, that’s the simplest method (although you’d probably have to limit the inrush current) but you’re paying 2/3rds the price of the Pi for limited protection and crucially not signalling to the Pi something’s wrong.

  3. Dear awjlogan
    Thanks for your project. I was very interested by your project (at least, adapt it to mine), but it seems that your sources are on available on github anymore…
    Is there a way to obtain it, and make my own power switch (others exist, but it seems that this one is the only open source one) ?
    Thanks !

    • Hi Thibault – I didn’t know this was still being viewed, I’ve just reuploaded the files to the hold location. Can’t guarantee they work (it was nearly 4 years ago), but let me know if you need any help. 🙂

      • Thank you very much for the files. I was interested by your schematic, at least by the way to power the Raspberry pi (the idea is to adapt it to my ATtinyRaspberry already in my project). Does your BSL215 and LD6806 able to power the Raspberry Pi 3 (<3A), plus a LCD display (700mA), some Leds and RGB Leds and (<1A) ?? Or maybe can you recommand something else ?

    • Hi Thibault – glad it was of use. The LD6806 only powers the microcontroller, so that’s fine. The BSL215 is rated at 1.5 A absolute maximum, so you wouldn’t be able to run your setup with that. You can replace the N-channel MOSFET with a jelly bean part (BS170, 2N7000 etc). For the P-channel, something like an NTF6P02T3 would do the job nicely. Alternatively, you could use 3x BSL215 and have one for the Pi (which is a lot less than 3 A), one for the LCD, and one for the LEDs. If you do that, put a small resistor in series with each gate, maybe 100R, just to stop any ringing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s