Measuring DC voltage with an adc

kd8cgo

New Member
Hello all,

I am in the process of planning a project where I will be measuring the voltage of batteries to determine state of charge. The batteries are common lead acid type, this is un-important save for the fact that I will be working with voltage range from 0 to ~18 volts. The system in mind would be agile enough to hopefully handle both 6v and 12v batteries, and be able to measure the voltage at the highest foreseeable situation, a 12V battery under "equalizing" charge, which is constant current in application and can reach over 16 volts easily.

Now what I would like to do is of course use the ADC's resolution to its maximum effectiveness. I.E. a 0-18V swing would use the entire 0-5V swing of the adc. Now this is strictly a DC application, and a very basic use of the input. I have come up with two possiblities that (I think) will work.

One is to use a simple voltage divider with the adc center tapped into it, setting the resistance ranges appropriate for both the impedance needed for the PICAXE and the voltage swing desired. I have been using a calculator program to speed picking the resistances, but it still seems a bit far from optimal.

I had another thought, but I am not familiar enough with opamp's to know how to implement a solution. I could set up a non-inverting buffer with a gain of approx. 0.25. This seems like it would work ok but I have never used an opamp before. I understand that most single supply voltage opamp's use an offset of the input voltage so that both a positive and "negative" swing is available, in reference to 1/2V. Since I am only worried about DC input, could I not just ground the negative supply rail on the opamp, and set up the input and feedback resistors for the appropriate gain ratio? Do I need to power the opamp from the full battery voltage (rather than the +5v supply for the picaxe) to ensure a full 0-5V swing on the output without clipping?

Apologies if the questions seem a bit rudimentary, but I am just starting out!

Cliff notes:
ADC Application: measure 0-18V DC
Use a voltage divider or opamp for best resolution?
Specifics on using an opamp in this DC configuration? (no negative components/non-inverting)
Prototyping on: 28X1
 

hippy

Senior Member
I'm not an op-amp expert so cannot answer your specific questions, but there is a third option ...

Have the op-amp take a window of voltage, say 10V-12V, and convert that to 0V to 5V. This gives 256/1024 steps of accuracy to measure the voltage in that range, far better than that number of steps over the whole potential range.

The actual voltage window can be controlled by the PICAXE itself and it can auto-track the voltage so it always uses the best window. You could use programmable 'bottom voltage' and 'amplification' to have a very programmable voltage meter. You'd need a means of protecting inputs from over-voltage and negative voltages, but it should be doable, although a bit more complicated.
 

kd8cgo

New Member
That is a very interesting idea, and makes even better use of the of the available resolution. Now I just need to dig deeper into opamp design to get to the bottom. I have read so far the they can do offset somehow, so instead of just a linear gain/scaling the output can be shifted (I imagine in either direction.) Problem is the basic opamp tutorials I am reading do not generally delve into such details; many are AC amplifier oriented, and attempting to read documents online on opamp optimization seems to make my head spin. Once the opamp fundamentals are down, I could go about figuring how the PICAXE can move the voltage window around to get the best solution. Of course a moving window is icing on the cake, but a working proof of concept prototype would be well worth the effort in the "wow I did it" sense.

I am sure the actual implemented solution will be somewhere in the middle, depending on what I can figure out, and keeping cost and component count to a minimum. If anyone has a link to really good opamp tutorials/references online they would be most appreciated!
 

womai

Senior Member
Another option would be simply to use a higher-resolution external analog-to-digital converter in combination with your voltage divider. You are already contemplating external devices (the op-amp), so in terms of component count it wouldn't make any difference. And it's quite a challenge to have the op-amp work in a perfectly linear fashion over the whole range unless you supply it with supply voltages beyond maximum and minimum input voltage (and that requires additional circuitry). With an external ADC this is straightforward.

You can get free samples from Microchip.com; one suitable device is the MCP3301, which has 13 bits of resolution (i.e. 8 times finer than the Picaxe by itself) and is available in DIP package. Talking to it is simple (standard SPI protocol - see shiftin/shiftout commands). They have also devices with more ADC input channels.

Wolfgang
 

Michael 2727

Senior Member
If you are just starting out I'd just stick with the
Voltage Divider for starters.
You can always build the MKII model at a later date.
You could include a 5.5V Zener on the ADC input
to clamp any accidental or unwanted voltages getting
in, and even an LED to warn you.
Anything over 14.2v to 14.5V may damage battery.

I had an old 1967 Toyota Corolla once, it had a
mechanical regulator (worked like a tuned Buzzer)
over time the adjusting points wore down allowing
17V into the battery, fried my New battery in about 3 weeks.
 

kd8cgo

New Member
I plan to prototype various methods and pick out whichever seems to fit the final application best. This is just one part of a larger project, and trying multiple methods on a solderless breadboard first is half the fun. I can see learning the opamp tricks coming in handy for a variety of adc sampling issues down the road for the same project, as well as future projects. I also will look into the costs of external high-res adc units and pick up a few as well. As I am tinkering, all this will probably cost 10x what the actual finished board will cost, but all that I learn on the way I think will be worth it.

Hopefully this will not only help me but other PICAXE'ers as well.

More searching and reading has lead me along to the analog switches hippy has mentioned in the past as well as a whole slew of opamp related posts. Unfortunately some of the more specific posts with schematics were done in ASCII art and seem to be corrupted in some way with the variable font.

And just to point out, the high voltages I mentioned on the batteries are seldom seen and only done under very tightly controlled situations with a proper charger unit. The equalization is only performed on flooded batteries used in a deep cycle/standby type of arrangement, so -hopefully- no worries regarding battery damage.

The zener protection at first seems like a good idea, but I have also been reading about the negative effects when used with an adc. There are options when using them with opamps however that provide good clamping voltage range and no droop or drop off near the zener breakdown range apparently.

The overwhelming and quick support of the forum here has got to be one of the best assets to using the PICAXE so far. This is probably the most undersung of the advantages you read about when picking a micro platform. All I can say is WOW and many thanks. I'll be back tommorow, it's very late.
 

moxhamj

New Member
Quick op amp primer

These are some useful op amp circuits.
1) The voltage follower. Useful if you have a voltage that can't be loaded very much - eg a voltage divider made of two 1M resistors. Use the voltage follower to convert this into a really solid voltage that can be loaded up to 20mA.
2) The 2.5V reference. Useful to create a virtual earth. The alternative is to use the real earth and run the op amp of +5V and -5V but usually a -5V supply is not to hand. It is easier to reference everything off 2.5V.
3) The basic non inverting amplifier (gain 100). Adjust the two resistors for gain. If the virtual earth is 2.5V then everything references off this voltage. Eg input 2.51V. This is really 0.01V. Multiply by 100 to get 1V. Then add back to 2.5V to get 3.5V output. Ok the maths is a bit harder than if using a real earth, but it is easier than building a-5V supply.
4) The basic inverting amplifier (gain 100). Input 2.49V. This is really -0.01V. Multiply by 100 and invert and this is +1V. Add back to 2.5V to get a 3.5V output.
5) The extremely useful differential amplifier. This schematic is for a gain of 1. The output is the + input minus the - input. Eg input 3V and 2V. Difference is 1V. This is added to the 2.5V reference so the ouput is 3.5V. If you make R6 and R8 the same and R5 and R7 the same then the gain can change. Eg make R5 200k (and hence R7 200k). The gain is now 2. So with one circuit you can measure the difference in volts and add in gain and even vary the earth reference via varying the 2.5V reference.

The catch - with a LM324 the outputs cannot go less than 1.25V or more than 3.75V with a 5V supply. You could supply it from +/-12V and get a full 5V swing. A CA3140 can swing rail to rail though.

Ok - to change 10V-15V input to 0-5V. It probably is possible to do this with one chip but lets do it with several chips and do it in stages so it is not so confusing. First, divide by 5 with a voltage divider(40k/10k). (use a 39k and 1k to get 40k) Range is now 2V to 3V. Run through a voltage follower. Create a 2V reference with a pot into a voltage follower. Run 2V reference into R8 of the differential amp. Run the 2-3V battery voltage range into R7 of the differential amp. At 2V input the output is 2.5V. At 3V the output is 3.5V. Hmm - not enough gain and not centred. So change the 2.5V virtual earth to maybe 2V. Change the gain a bit on the differential amp to 1.5 (100k/150k). Now the output should swing about 1.25V to 3.75V. But we want 0-5V so lets either use a +/-12V supply, or a CA3140.

Now maybe simplify things. Use a 4k/1k voltage divider for the 15V input- this has lots of current and so the voltage follower isn't needed. Make a 2V reference from a 1k trimpot - this has lots of current so the second voltage follower isn't needed. Make a virtual earth from another 1k pot - now the virtual earth voltage follower isn't needed. Use a ca3140 so it swings 0V to 5V. Now it is down to just one chip.
 

Attachments

Tom2000

Senior Member
Here's a wacky idea. I don't know if the outputs of a 28X1 pull low enough to make this work, but you might try it before you go the op amp route.

If nothing else, it's cheap and simple.

Good luck!

Tom
 

Attachments

hippy

Senior Member
@ Tom2000 : Like it. There are a couple of issues ...

Voltage at ADC In could exceed +V if the PICAXE selects the resistors wrong. That can probably be solved with a zener diode across ADC In and 0V, or an op-amp buffer.

Analogue inputs would be present on digital inputs. The PICmicro datasheets caution against that ( may cause excessive current draw ), but I've never personally witnessed such problems and the PICAXE appears to work in that mode anyway.

Although a PICAXE Output Low will never drive fully to 0V, that can be compensated for in software. Only outputing low a single I/O nearest ADC In and keeping further right pins as inputs would minimise parallel resistance changes as more R's switch to 0V. It would make sense to have a very high R at the far right with the rightmost I/O always output low ( if no other is Output Low ) for consistency. In practice it may not matter.

On a separate note for op-amp use - Getting rail-to-rail outputs from an op-amp appears to be difficult or needs the right op-amp. The simple solution would be to not require rail-to-rail outputs but let the software do some adjustment on whatever it reads.

If trying to auto-range, then this needs to be done anyway; the PICAXE reads ADC, if > 75% switch to a more attenuating input, if < 25% switch to a higher amplifying input or similar, so the PICAXE will never be reading a 0-255/0-1023 input anyway.

In fact, that's perhaps an advantage because it may be possible to adjust op-amp gain to give 0-100/0-1000 input readings which helps the maths.

Although not reading 0-255/0-1023 means the resolution drops, that's compensated for in earlier gains of windowing etc which have increased resolution anyway.
 
Last edited:

kd8cgo

New Member
I've been drawing up schematics following Dr. Acula's opamp posts and I have a question regarding the last bit. Here we have a differential amplifier in the final form of 1.5 gain, 2-3V signal in (from batt), 2V reference, 2V virtual earth. Let's call the battery signal Vsig, the 2V reference Vref, and the virtual earth Ve. The swing he came up with assuming an LM324 was 1.25-3.75. I am not coming up with the same answer. I am using this calculation:

1.5(Vsig - Vref) + Ve = Output

So, with a 2V Vsig, I get an Output of 2V. With a 3V Vsig, I get a Output of 3.5. It seems that by trying to adjust the gain and the virtual earth, the two changes almost canceled out, I only gained 0.5V of range on the bottom end. I realize the quoted range he gave was the ideal range of the LM324 running on a 0 to +5 supply, but I am not getting the swing on the Output he seems to have expected. What am I doing wrong?
 

moxhamj

New Member
It probably helps to play with this on a breadboard. My maths may be a bit off but it was designed to get things into the ballpark range.

The swing above is the max voltage range the 324 op amp can possibly output when powered by a 5V supply, not the actual voltages for that circuit. A CA3140 can go 0V to 5V.

Here is the maths on a differential amp. Consider a gain of 1 first - all resistors equal (eg 100k). Put 2V onto the - input resistor and put 3V onto the + input resistor. Put 2V onto the virtual earth. Consider the + input - there is 3V going to the 2V reference so that is a voltage divider. That means there is 2.5V on the op amp. Now, op amps like to keep their + and - inputs the same, so assume there is also 2.5V on the - pin as well. We have 2V on the -ve resistor, 2.5V on the - pin so what voltage must be on the output to make the maths work with the voltage divider? Answer is 3V. Let's check this works - the difference between 2V and 3V is 1V. The virtual earth is 2V so the output is 2V+1V=3V.

The exercise can be repeated with a gain of 1.5 - the voltage dividers just divide differently. I'll post the answer in a sec - lunch hour over and got to see a patient...
 

moxhamj

New Member
Maths for gain of 1.5. Set the virtual earth at 2.5V. Difference between inputs is 1V. Gain is 1.5. Output is thus 1*1.5 = 1.5V plus the 2.5V virtual earth = 4V. This checks with the voltage dividers as shown in the schematic.

In reality a 324 op amp running from 5V will max out at 3.75V but the maths all work fine if this were powered from a 5.25V supply or higher. I probably would use the CA3140 for a final design, but I prefer to breadboard with 324s as they are less than $1 for 4 op amps.

The complex bit here is the voltage divider maths. Consider a 10V input to a 100k then a 150k then to ground. The total resistance is 100+150=250k. The divisor is 150/250 = 0.6. The volts between the resistors is 6V. The formula of a voltage divider is (lower resistor/total resistance)* volts to divide, where "volts to divide" is the difference between the upper and lower volts.
 

Attachments

womai

Senior Member
Re: cost of components

Cost for ADCs, DACs, opamps, regulators and so on is usually ZERO if you only need a few:

You can get many integrated circuits for free - most semiconductor manufacturers have very decent sample programs where you can get a few devices (usually 2 to 5) for free. Most of them will ship to private hobbyists as well (you never know when a private hobbyist may become a customer buying thousands for a large company :) Shipping is often free as well (it's virtually always free if you work for an electronics company). Good sources I use often are
  • Microchip (www.microchip.com; can order 3 times, up to 5 different devices, every three months) - EEPROMs, DACs, ADCs, regulators; free shipping
  • Maxim (www.maxim-ic.com) - ADCs, DACs, op-amps, RS-232, etc. etc.; free shipping
  • On Semi (www.onsemi.com) - huge portfolio, great also for high-speed ECL stuff; small customers have to pay a nominal shipping fee
  • Linear Technologies (www.linear.com) - great for regulators, opamps; they also offer LTSpice / Switchercad, a pretty good Spice simulator, for free.
  • Analog Devices (www.analog.com)
And there are many more.

Wolfgang
 

kd8cgo

New Member
Ok, if I understand this correctly, and I think I do, then using these design parameters:

signal to be measured: 10-15V DC
Output to ADC: 0-5V DC

Then, do these steps in order:

Voltage divide signal by 5 (4k/1k): Sample Range 2-3V (1 volt span)
Provide a Reference for differential amplifier of 2V
Use ground potential as virtual earth, i.e. 0V, or no 'virtual' earth
opamp would be rail to rail, CA4130.
Gain of 5 (100k/500k for simplicity) - Turn that 1 volt span into a 5V span

Now, some Examples:

10V -> = 2V through divider -> subtract reference of 2V=0 -> apply gain of 5: 5*0=0 -> Add virtual earth = 0V

11V -> = 2.2V through divider -> subtract reference of 2V = 0.2 -> apply gain 5*0.2=1 -> add virtual earth = 1V

12V
-> = 2.4V through divider -> subtract reference of 2V = 0.4 -> apply gain 5*0.4=2 -> add virtual earth = 2V

....

15V
-> = 3V @ divider -> minus 2V ref = 1 -> gain 5*1 -> Output 5V

So, as long as it is ok to use 0V potential as virtual earth, in other words R7 tied to ground, then this provides a perfectly scaled, linear sample of the voltage for the ADC to use. Of course the 10-15V example makes for easy math, but the same could be worked out for any range, and in fact I think a moving window algorithm could also be employed by switching resistors via analog switches controlled by the PICAXE.

I have no opamps to proto with yet, in my box I have a few comparators but I am very light on analog ICs. SO some are on order and I can play in the real world. I also will pickup a few analog switch IC's, a digital pot, and a few high res adc chips to play with.

I just picked up on your post womai while typing this up. I'll certainly look into some of the manufacturer sample programs. For the final project this probably isn't practical as I'll be making several finished boards, but I could save a ton of money in the prototyping phases...
 

D n T

Senior Member
I'm trying to measure amps in 24 volts

Can you use a resistor to drop the voltage from 18 volts to 5 ( or to be safe, 4.8) volts to sample?
If the resistance and the current stay the same and the voltage will change.

My brain is full of more shunts than a rail yard and more amps than a rock concert, so if I'm off the mark ignore it.
 
Top