Hi,
There are several possibilities, each with its own cost and accuracy. An accurate one that I have applied successfully in my "Picaxe Satellite" project (
http://www.picaxeforum.co.uk/showthread.php?t=9236 ) is by using a MAX6018A Voltage Reference (1.2V output version) and connect it to a Picaxe ADC input.
The following code (written for a Picaxe-18X) then delivers the Vcc value in millivolts:
Code:
ReadBatteryVoltage:
REM Calculate Battery level (Vcc. in mV) using Vref (1263 mV at 25 degr. C) delivered by MAX6018
REM Only 8-bit ADC value is used, accuracy for calculated Vcc in the critical range
REM of 2.0V is approx. 10mV, for Vcc of 3.6 (maximum battery voltage) the error is approx 40mV.
REM Calculation formula: Vcc(mV) = 1263mV (=Vref) * (255 / ADC_Vref)
REM = (1263 * 51 * 5) / ADC_Vref
REM = (64413 / ADC_Vref) * 5
REM Note that 16 bits integer calculus is used using as large integers possible
REM before a division is done, in order to minimize round-off errors
SYMBOL ADC_Vref = b0
SYMBOL Vcc_Calculated = w1
SYMBOL Pin_Vref_MAX6018 = 1 REM Voltage reference at ADC 1 input!
LET Vcc_Calculated = 0
READADC Pin_Vref_MAX6018, ADC_Vref
LET Vcc_Calculated = 64413 / ADC_Vref
LET Vcc_Calculated = Vcc_Calculated * 5 REM Vcc calculated in mV's
RETURN
Note the way the integer arithmetic has been done in order to minimize errors; first multiply to as large numbers as possible within a 16-bit word before a division is done.
I particularly like the 1.2V Vref version of the MAX6018 because it functions at a Vcc down to 1.8V (which is about the limit of the Picaxes as well) and their power consumption is in the microamp area. Most other voltage references that I know of have both higher minimum Vcc as well as much higher power consumption, easily into the milliamps. Last but not least, the choice of Vref = 1.2V lin combination with 8-bit ADC eads during integer arithmetic to a very large multiplied value (64413) close to the 16-bit limit (65535) just before division, which implies a very accurate calculation given the restrictions of Picaxe integer arithmetic.
Note that the MAX6018 output needs to be grounded with a small capacitor for stable operation.
Hope this gives you some inspiration.
/Jurjen
EDIT: Small correction for room temperature operation (initially the application was aimed at very low temperatures with slightly lower Vref)