Figuring Out When to Shutdown your Solar Powered Raspberry Pi – WeatherPi

solar power
The Slow Death Test on WeatherPi

Figuring Out When to Shutdown your Solar Powered Raspberry Pi – WeatherPi

What is WeatherPi?

SwitchDoc Labs is in the process of building a Solar Powered Raspberry Pi WeatherIMG_3171 Station.   The design will be released as a SwitchDoc Appnote, an Instructable and a series of posts on SwitchDoc.com.  We are right in the middle of finishing project and putting it outside.

In this article, we are going to discuss setting the threshold for shutting down the Raspberry Pi and then address the problem of powering down and up your Raspberry Pi.  In Solar Powered systems, this is called the “Brownout Problem”.  We will be showing how to use a simple device, the USB Power Control from SwitchDoc Labs to solve this problem.

Avoiding Brownouts for your Raspberry Pi

One of the most important issue in designing a Raspberry Pi Solar Power System is turning on and off.  The “Brownout Problem” is a real issue.  Why worry? If you have a long string of cloudy days, you may run your battery down. You can compensate for this in your design by adding more panels and more batteries, but that can get really expensive and your system might still run out of power, just a lot less frequently.

Our previous post talked in depth about the problem and how to solve it.  Now we are talking about how to set the parameters.

Setting the On and Off Threshold

On Threshold Voltage:  We set the power up threshold (by using the USB Power Control) at ~3.9V on the battery (unloaded voltage – after the Pi powers

USB PowerControl / SunAirPlus / Pi B+ Test Setup
USB PowerControl / SunAirPlus / Pi B+ Early Test Setup

up, it will drop a little bit.  That’s why the USB Power Control includes hysteresis — turn off at about ~3.4V, turn on at ~3.9V).  We just connect the input line on the USB Power Control to the output of the Battery on the Solar Power system (controlled by SunAirPlus – which conveniently has a pin for just this reason connected to the LiPo battery + terminal).  You CAN’T use the input to the Raspberry Pi to trigger from.  Modern Solar Power controllers (SunAirPlus included) will try to keep the 5V going until the last possible instant and then give up and crash the voltage – not allowing enough time to shutdown the Raspberry Pi safely.  We have to look at the battery voltage.

Off Threshold Voltage:  To figure out the Off Threshold Voltage, we need to run a test.  SunAirPlus includes an INA3221 3 channel voltage/current monitor so we have all the needed data.  Battery, solar cell and load voltage and current.  We need to see when the loaded system (SunAirPlus powering the Raspberry Pi and peripherals).  You have to look at the loaded system to see how your system behaves.  We installed our RasPiConnect software to see the data and then wrote software on the Raspberry Pi to read the INA3221 (i2C) on SunAirPlus and store all of the information on a MySQL database, read and display it using MatPlotLib and then pushing it out to our iPad to look at it using RasPiConnect.

We charge the battery all the way up with an external power supply (connected to the Solar Panel inputs on SunAirPlus), then turn the power supply off and let the system run all the way down until it quits.   NOTE:  This is not a good thing to do in general.  Raspberry Pi’s need to be shutdown nicely because you can corrupt your SD Card – see below.  In this case we need to see the results, so we took the risk (after backing up the card!).  Here are the results from RasPiConnect (Point 2 should be “Off” not “Of”!):

solar power
The Slow Death Test on WeatherPi

The Raspberry Pi crashes at about 3.45V on the LiPo Battery.  And then a little later (about ~3.2V – we can’t see the voltage since the Raspberry Pi has now crashed) USB Power Control turns off the power to the Pi.  Note: Even when shutdown, the Raspberry Pi still draws around ~100ma, so you should turn the power off to allow the solar panels recover your battery more quickly.

So, now we know the off threshold for our system.  3.45V.   We will use a value of 3.5V in our control software:

def rebootPi(why):
    pclogging.log(pclogging.INFO, __name__, "Pi Rebooting: %s" % why)
    os.system("sudo shutdown -r now")

And the lines in our main program loop (executed every 5 minutes) – battery voltage comes from SunAirPlus:

if (batteryVoltage < 3.5):
    print "--->>>>Time to Shutdown<<<<---"
    shutdownPi("low voltage shutdown")

And now our WeatherPi software will shutdown safely before it runs out of power and the USB Power Control turns the power off shortly thereafter.  We tested it and it works perfectly.

Turning Back On

Who turns the Raspberry Pi back on when the battery recovers (~3.9V)?  The USB Power Control (off at ~3.3V on at ~3.9V – Hysteresis) turns the Pi back on as in point 3 above.  We have a much more reliable system now.

Note that it looks like the USB Power Control actually turned the Raspberry Pi back on at about 3.8V.  That is the LOADED voltage on the battery when the Raspberry Pi is on.  The battery voltage drops about ~0.1V when the Raspberry Pi turns on and that is exactly why you need hysteresis.  If you turn on and off at a single voltage, you will ping-pong across the voltage and corrupt your SD Card and kill your Raspberry Pi based system.   This is what happened to Project Curacao when the battery watchdog Arduino failed.

Conclusion

Working through the issues of a solar powered raspberry pi system should include how to handle the inevitable power outages.   This solves the “Brownout” problem for Weather Pi.