Shop OBEX P1 Docs P2 Docs Learn Events
Measuring a 12v battery with an ADC0831 and BS2 — Parallax Forums

Measuring a 12v battery with an ADC0831 and BS2

icepuckicepuck Posts: 466
edited 2014-06-25 14:07 in BASIC Stamp
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
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

  • RDL2004RDL2004 Posts: 2,554
    edited 2012-05-29 16:12
    I played around with this a bit today. I actually had posted earlier, but the code I attached was messy and confusing so I deleted it. I cleaned it up a bit and made some improvements.

    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.
    1024 x 681 - 94K
  • icepuckicepuck Posts: 466
    edited 2012-06-01 18:04
    Thanks for posting the example code, it's interesting to see another way of doing things. I also see how you did the debug
    statements differently from the way I was trying to.
    RDL2004 wrote: »
    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.

    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
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-06-02 06:05
    I said that there was no big advantage using my method as far as resolution, but there is. If you're using a voltage divider to reduce 15 or 16 volts to 5 for input to the ADC, then each measured volt is really 3 volts (15/5). Using the capability of the ADC0831 to set upper and lower bounds of the measuring range gives you about 3 times better resolution, since the range is now 5 volts actual instead of 16 divided down.

    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).
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-06-02 12:35
    For the display part, you had,
    [SIZE=1]DEBUG "volts... ", DEC2 mVolts DIG 3, ".", DEC2 mVolts[/SIZE]
    
    But the DIG 3 is what makes it display only the 3rd digit. This does all the digits to the left of the decimal point with two digits to the right...
    [SIZE=1]DEBUG "volts... ", DEC2 mVolts/100, ".", DEC2 mVolts[/SIZE]
    
  • sir_hacksalotsir_hacksalot Posts: 15
    edited 2014-06-25 14:07
    I have found this thread incredibly useful! Thank you for the code!. You had mentioned a possible schematic for the voltage divider circuit. Were you able to create one?
Sign In or Register to comment.