Reading the powerlevel from inside your program

I have a project on my Panda II powered by a standard 9-V battery. Does anybody have any suggestions on how I might test the battery level from within my program? For example I would like to know when the voltage drops below a certain threshold and turn on or flash a diode or something.


use an analog input with a voltage divided to get the voltage less than 3.3 volts.

Thanks for your quick reply, however my question is simply around how to measure the power level from my running program.


Mike’s answer says that though. Use an AnalogIn pin, that is connected to your battery via the voltage divider (that MUST get the voltage down to a maximum of 3v3) and monitor that. When it gets below a certain threshold number, flash your LED.

Apologies but I am new to the electronics side of things so if you wouldn’t mind going a little slower for me ,

[quote]connected to your battery via the voltage divider (that MUST get the voltage down to a maximum of 3v3)

Excuse my ignorance but is this voltage divider on the Panda II already or are you talking about something I have to acquire? And by “MUST” are saying I have do something special to get the input down to 3v3 (i.e.; some circuitry), or this will happen automatically?

Let’s say my project requires 6-9V to run. I want to flash my LED when it drops below 7V. So how does what you are saying work for this scenario?


You need to do it… ALL. big grin.

You need to take the PWR wire from your battery and wire in a battery divider, which is simply two resistors, and have the divided power connected to an AnalogIn connector. See You need to pick the resistors such that your 9v battery will only ever show a max of 3v3 on the fez pin. Approximately, if using a 9v battery source, that’s a 100k resistor and a 58k resistor. In that scenario, when the battery is full the voltage showing on the AnalogIn pin is 3.3volts, or 1024 when you read it in code, and when the voltage drops to about 6v, the reading on the pin is 2.2v and in code is about 680 (ignoring the ability to scale the values).

Added: The example values for resistor values were just examples. There must be a theory that says whether it’s better to use bigger or smaller starting values (meg-ohm vs tens of ohms) but I don’t know the answer, so that would be worthwhile looking more at from an electronics perspective (come back and tell us the answer when you find it :))

As for monitoring the voltage, depending on the battery chemistry and discharge profile, and the power you’re using with your project, will vary how often you need to poll the battery. I would think that at your “full” state, you probably only need to check every minute. As the battery voltage gets to 7 you might want to check say every 30 secs, and as it gets lower you will want to check more often too, and ramp up the alerting to the end user (green flashing LED when all is well, slow red flashing LED when at your first threshold, rapidly flashing when it gets real bad, flash red/green when you think the brick wall is about to be hit etc). Obviously you don’t want to make things worse in your monitoring so you have to set some kind of realistic level that you’d never check more often than a specific time or flash LEDs to use more power than the rest of the circuit etc.

Thanks Brett!

I think I get what you are saying. So basically you have to “scale” (i.e.; divide) the voltage to the 0-3v3 range. This is the kind of pointer I was looking for. Thanks.

(I can see this hardware stuff is going to be a back to school effort for me…).

One thing to consider is that setting up this voltage divider like this will guarantee that your battery drains all the time (through the divider) whether or not it’s in use by the Panda II. Ideally, there’d be some way to “turn off” this connection to the voltage divider when you’re not measuring the battery voltage.

A MOSFET could probably accomplish that.

As long as you use large resistances (around a Mohm are high enough and easy to find) the power consumption of the voltage divider should be pretty insignificant compared to the power consumption of the panda. I = V/R = 7V / 1Mohm = 7uA for the voltage divider, compared to ~100mA that the panda uses.

Hum, 1Mohm probably much too high regarding the ADC input impedance.

Yes power conservation is a consideration here and is not lost on me.

Thanks for your comments…much appreciated.


The voltage divider is going to draw current night and day, whether or not the Panda is running, sleeping, or even off.

If you’re using a 9V battery, there’s very little to spare…

You better take notice of Barbudor or you will increase the amount of noise on the ADC input.


Godefroy said :

Hum, maybe he can connect the divider after the power switch, which will also prevent sending a voltage on USBizi while it is not powered…