Battery level


I’m trying to detect the battery level that is connected to the imp through the mini-USB, using the following sample code:

function getVoltage() {
local voltage = hardware.voltage();
return voltage;

function sample() {
server.log("The voltage is: " + getPinVoltage());
imp.wakeup(3.0, sample);

// Start sampling the voltage on Pin 9

It returns 3.269 when connected to the laptop and 3.276 when connected to the battery. This is just the April board. So I think it is being regulated. Is there a way to get the actual value?
At least how can know that the imp is operating on low battery using this?


That’s not really going to work. The april is regulating it down to ~3.3v (hence what you see).

You could add a resistive divider to measure the USB 5v input… but that’s not going to work either, because USB battery packs will be boosting their internal 3.5-4.2v-ish battery to 5v, and this 5v will stay stable for the entire battery range.

Thanks Hugo,

Will it work if I connect the battery in a different way?

The most efficient way is to connect a battery & charger direct to the april board, and add a voltage sense divider network. Not so easy to do without a PCB, though.

The easiest solution may just be to use a gas gauge chip (so you don’t have to prototype the paths) and a li-ion pack + charger eg using this:

Oh I see. I will check the second solution then. Thanks!