LowPowerLab Forum

Hardware support => Moteino M0 => Topic started by: cbrandt on September 25, 2019, 10:18:20 AM

Title: Reading A5 / Vmonitor - which power supply is connected (USB and/or battery) ?
Post by: cbrandt on September 25, 2019, 10:18:20 AM
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 ?
Title: Re: which is the active power supply ?
Post by: Felix on September 25, 2019, 11:32:14 AM
You already have the RED charging LED which should be ON if the battery is not full.
Otherwise if you read A5 (see schematic (https://lowpowerlab.com/guide/moteino/moteinom0/)) you will see the voltage is about 1 schottky diode drop below 5V when the USB is plugged in.
Title: Re: which is the active power supply ?
Post by: cbrandt on September 25, 2019, 03:55:12 PM
Many thanks !
I was under the (wrong) impression that A5 measured only the battery voltage, regardless of USB connection.
Much clearer now!
Title: Re: which is the active power supply ?
Post by: Felix on September 25, 2019, 05:15:27 PM
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.
Title: Re: which is the active power supply ?
Post by: cbrandt on September 26, 2019, 02:53:44 PM
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
Title: Re: which is the active power supply ?
Post by: Felix on September 27, 2019, 09:53:20 AM
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 (https://lowpowerlab.com/guide/moteino/moteinom0/) to clarify this and a bit of sample code.