Project Curacao – The Battery Watchdog (Arduino)
NOTE: The Solar Panel Charger Controller in this project has been replaced by a 3rd Generation Controller by SwitchDoc Labs called SunAir and SunAirPlus.
The Battery Watchdog
After a significant period of testing, we have determined that the Raspberry Pi does not wake up very well from a discharged battery event as it is being recharged by the solar cells. What appears to be happening is the battery reaches a high enough voltage and the charger turns the power on to the Raspberry Pi. The Raspberry Pi Model A takes around 300mA and immediately pulls the battery back down to the point where the Raspberry Pi will not boot. This kind of constant boot/reboot cycle can kill SD cards after a period of time. After some thought, we determined that we could use a low power Arduino Uno with a relay as a very smart watchdog timer. This device has its own small LiPo battery, a small solar cell and it’s own SunAirPlus charger. We want this device to be independent of the main system. It will do the following four things:
1) Monitor the main system battery voltage and turn the relay on to the main system when the batteries reach 20% charge. This is in the case of a full battery discharge rate. There will be a 5% of charge hysterisis to prevent flapping. This meaning that the relay will not turn off until a 15% charge rate has been reached.
2) Monitor a GPIO pin from the Raspberry Pi, looking for transitions at least every 15 minutes. If the Raspberry Pi does not change the state of this pin every 15 minutes, the Battery Watchdog attempts to shutdown the Raspberry Pi and then cuts power for 10 seconds and re-applys the power to reboot the Raspberry Pi.
3) It will start the Raspberry Pi up approximately 3 hours before sunrise and shut the Raspberry Pi down approximately three hours after sundown. Sunrise and sundown will be approximated by the use of a photoresistor with enough hysterisis to not mistake a cloud for the sunset. It will also bring the Raspberry Pi up in the middle of the night for 30 minutes to record night time temperatures. If the main battery is fully charged, the Battery Watchdog will cancel the night time shutdown.
4) Additional GPIO pins on the Raspberry Pi will be used by the Battery Watchdog to communicate status and events through a linked serial interface. This will also be used to allow the Raspberry Pi to shut itself off in case of high temperatures.
This entire watchdog could be done in hardware with an FPGA board, but we decided to go with the parts we had on hand. A good question we have yet to answer is whether to make the power relay normally open or normally closed. If it is normally open and the Battery Watchdog fails, then the Raspberry Pi is out of action. If it is normally closed and the Watchdog fails, then the Raspberry Pi might be able to still function.
For a system designed to operate 3700 miles away from the engineer who has to fix it, a good watchdog function is critical.
Implementation
We have implemented the battery watchdog by using an Arduino Mega 2650. We used a Mega because it has 8K SRAM and we did not want to worry about running out of SRAM. Yes, it’s lazy, but quicker. We will probably run out anyway!
Arduino Battery Watchdog i2c Mappings
i2c address | Device | Function |
---|---|---|
0x40 | SunAirPlus | INA3221 |
0x5C | AMS 2315 | Outside Temp/Humidity |
0x68 | RTC1307 | Real Time Clock |
Arduino Battery Watchdog GPIO Mappings
GPIO | Function | Comments |
---|---|---|
22 | Interrupt Pi | Level |
27 | Pi Latched Relay Reset | Pulse |
29 | Pi Latched Relay Set | Pulse |
35 | LED Pi Interrupt On | Arduino Battery Voltage/Current |
41 | Select Wind Set | Wind Turbine |
43 | Select Solar Reset | Solar Panels |
Arduino Battery Watchdog A/D Mappings
Channel | Function |
---|---|
A3 | Pi Battery Voltage |
A5 | Regulated Wind Voltage |
A7 | Unregulated Wind Voltage |
A9 | Pi Solar Voltage |
Software
The Battery Watch Arduino software has expanded to about 3000 lines of code. There are certainly bugs in this software. One of the unresolved issues about the architecture of Project Curacao is that the software on the Raspberry Pi can be easily updated remotely across the Internet while the software on the Battery Watchdog Arduino can not be updated. This is because there is no current way of updating the Arduino without plugging in a USB serial port. While we are investigating options (different boot loader, figuring out a way of using the USB port on the Raspberry Pi for programming) we are looking at connecting the Raspberry Pi to make our system just a bit more resilient than it currently is.
To partially address this problem we are going to build what is called a “Dead Man Switch” in software. If the Raspberry Pi has been off for two days, then the Battery Watchdog will turn it on. This helps us recover from many types of errors in the Battery Watchdog software.
The software is written in the standard Arduino IDE. All of the code used in this article is posted on GitHub at github.com/projectcuracao.
Hi
Relay is a bit of a waste of power (unless you use a power-saving circuit*)
Use a P-channel FET (without “body diode”) instead. It’ll also protect against reverse connection, and probably cheaper than the relay!
*Power saving: Relays need much more power to activate than to hold. Thus, a capacitor across the relay to latch it, and a series resistor to hold it should save half a watt…
Try 470 mucrofarad, 47 ohm – dunno what relay you’ve got, but…
Andy,
Excellent suggestion about relays. We were burning a bunch of power in relay holding current, so after this article was written (see the update in https://www.switchdoc.com/project-curacao-camera-subsystem-part-5/ ), we replaced them with latching relays that consume no power after they are switched. We used https://shop.ciseco.co.uk/3v-to-5v-bistable-latching-relay-kit/
“This is because there is no current way of updating the Arduino without plugging in a USB serial port.”
Yep, there is.
Have a look at spellfoundry’s sleepy Pi.
https://spellfoundry.com/
Ferkle around a bit on that page, and eventually you’ll find detailed instructions how to program an Arduino from the Pi’s GPIO pins.
Remember:
“It ain’t ‘ard! It’s an Arduino!”
BR
-Andy, Oulu, Finland.
Andy,
The sleepy Pi was not available yet when this box was designed (in August – December of 2013), but it is an excellent alternative architecture.
I’m going back down to Curacao in September to upgrade the box and I’ll check this out and see if I could upgrade my box to do the programming from the Pi’s GPIO pins.
BR
John, Coeur d’Alene, Idaho