Raspberry Pi Battery Testing

Posted by Brandon on Apr 22, 2013

My name is Brandon, and I am a Product Analyst here at MCM.  I am fortunate enough to be one of the people involved with the Raspberry Pi product line.  Specifically, I get to test some of the new products and accessories that we bring in, and also determine product compatibility.  Recently, we were working on putting together a low power Raspberry Pi kit (which we now sell as part number 83-14949), focused around a Raspberry Pi model A and a rechargeable battery pack.  We wanted to determine how long the model A would run on a single charge of the battery, so I went about finding a way to test it. 

I have worked with programming a little bit in the past, so it was pretty easy to do a little searching on Bash and write a script to run a counter and output to a text file.  I have used this a couple of times on the Pi to get output to a text file, so I thought it would be a piece of cake.  I initially wrote a bash script to start when the Raspberry Pi started, and every second to increase a counter and output to a text file.  My thought was that once the battery died, I could just look at the last number in the text file, and that would be how many seconds the battery ran. The problem was that every time I ran the script the output was blank.  I eventually realized that in order for the Raspberry Pi to output the text the script had to be completed in order for it to properly write to the text file, so it was back to the drawing board.

Of course there are other ways to measure this, like the 72-10182 Tenma Voltage datalogger, but I wanted to figure it out with the Raspberry Pi. Eventually, this is what I came up with.

I decided to use one Raspberry Pi to do the counting and measuring (lets call this Pi A), which is connected to another Raspberry Pi (the bare board, Pi B) that is powered by the rechargeable battery. I connected the power lines of the bare Raspberry Pi to the GPIO input of Pi A.  Make sure to connect to the 3.3V power on Pi B, and not the 5V, since the GPIO connections on the Pi only operate at 3.3V, and I don't want to release any magic smoke from my Pi. Below is the script I wrote to accomplish the task.

echo none > /sys/class/leds/led0/trigger
echo 1 > /sys/class/leds/led0/brightness
declare -i timer
/usr/local/bin/gpio export 18 in
variable=$(cat /sys/class/gpio/gpio18/value)
while [ $variable = 0 ]; dosleep 1variable=$(cat /sys/class/gpio/gpio18/value)
echo "Going to sleep"
echo 0 > /sys/class/leds/led0/brightness
sleep 30
while [ $variable = 1 ]; doecho $timer >> /home/pi/output.txtsleep 1timer=$timer+1variable=$(cat /sys/class/gpio/gpio18/value)
echo 1 > /sys/class/leds/led0/brightness

The  first thing the script does is make it so the ACT LED of the Pi is no longer triggered by the SD card. You will notice that I set the timer to start with a value of 30 and put the Pi to sleep for 30 seconds. This is because during the boot process of Pi B the power output was fluctuating a couple of times, and it was causing the script to end prematurely.

To run the script, you have to create it in a text editor on the Pi, then give it execute permissions. I named the text file gpiotest.sh and had it in the Pi default folder, so I just did sudo chmod +x gpiotest.sh. I wanted the script to run automatically without requiring any input, and I didn't want to have to interact with it, so I set it to run at boot time. To do this, I added


to the startup file at /etc/init.d/rc.local. When the script starts, it turns on the ACT LED and starts monitoring the GPIO pin for a high condition, which will occur when Pi B is powered on. Then it turns the LED off and begins the counter. The script counts every second and spits the count out to a text file called output.txt. It does this continually until the GPIO goes low again, which means that the battery powering Pi B has died. When this happens the LED on Pi A lights up again, letting me know that the script is complete. Then I just go on Pi A and pull the output.txt file. The last count in the text file is the number of seconds that Pi B ran.

Now for a couple of pictures while it ran. Here is a picture of Pi A powered up and the ACT LED lit, waiting for Pi B to power on.

Now here is a picture after Pi B is powered on. Note that the ACT LED on Pi A is off.

When all was said and done, I found that the 3600mAh battery that I was testing with the model A (different test than the one pictured) gave me 19 hours, 11 minutes, and 10 seconds of runtime. This was with Pi B idle and nothing running. My next test will be testing while Pi B is under full load.



Add comment

Please register or log in to post a comment.