Mobile Power

A couple of the projects I have been working on recently have a requirement to run from a portable power supply. Now finding a power supply that can power the Pi is quite a simple task there are a large number out there to chose from and each have their own merits, but as usual I wanted something that was readily available in as many places as possible.

My final choice was the Adafruit PowerBoost 500C as it has a built in charging circuit to enable the device to be charged over night, I have since learnt that there is now a 1000C out there should you need a little more power.

So some basic info on the 500C mainly taken from the Adafruit site for reference

  • Synchronous operation means you can disconnect the output completely by connecting the ENable pin to ground. This will completely turn off the output
  • 2A internal switch (~2.5A peak limiting) means you can get 500mA+ from a 3.7V LiPoly/LiIon battery. We had no problem drawing 1000mA, just make sure your battery can handle it!
  • Low battery indicator LED lights up red when the voltage dips below 3.2V, optimized for LiPo/LiIon battery usage
  • Onboard 500mA charge-rate ‘iOS’ data resistors. Solder in the USB connector and you can plug in any iPhone or iPod for 500mA charge rate. Not suggested for large iPads.
  • Full breakout for battery in, control pins and power out
  • 90%+ operating efficiency in most cases (see datasheet for efficiency graphs), and low quiescent current: 5mA when enabled and power LED is on, 20uA when disabled (power and low batt LED are off)

Power Pins

There’s three power voltages, the USB input for charging the battery (4.75-5.25V whatever is coming out of the USB port), the battery itself (3-4.2V) and the output (5-5.2V)

  • USB – this is the micro USB 5V power pin. It’s the pin that is used to charge the battery, NOT the output power! You can use this if you want to grab power from the microUSB port when it is plugged in
  • GND – this is the power ground. This boost converter is not ‘isolated’ – the ground input is the same as the ground output
  • BAT – this is the battery input, connected directly to the JST connector. For most Lithium batteries, this will range from 3.0V when near-dead to 4.2V when fully-charged. Higher voltages will let you draw more current and in general, are more efficient. Try to keep the wires going to this pin nice and short – 3″ or less is best!
  • 5V – this is the boosted output. When the board is running, the voltage will be 5.2V approximately. It may dip down to 5V as the current draw starts to go up (over 500mA). When the board is disabled, this output is ‘floating’ but you should still try not to apply a voltage to it while the board is disabled. There’s a green LED connected to this pin which will let you know when there’s power output

Control Pins

There’s two ‘control’ pins.

  • EN – this is the ‘enable’ pin. By default it is pulled ‘high’ to VBAT. To turn off the booster, connect this pin to ground. The switch can be as small as you like, it is just a signal. Contrast this to an inline power switch which would have to be able to handle up to 2A of current! When the chip is disabled the output is completely disconnected from the input.
  • LBO – not a leveraged buy out! this is the Low Battery Output. By default it is pulled high to BAT but when the charger detects a low voltage (under 3.2V) the pin will drop down to 0V. You can use this to signal when its time to shut down or alert the user that the battery is low. There is also a red LED connected to this pin.

LEDs

There are four onboard LEDs.

  • The Blue LED sits next to the USB connector socket, and indicates the 5V output power state.
  • The Red LED is next to the battery JST port and indicates when the battery voltage is below 3.2VDC (Low Battery Output)
  • The Yellow LED is next to the microUSB connector and indicates when the battery is being charged
  • The Green LED is also next to the microUSB connector and indicates when the battery is done charging (all full)

It comes without the USB connector soldered onto the output side, but other than that connect up your LiPo/LiIon battery to either the JST connector or the positive (+) BAT pin and the negative (-) to a GND and away you go power to your device is ready to go.

On/Off Switch

The PowerBoost 500C does not come with an on/off switch for the output, but its very easy to add one.

We will be turning the boost on/off via the ENABLE pin, so the switch does not have to carry any power, it is only signalling to the boost converter what to do. This means the switch can be small. Use any switch you like, if it is 0.1″ spacing thats ideal. If your switch only has two pins instead of three, tie one pin to GND and the other pin to EN – when the switch is closed, the power will turn off.

All fine and nice if your device is not headless and you can run any shutdown commands you want to before flicking the switch, so how do we get around that?

We have to tie it back into the Pi itself and use the Pi to signal back to the 500C once the shutdown has been completed via the EN pin and cut power thus saving the battery. I’m going to use the Pi Shutdown Button code that I hacked together, to control the shutdown of the Pi and modify it slightly to enable me to send a signal over to the 500C to kill the power and save the battery

#! /usr/bin/python
#pipower.py
import time
import os
import RPi.GPIO as GPIO

#set the signal pin high
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)

#the shutdown script
GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
 
def Shutdown(channel):
 os.system("sudo reboot")
 
GPIO.add_event_detect(20, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
 
while 1:
 time.sleep(1)

Notice that here we are using a reboot not a shutdown command, the reason for this is, as part of the reboot the GPIO pins are reinitialized, giving us the pull to GND that we need to signal to the 500C 😉

If you wire this up right now your Pi is not even going to boot! there is a little more work to do this time, we need to add a switch to control the On/Off state of the device and a diode to protect the Pi from the battery

Notice that I used On/Off there? as unlike the Pi Shutdown Button where the device would be left in a halted state this is going to turn the device off once the shutdown is compete, only to be powered back on with human intervention.

The circuit diagram looks something like this;

pipower_diagramWhere GND, BAT, EN are connections on the 500C and GPIO 20 and GPIO 21 are connected to the Pi.

In it’s drawn state the device will be powered on once the boot sequence has completed and the script above is running GPIO21 will have been pulled High, flicking the switch removes the connection between BAT and EN but the signal is maintained from GPIO21 the other pole of the switch in turn causes GPIO20 to be pulled to GND forcing the pin low and initiating the os.system("sudo reboot") in the code causing the Pi to halt all running applications and reboot, as part of the reboot process GPIO21 will get reinitialized pulling it to 0 then signalling to the EN of the 500C to powerdown. At this point shutdown and power off are complete.

Flicking the switch back again makes a connection between BAT and EN of the 500C causing it to power up, and the Pi boots as one would expect when supplying it with power, with the diode stopping GPIO21 being forced High at this point, once boot is complete and pipower.py has been called turning the switch to the off position will cause a graceful shutdown and power off, prior to that it’s just the same as pulling the power plug!

Now we want to get pipower.py to run every time the Pi is booted up so that it’s sitting there waiting for the signal to shutdown your device, for this we need to create a shell script that will start the Python script with root access. Put the shell script in the same directory as your pipower.py script.

save the following as pipower.sh

#!/bin/sh
#pipower.sh
cd /
cd home/pi
sudo python pipower.py

Lets test the shell script

sudo chmod 755 /home/pi/pipower.sh

To make the script executable then

sudo sh /home/pi/pipower.sh

To run it, flick your switch and your Pi should begin it’s shutdown process and then power off.

Now we are going to use crontab to autostart the script. You are going to want to open the crontab editor under the root user by using the following command;

sudo crontab -e

Append the following line to the end of the file

@reboot sh /home/pi/pipower.sh

Thats about it your done, enjoy and stay safe

UM

You may also like...

15 Responses

  1. Mark says:

    Hi, nice article! 2 questions: What size/type diode to you recommend using to protect the GPIO? Also, does the switch need to be able to handle high current if the battery is passing through the switch? Thank you!

    • uncle-muddy says:

      @Mark sorry for the delay in getting back to you….

      The switch doesn’t need to handle anything over a couple of Amps @ 5v DC I simply grabbed one of these and have had no problems since. As with the diode I just grabbed what I had to hand on the bench and stuck it in there (maybe not the best approach) all I wanted to do was stop the flow back into the Pi, which it does nicely.

      UM

  2. Brijesh says:

    Hello UM,

    Thanks a lot for an excellent solution. This was really very helpful for the people like me, who are relatively new to electronics and raspberry pi world. I am planning to implement this to my raspberry pi zero but i have a doubt as i lack a knowledge of electronics. I would request you to kindly provide the details of the switch (part no. or manufacture’s no.) so that i can buy that online or in any local shop.
    I googled but got so many options of DPDT switch and got confused, some most of them are 3 pin but your design is showing 4 pins.
    Please guide, eagerly waiting for your reply.
    Thanks and Regards
    Brij.

    • uncle-muddy says:

      @Brij – The switch is a simple DPDT that has 6 pins on it, I only show 4 in the diagram as that is all I use. Something similar to this should be easy to get hold of in your location and will do the job nicely

      UM

  3. Jason George says:

    This is simple in its brilliance. Infinitely more elegant than all the delay capacitor type circuits or external board solutions that don’t nicely integrate with the powerboost. I tip my hat, sir. Is it safe to say that the diode can be a schottky barrier diode, or perhaps even an LED?

  4. Woo says:

    Awesome, but, as i am an electronics noob, do you perhaps have any clue as how tot wire a switch like this?
    https://www.adafruit.com/products/482

  5. Jamie Raadt says:

    Hi there, and thanks for posting this!
    Simple though it may be, I seem to be having trouble with the implementation. I’m fairly certain I built the circuit correctly, and yet as soon as it goes to reboot, the power doesn’t cut out, and it just performs a normal reboot. Just wondering if you, or anyone else has run into this issue.

    Again, thanks, and take care.

    • uncle-muddy says:

      @jamie couple of things to check…

      Are you using reboot in your script and not shutdown?

      Is GPIO21 pulled to 0v during the cycle?

      • Jamie says:

        Definitely using reboot instead of shutdown, but the gpio pin doesn’t seem to be going low on reboot. Not sure why, because the gpio should be reinitializing during reboot! There is an error message that prints to the console just before it shuts down, so if I can figure out what that says, in sure it’ll help.

  6. uncle-muddy says:

    I noticed the other day whilst working on something else that there is now a “poweroff” command, I need to look into this a little deeper and see what happens to the GPIO pins when you issue that command as by the looks of things the “reboot” command no longer pulls the pins to GND which is why this has stopped working.

    • Pat says:

      Hi Uncle Muddy,

      Firstly thanks for putting this all together. Have gotten everything working except the last part that seems to be the issue with the pin not pulling down. Did you ever get this sorted? Thanks!

      • uncle-muddy says:

        I’ve not had anytime to look into this, but Cal did say there was an option to add a dtoverlay option that forces the pin low during reboot which should have the same effect. If I get chance I will test it but I have very little spare time at the moment,

  7. Cal says:

    Just as an FYI, I think this setup will still work, as there is now the option to add a dtoverlay line to your boot/config.txt file: dtoverlay=gpio-poweroff,gpiopin-##,active-low=”y”
    This will force the pin low once shutdown is complete.

    Thank you for the dead simple implementation of a safe shutdown!

Leave a Reply

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