Measuring a 12v battery with an ADC0831 and BS2
icepuck
Posts: 466
I've been on the hunt for a while looking for examples on how to measure a wet cell LA 12v battery but the closest I've found was 0-5v in milli volts, so that's what I started with.
Here's the math I used changing 5v to 16v to get the needed value.
16v/255=0.062745098039
x 100
=6.2745098392
x256
=1606.2745098 or 1606
=$646 hex
A raw count of 255 with my voltage divider should be 16v. Why 16v? Because my charge controller has a
voltage of 15.5-15.8 volts when in the equalize mode, so I rounded up to 16v to safe.
I changed the following line of original code
to this
It is now displayed the way I want but I get the following
5.00v is a raw count of 81 but shows 05.82v <=which I thought was OK until I tried 12 volts.
11.99v is a raw count of 195 but shows 02.35v <=this is were I really get confused as to why the "1" disappears.
I did some more experimenting and then I changed
to the following
Now 12v gets displayed as 1223mV, which isn't bad for an 8bit adc.
Which leaves the last problem of trying to display it as 12.23v.
I thought about leaving things as they are but the more I read through the DEBUG help the more I seem to be
missing how to put the decimal point were it needs to be.
The included code has some of my experiments commented out so it outputs in milli volts, but it works.
-dan
Here's the math I used changing 5v to 16v to get the needed value.
16v/255=0.062745098039
x 100
=6.2745098392
x256
=1606.2745098 or 1606
=$646 hex
A raw count of 255 with my voltage divider should be 16v. Why 16v? Because my charge controller has a
voltage of 15.5-15.8 volts when in the equalize mode, so I rounded up to 16v to safe.
I changed the following line of original code
DEBUG "volts... ", DEC mVolts DIG 3, ".", DEC3 mVolts
to this
DEBUG "volts... ", DEC2 mVolts DIG 3, ".", DEC2 mVolts
It is now displayed the way I want but I get the following
5.00v is a raw count of 81 but shows 05.82v <=which I thought was OK until I tried 12 volts.
11.99v is a raw count of 195 but shows 02.35v <=this is were I really get confused as to why the "1" disappears.
I did some more experimenting and then I changed
DEBUG "volts... ", DEC2 mVolts DIG 3, ".", DEC2 mVolts
to the following
DEBUG "volts... ", DEC mVolts
Now 12v gets displayed as 1223mV, which isn't bad for an 8bit adc.
Which leaves the last problem of trying to display it as 12.23v.
I thought about leaving things as they are but the more I read through the DEBUG help the more I seem to be
missing how to put the decimal point were it needs to be.
The included code has some of my experiments commented out so it outputs in milli volts, but it works.
-dan
Comments
I used the "ratiometric method" with the ADC0831. This can in some circumstances provide better resolution and more flexibility. The disadvantage is the need to provide accurate reference voltages to the ADC instead of just using 5 volts and ground.
Since you're measuring a 12 volt battery, I used 11 to 16 volts and there's no big advantage. I was able to get pretty accurate results.
I've attached the code, and I'll try to make a simple schematic in case anyone finds it useful.
statements differently from the way I was trying to.
Now that I think about it there is no need to measure down to 0 volts on a lead acid battery since my inverter shuts off at around 9v. Anything lower would probably damage the battery.
-dan
The thing about the ADC0831 that often is confusing is the fact that it's ratiometric. When you set Vin(-) to ground and Vref to 5 volts, you're telling it to measure 5 volts up from 0. You can set those pins to any value between ground and V+.
Basically Vin(-) tells it where to start and Vref tells it how far to go (not where to go, it's a subtle difference).