It would be desirable to have an indicator on board to let the user know when the battery is low. Typically, to monitor the battery voltage you can run a wire from V+ of your battery to an analog input pin on your uC.
In this circuit however, there are two operating modes.
1) Running off of Vusb which results in a system Vdd of 3.3V
2) Running off the Vbatt which results in a system Vdd of 2.7V-3V
The digital value for a corresponding Analog voltage for an n bit A/D converter is:
(Vanalog * 2^n) / (Vrange) == ( Vanalog * 2^n) / (Vref+ - Vref-)
Where Vref+ is typically Vdd and Vref- is usually ground. So, when you’re running off of the battery you will have Vanalog = Vref+ which results in Vanalog*2^n / Vanalog == 2^n * (1) = 2^n
What that means is regardless of what the actual battery voltage is the digital value will always be 2^n.
So, i devised a circuit which takes advantage of the built in potential of a diode. How it works is the voltage the A/D converter will always be evaluating is 0.7V. But, as the Vdd decreases (due to the battery loosing charge) since the Vref+ == Vdd the voltage range the analog to digital converter has to cover will reduce.
For the following example the following assumptions are made:
1. Vref+ = Vbatt = Vdd
2. Vref- = GND
3. n = 10 (10 bit A/D)
If Vbatt = 3.3V:
0.7V*2^10 / (3.0V-0) = 217
If Vbatt = 3.0V:
0.7V*2^10 / (3.0V-0) = 238
If Vbatt = 2.7V:
0.7V*2^10 / (2.7V-0) = 265
If Vbatt = 2.5V:
0.7V*2^10 / (2.5-0) = 286
As you can see the lower the battery voltage gets the higher the digital value. The code running on the microcontroller should read the battery voltage on RA0 and if it’s greater then a certain threshold value then we say that the battery is low. Since lithium batteries have a in system voltage of about 2.7V but have a very flat discharge curve. I will probably set my threshold value to be somewhere around 275 which is in-between 2.7V and 2.5V. Threshold values are usually determined experimentally but our hand calculations provide us with a good starting point.
There is one small problem with this circuit. When the unit is attached to a computer via a usb cable then the system Vdd voltage is 3.3V. Which means that Vref+ = 3.3V and no longer will it equal Vbatt. Therefore, we no longer have anyway to detect whether or not we have a low battery when the unit is plugged into the usb port. To remedy this we would have to dedicate another analog i/o pin and connect it directly to the positive terminal of the battery.
There are two other ways this could be fixed. One way would be to use a buck regulator to boost the battery voltage to 3.3V which i decided not to do to reduce parts, complexity, and reduced noise in the circuit. The other way would be to tie Vref+ to Vbatt+. That way our little trick would always work. But, then any users that were planning on using the A/D converter wouldn’t have a constant voltage reference which would make their data dependent on battery voltage which isn’t good.
This isn’t that important since when the battery condition is most important is when we are powering it from the battery. So, when Vusb is detected we will not update the battery low pin.