i am building a standalone device with a seeeduino xiao. this is powered by a 3.7V lipo battery. i want to measure the voltage from the battery with this circuit via an analog input (A0).
analogRead(A0) always shows 1023 (even when the battery is almost empty).
can i implement this with this circuit like this? or does anyone have a tip on the best way to do this?
thanks in advance
The maximum analogue input into an I/O pin should be 3.3V and this should read 1023. More than 3.3V will show 3.3 but it could damage the input so it ceases to function correctly (or even destroys the processor). The trick to measure a higher voltage is to use a resistive potential divider - R1 in series with R2 connected to ground. So in this case, maybe R1 = 10k connected to the +ve of the battery, R2 = 47k connected to R1 and to ground, so the voltage at the junction between R1 and R2 = Vbat x R2/(R1+R2) = Vbat x 47/57 which is a maximum of 3.7 x 0.82 = 3.05 volts. This R1/R2 junction can then be connected to the processor pin.
The input impedance of the pins is fairly high so it should be fairly accurate. You can calibrate by checking the actual voltage with a multimeter and Serial.print the corresponding XIAO. I’ve used the same technique for measuring the voltage of a PV panel which can go up to 28V.
While posting, I thought I’d mention, in case you plan to do other things with the processor other than the components in your post, I recommend using another input pin than A0 as A0 is worth reserving in case you need a genuine analogue output - A0 being the only pin that offers this. For example, if you are trying to control a battery charging process.
Hope I’ve understood the problem correctly and that this helps!
I have 3 points of concern.
- When a XIAO is connected to a PC USB port, does this mean that a battery will be charged directly by USB 5V? If so, it is dangerous. It is recommended to connect a battery and a XIAO via a Schottky diode.
- Since 3.7V Li-PO battery can be charged up to 4.2V, it is recommended to set R2=33k.
- If a battery voltage drops below about 3.5V, the AD conversion value will be inaccurate.