Measuring voltage a 24 volt battery from a micro. - Page 1
Forum section: electronics / projects

author: lk
author_rank: Regular Contributor
timestamp: November 15, 2012, 04:27:30 pm
content: Greetings,I'm measuring the voltage on my 24 volt battery, using the voltage divider between R25 and R2. This is all nice and fine, but i feel that i throw away a lot of resolution, its a 10bit ADC, the divider will divide by 10, and this means i will get voltages in the 22v-28v ranges, while this might be good enough, i have been pondering on how to use the entire micro ADC range from 0-5 volt for my measurements, so that 0-5 would measure 20-30 volts, and i simply cannot figure out a way to do that, i have looked at op amps and I'm sure i can somehow use one for that, but i have not come up with anything that made senseSo if someone could push me in the right direction.And if anyone is interested this setup is meant to control the source of power to a PC, from either solar panels and batteries or a 24 volt power brick, and there is some logging and output displays as well.-lk

author: SeanB
author_rank: Super Contributor
timestamp: November 15, 2012, 04:57:00 pm
content: Try stacking 3 6V8 zeners in series to drop a constant voltage, then measure the voltage across the resistor. Not going to be linear to 10 bits, and will have a slight tempco ( thus the 6V8 zeners, which are available with a close to zero TC) but will increase the usable range of the ADC input. Instead of having the small 0.6V difference you now will have to divide down only 10V, so a divide by 3 will give a 1 to 3v range.

author: mariush
author_rank: Super Contributor
timestamp: November 15, 2012, 05:01:42 pm
content: http://hyperphysics.phy-astr.gsu.edu/hbase/electric/voldiv.htmlSo assuming 30v will be the peak, in order to get Vout between 0 and 5v, you pick a value for the R1 and go from there ...5 = 30v x R2 / (1000000+R2) => 1.000.000 + R2 = 6 R2 => R2 = 1.000.000 / 5 = 200k ohmSo now you have a maximum of 5v when the input is 30v - the 10bit ADC will give you values between 0 and 1023 so in theory each value is about 30 mV. In practice, it will probably be good for 100mV sensitivity.Unrelated, check your pdf, there's something fishy about the capacitor polarity, for example C17 and C18 , C3 and C22...

author: w2aew
author_rank: Super Contributor
timestamp: November 15, 2012, 05:45:05 pm
content: The voltage divider won't give you the translation you asked for... 20-30V input to 0-5V output. You need to do an offset. The zener idea will work, but you may not get exactly the range you need due to the discrete zener values available, and the non-linearity due to the current change through the zeners.Here's an idea that should work. It uses one of my favorite little parts, the LM10CN. This 8-pin device has a nice little opamp and an adjustable regulator in one package. Very wide operating voltage range, and the opamp output will swing very close to the supply rails. This circuit creates a reference voltage of 20V, uses it with a simple difference amp and voltage divider to create a 0-5V output (1K output impedance) for the 20-30V input.The circuit could likely be simplified - I just threw this idea together. I used a lot of same-value resistors so that the scaling works well.

author: Kremmen
author_rank: Super Contributor
timestamp: November 15, 2012, 05:47:00 pm
content: I think the OP meant to map the range 20 ... 30 volts to the ADC full range. That can be accomplished with a simple level shifter and attenuation.

author: w2aew
author_rank: Super Contributor
timestamp: November 15, 2012, 06:09:01 pm
content: Quote from: Kremmen on November 15, 2012, 05:47:00 pmYes, this is what my circuit does too 20V --> 0V, and 30V --> 5V. Mine uses a some dividers and an opamp based subtraction for the level shift.

author: lk
author_rank: Regular Contributor
timestamp: November 15, 2012, 06:52:30 pm
content: Greetings again,SeanB, thanks for the suggestion, its seems like a fairly simple way to solve the problem, but wont i have problems with the zeners when i don't have a somewhat constant current through them, with the initial 6 volts play i expect to see, the current through the zeners would change as well, and therefore introduce some error? or am i mistaken i this.Mariush, thank you for taking time to find the voltage dividers, but that is the approach that only gives me 0.6 volts to measure on in the first placeW2aew: your "W2aew CAD" suggestion looks intriguing, i have some questions however, would i be able to feed the op amp off my battery voltage directly, or does the input need to be higher? the battery voltage should be high enough to create the 20 volt reference i guess. I'm unsure of the significance of the little circles you use-lk

author: Zero999
author_rank: Super Contributor
timestamp: November 15, 2012, 08:58:44 pm
content: The TL431 could be used to drop the voltage by 20V (schematic and calculations on the data sheet), giving 0V to 10V which could be converted to 0V to 5V with a potential divider.You could also use a negative regulator, such as the LM337L, to reduce the voltage by 20V.

author: Psi
author_rank: Super Contributor
timestamp: November 16, 2012, 12:55:29 am
content: I don't know anything about that PIC but you may be able to do something with the ADC reference system (or use differential mode, but i don't think it has that).If you can switch the Vref- so its 1.67V instead of GND then a 0-5V signal (representing 0-30V) will be measured from 1.67V - 5V (20-30V)From the datasheet it looks like the ADC Vref- can be switched between GND and AN0presumably if you had 1.67V DC on AN0 you could switch the negative ADC Vref to that, take a measurement, and then switch it back to normal so you can read other ADC inputs referenced to GND.Actually it seems that the DAC shares the same pin as AN0, pin 19, so you might be able to generate the 1.6V in software using the DAC.Anyone with more knowledge about PICs know if that's possible?

author: lk
author_rank: Regular Contributor
timestamp: November 16, 2012, 01:08:32 am
content: Greetings again,Hero999, i just finished simulating with your suggestion and that solved the problem i have with a minimum of parts and complexity, it worked perfectly over the ranges i needed.Psi, that is something i don't know if or how that would work, and how many silicon bugs i would have to %-B to get it configured-lk

author: Psi
author_rank: Super Contributor
timestamp: November 16, 2012, 01:12:18 am
content: Quote from: lk on November 16, 2012, 01:08:32 amhehe, true

author: ptricks
author_rank: Frequent Contributor
timestamp: November 16, 2012, 01:17:36 am
content: One some PIC chips you can switch the reference voltages ,set up the reference before initializing the ADC and once initialized the reference cannot be changed without turning off ADC functions, setting up a new reference and initializing the ADC again. Not hard to do all that but it is lot of steps and depending on how time critical the task might have problems. The newer pic chips like the pic32 line can switch references without stopping and initializing the ADC to change references.

author: Harvs
author_rank: Super Contributor
timestamp: November 16, 2012, 02:28:22 am
content: Slightly off topic, but if you want to increase you resolution you may find the Microchip app note AN1152 and AVR app note AVR121 worth a read. Oversampling to increase ADC resolution. All depends on how much time you have available to make the measurement.

author: w2aew
author_rank: Super Contributor
timestamp: November 16, 2012, 04:19:16 am
content: Quote from: lk on November 15, 2012, 06:52:30 pmThe opamp output can swing very close to the positive supply rail. Not sure about the reference amp, but I'd expect it to be the same. I'd have to study the data sheet or simply breadboard it quickly to see. If so, the it could be powered off the battery. The little circles represent the 'pins' on the 8 pin package - that's what the little numbers are, pin numbers.

author: Zero999
author_rank: Super Contributor
timestamp: November 16, 2012, 12:14:34 pm
content: Quote from: lk on November 16, 2012, 01:08:32 amWhich one did you simulate? If you have the LM337 the go for it but the TL431 is more accurate and doesn't need the capacitors.

author: lk
author_rank: Regular Contributor
timestamp: November 16, 2012, 01:59:21 pm
content: Hero999: i simulated with the lm337, because that made sense to me, i did find the way to use the TL431 in the datasheet, but I'm still a newbieI have however included the LM337 simulation for LTspice, if anyone finds it interesting.-lk

author: BravoV
author_rank: Super Contributor
timestamp: November 17, 2012, 05:03:01 am
content: Attached below zipped TL431 model for LTSpice, put the TL431.asy into the lib\sym folder, while TL431.sub in the lib\sub folder.

author: Zero999
author_rank: Super Contributor
timestamp: November 17, 2012, 11:05:27 am
content: Quote from: lk on November 16, 2012, 01:59:21 pmThe TL431 is just a an adjustable zener. Set the voltage to 20V (R1 = 33k, R2 = 4k7) and put it in series with the potential divider.

author: lk
author_rank: Regular Contributor
timestamp: November 18, 2012, 07:23:49 am
content: Thanks BravoV and Hero999,I clutched together the simulation in LT spice using the TL431 that BravoV provided, and it looks to work, but am i using too many resistors ?The 1N750 zener is there only because i could not find a 5.1 volt one in ltspice.-lk

author: Zero999
author_rank: Super Contributor
timestamp: November 18, 2012, 11:45:51 am
content: Just thinking, the potential divider values could do with being a bit lower. The TL431 has a minimum cathode current of 1mA. Below that will won't give a steady 20V drop, which will typically happen around 22V. Going down to 150R will stop this from happening until 20.3V (worst case, 20.12V typical) but draining 33mA from the battery may be unacceptable.EDIT:The LM337L solution doesn't suffer from this problem, although it's accurate than the TL431.Just thinking, the potential divider values could do with being a bit lower. The TL431 has a minimum cathode current of 1mA. Below that will won

author: lk
author_rank: Regular Contributor
timestamp: November 19, 2012, 07:37:30 pm
content: Hero999, thank you for all your input it has been very valuable, in this case i have decided to go with the LM337L, it seemed to fit the bill better even tough i have to go buy them, its like so many other times before, i will never ever need a negative voltage regulator, just like i would never need a microcontroller or powersupply that could provide more than 20 volts-lk