Reading A5 / Vmonitor - which power supply is connected (USB and/or battery) ?

Started by cbrandt, September 25, 2019, 10:18:20 AM

cbrandt

Hi all,

I've got a Moteino M0 powered by a Lipo battery, occasionnaly recharged by connecting the usb plug to a wall wart or computer.
I want to include a red/green led to indicate battery level, but only when powered by usb.

How could I know if the Moteino is powerd by usb ?

Felix

You already have the RED charging LED which should be ON if the battery is not full.
Otherwise if you read A5 (see schematic) you will see the voltage is about 1 schottky diode drop below 5V when the USB is plugged in.

cbrandt

Many thanks !
I was under the (wrong) impression that A5 measured only the battery voltage, regardless of USB connection.
Much clearer now!

Felix

I should have mentioned also that A5 is a voltage divider from the VIN (half ratio given by 2 1MEG resistors).
So the reading corresponds to 1/2 of the actual VIN voltage.

cbrandt

#4
So, do these readings seem correct ? (battery is kind of almost fully charged)

USB only: 4.7v
USB + batt: 4.2v
batt only: 3.8v

values obtained with this code:
float vin = analogRead (A5) * 2 * 0.003226; // 0.003226 = 3.3 / 1023


If my reasoning is correct,  I could differentiate between the 3 cases and light my leds accordingly:
'vin' above 4.5: USB only
'vin' between 4.0 and 4.5: USB and battery
'vin' below 4.0: battery only

Felix

Yes that is correct, however if your battery is full or near full, you could still be reading in the range of 4.15 or close to 4.2V, without USB.
When the battery is not near full then you can make that distinction more evidently.
Anyway the M0 allows you to at least tell when you're on USB power rather than battery power.

BTW this was an excellent question so I added a little section to the M0 page guide to clarify this and a bit of sample code.