MAX6018 battery voltage reference math

NXTreme

Senior Member
I'm working on the basics of a weather monitor ("O noes!", I hear you scream...) that, for now, will be powered by three AA batteries. I will eventually convert it to solar power but I'm waiting to put an order in to SparkFun that will have the necessary parts. So, for now, I'm powering it off of the AAs and I want to figure out how long it'd last on one change of batteries. I bought two MAX6018B V. Ref. chips, to be able to accurately monitor the battery voltage. However, I'm having problems with the math... :D.

With a quick search (there's a search bar? :eek:)I found a post by kranenborg with the math necessary to convert 8 bit ADC readings to a mV voltage level. However, he used the 1.2V version and I'm using the 2V version of the V Ref. chip. I know, I probably should have gotten the 1.2V but I didn't and it'd take a couple weeks (if not months) for a package to arrive...

Anyways, I changed the math around somewhat to give me a somewhat accurate reading (within ~100 mV), but it's not accurate enough for me :p. And, I can't wrap my head around it well enough to change it to my liking. I'd like it accurate within 30-40 mV like the original code, even if it means using the 10 bit ADC and a word variable to store the value in.

This is my modified code.

Code:
	#rem
	Original program by kranenborg @ http://www.picaxeforum.co.uk/showpost.php?p=95202&postcount=3
	Modified to make it easier for me to read... and to work with a 2048 mV reference instead of 1263 mV.
	
	Original text: "Calculate Battery level (Vcc. in mV) using Vref (1263 mV at 25 degr. C) delivered 
	by MAX6018. Only 8-bit ADC value is used, accuracy for calculated Vcc in the
	critical range of 2.0V is approx. 10mV, for Vcc of 3.6 (maximum battery voltage) the
	error is approx 40mV.
	 
	Calculation formula: Vcc(mV) = 1263mV (Vref) * (255 / ADC_Vref)
	  			        = (1263 * 51 * 5) / ADC_Vref
	 			        = (64413 / ADC_Vref) * 5
	Note that 16 bits integer calculus is used using as large integers possible
	before a division is done, in order to minimize round-off errors"
	#endrem
	
ReadV:	let w1=0
	readadc 1,b0
	let w1=10240/b0
	let w1=w1*5	;Vcc calculated in mV's
	sertxd (#w1,13,10)
	pause 2000
	
	goto ReadV
P.S. I soldered the MAX6018 and required cap to a 3-pin header, to make it easier to move between designs.

DSC00354.JPG
 

hippy

Technical Support
Staff member
Basically you are using the reading of a constant voltage to determine what the power supply is, which from READADC10 with result in 'Nadc' ...

Nadc = ( Vadc / Vpsu ) * 1023

Vpsu = Vadc* 1023 / Nadc

That's in volts, multiply by 10 and you have it to 0.1V resolution ...

Vpsu = Vadc * 1023 * 10 / Nadc

Mutiply by 10 again and it's to 0.01V resolution ...

Vpsu = Vadc * 1023 * 10 * 10 / Nadc

Drop in 2V as Vref and it's ...

Vpsu = 204600 / Nadc

However that 204600 is too big for PICAXE 16-bit maths ( 65535 max ) but we can divide that by four, and also divide Nadc by four ...

Nadc = Nadc / 4
Vpsu = 51150 / Nadc

That also drops the resolution from 0.01V to 0.04V, 40mV, but as that's sort of where you were after; bingo !

Do
ReadAdc10 1, w0
w0 = w0 / 4
w0 = 51150 / w0 * 40
SerTxd ( #w0, "mV", CR, LF )
Pause 1000
Loop
 
Last edited:

NXTreme

Senior Member
Thanks for the in depth explanation, I now see the light! One small comment. I had to change the "w0 = 51150 / w0 * 40" to "* 10" but other than that, its perfect. I'm surprised at the value of the support, especially since it's free. Most forums would have taken an arm or a leg by now :).
 

hippy

Technical Support
Staff member
Most forums would have taken an arm or a leg by now

You see that van pulling up outside your house .... :)

Is it * 10 ? My brain's too addled to know at the moment; whatever works, give it a good testing. There's a risk of * 10 or * 40 overflowing, hadn't taken that into account ether but may not matter for the actual values likely.
 

NXTreme

Senior Member
I'm not sure what it's 'sposed to be, I do know what works though :). With the 40 changed to 10 and two AAs as a power supply, it reads 2840 mV, while my (admittedly cheap) multimeter reads 2.88 V. With three AAs, Picaxe reads 4260 mV while my multimeter reads 4.32 V. Not spot on, but close enough! I might test it with a regulated 5V supply here, just gotta find a USB extension cable to get the breadboard and programmer to reach to my workbench...

There's a risk of * 10 or * 40 overflowing, hadn't taken that into account ether but may not matter for the actual values likely.

Well, the Picaxe will go up in smoke at about 6V, right? :)

You see that van pulling up outside your house .... :)

Don't tell me... I thought they were here to trim the trees...
 
Top