# Is It Really This Easy?

## Recommended Posts

I'm a real noob here, especially when it comes to the electronics side of things.

Is it really as easy as it seems to make a Voltmeter?

I'm interested in the state of charge of my 24V battery bank, so am only interested in a fairly small voltage range.

What I have done is gone here: http://ourworld.compuserve.com/homepages/Bill_Bowden/r2.htm to work out which resistors I need to connect between the battery bank and the A/D input channel (23000 and 5000 @ .001 amps).

I am planning on using a 5V voltage regulator, also fed off the battery bank, to drive the PIC16F872 chip I happen to have and hence also provide the reference voltage.

I have written the code in boost C to compare the voltage I have 'made up' (5V off of up to 28V) and then lit one of a series of LED's based on the A/D comparison results.

I've not connected it all up to the battery bank, as I'm not near it, but have tested everything with a 5K variable resistor.

It all seems to work as expected. Hence the question, as I can't believe it can possibly be so straightforward.

##### Share on other sites

Your math is good; the resistors you've chosen result in 5v when the battery bank is at 28v. Sometimes it IS that easy

For bonus points, try coming up with a circuit that more closely monitors the range between charged and discharged battery voltage; say something that converts the range of 20 to 28 volts to be 0 to 5 volts.

##### Share on other sites
For bonus points, try coming up with a circuit that more closely monitors the range between charged and discharged battery voltage; say something that converts the range of 20 to 28 volts to be 0 to 5 volts.
Hi Kenn

Thanks for that. When things just work I worry!

Would it not work to simply use additional A/D channels to measure successively lower voltages using progressively lower R1 resistances, 1900 for the next stage?

Obviously a wise person would not sample the lower ranges with a higher voltage.

Not that I care about anything less than 24V!

##### Share on other sites
For bonus points, try coming up with a circuit that more closely monitors the range between charged and discharged battery voltage; say something that converts the range of 20 to 28 volts to be 0 to 5 volts.
Ahh

After connecting things up, I understand the problem now. The 28 Volts seems to be scaled down over the 5V, or so, so the calibration is not what I was expecting. For some reason I assumed I would only have the 'top' 5V ie. 28V to 24V.

Hmm, any ideas on how to fix this?

##### Share on other sites
For bonus points, try coming up with a circuit that more closely monitors the range between charged and discharged battery voltage; say something that converts the range of 20 to 28 volts to be 0 to 5 volts.
Ahh

After connecting things up, I understand the problem now. The 28 Volts seems to be scaled down over the 5V, or so, so the calibration is not what I was expecting. For some reason I assumed I would only have the 'top' 5V ie. 28V to 24V.

Hmm, any ideas on how to fix this?

Ken has an idea I am sure, but while wating for him look up level shifting, perhaps related to op amps.

Russ

##### Share on other sites

I may have miss lead you, what I said should work, but perhaps looking into the other voltage reference pin would be an easier path. But brain not working too well from all the turkey.

For bonus points, try coming up with a circuit that more closely monitors the range between charged and discharged battery voltage; say something that converts the range of 20 to 28 volts to be 0 to 5 volts.
Ahh

After connecting things up, I understand the problem now. The 28 Volts seems to be scaled down over the 5V, or so, so the calibration is not what I was expecting. For some reason I assumed I would only have the 'top' 5V ie. 28V to 24V.

Hmm, any ideas on how to fix this?

##### Share on other sites
After connecting things up, I understand the problem now. The 28 Volts seems to be scaled down over the 5V, or so, so the calibration is not what I was expecting. For some reason I assumed I would only have the 'top' 5V ie. 28V to 24V.

Exactly. Your current arrangement scales 0-28 to 0-5. What you really want to do is to convert exactly the range you're interested in to the full range your A/D can cope with. Let's say for example you want to convert the range of 20 to 28 v to 0-5v.

I don't right now have a schematic drawing app so I will try to do it in ASCII

The simplest circuit I can think of is to use a zener diode to "drop" the part of the range you're not interested in. The circuit would like so:

```  [+ve] battery terminal (20 to 28v)
|
_	Zener Diode
A
|
R1
|
o------> to A/D
|
R2
|
V	 Ground```

Since we want to throw away 20v, let's choose a 20V Zener. A typical Zener rating is 1/4 watt (250 mW).

So the max permissible current is

I = P/E = .25 /20 = 12.5 mA

Max current would occur at 28 v, so the voltage across R1 and R2 would be 28-20 = 8.

This lets us solve for the minimum value for R1 + R2:

(R1+R2) = E/I = 8/.0125 = 640 ohms

Eh... lets try 1000 ohms.

We know from above that the max expected voltage across R1+R2 is 8v, and we want a max of 5v across R2, we can find R2 as

R2 = 1000 * 5/8 = 625. Let's use 620.

Pushing that back into the relationship 5/8:

(R1+R2) = 620 * 8/5 = 992 then R1 = 992 - 620 = 372... nearest standard value is 390.

So... if you used 390 for R1 and 620 for R2, you'd be quite close. As you can see, you can fiddle the resistor values to get the ratio right, as long as you don't exceed the zener current.

Edited by kenn
##### Share on other sites

No it is not that easy. If the input impedance into the A/D input is too high, the input impedance impacts the measurement. What you want to do is relatively easy for do. It requires two resistor voltage dividers and two halves of a dual opamp.

The first voltage divider is on your battery. You feed the output of the voltage divider into one half of the opamp configured as a voltage follower and the output of the voltage follower into the A/D input of the PIC. This now gives you a low impedance source.

The second voltage divider comes off the + 5 volt rail and connects to the second half of the opamp also in a voltage follwer configuration. The output of this voltage follower connects to the -Vref input of the A/D.

When you do an A/D conversion the converter performs the conversion over the range of -vref to +vref. By increasing the value of -vref you are increasing the resolution of the subsequent conversion.

##### Share on other sites
No it is not that easy. If the input impedance into the A/D input is too high, the input impedance impacts the measurement. What you want to do is relatively easy for do. It requires two resistor voltage dividers and two halves of a dual opamp.
This is beginning to sound a bit complex, and that's before we get our turkey later on this month here in the UK.

I like Ken's more straightforward sounding option of the zener diode. I guess it's a case of sacrificing some precision in favour of simplicity?

Interestingly, I found I had issues with my voltage regulator (http://www.maplin.co.uk/Module.aspx?ModuleNo=7939). For some reason it would not provide a steady 5V, but seemed to match the voltage out of the voltage divider for some reason, as a result nothing worked right. A separate 9V supply fixed that.

See, I knew it was not going to be THAT easy

##### Share on other sites
If the input impedance into the A/D input is too high, the input impedance impacts the measurement.

Quoting from A/D specs for a mid-range PIC:

The maximum recommended impedance for analog sources is 10 kOhm.

In my suggestion above, the worst-case source impedance which occurs when the zener diode is out of conduction, is equal to R2. Once the zener conducts the equivalent source resistance drops to R1 in parallel with R2. In both cases the source resistance are well under 10k, and have negligible influence on the measured voltage. This is easily proven.

Otherwise... your op-amp & reference idea is OK too, but more complex to build and calibrate. I did say that I was giving the simplest solution I could think of

Tim, my suggestion costs 2 resistors and a zener to try... You can just try it with a variable power supply and a voltmeter, before hooking up to the PIC.

Also, there's no reason you shouldn't be able to get a steady 5v from a regulator connected to your battery. a 7805 or a 78L05 should both work fine.

So, you should still be in the realm of easy.

##### Share on other sites

Note that the suggested values give a nice low output impedance so the 10k value for max input is cool with this divider.

The use of the low reference pin gets rid of the need for the zeener, consider it too, at least enough so you understand the use of the pin.

Then to make things complicated again note that it is very bad to exceed the maximun and minimum input to the a to d converter, though I think you are allright with your circuit.

As usual there are several solutions, some simpler some "better". "Engineering is the art of making what you want from what you have"

After connecting things up, I understand the problem now. The 28 Volts seems to be scaled down over the 5V, or so, so the calibration is not what I was expecting. For some reason I assumed I would only have the 'top' 5V ie. 28V to 24V.

Exactly. Your current arrangement scales 0-28 to 0-5. What you really want to do is to convert exactly the range you're interested in to the full range your A/D can cope with. Let's say for example you want to convert the range of 20 to 28 v to 0-5v.

I don't right now have a schematic drawing app so I will try to do it in ASCII

The simplest circuit I can think of is to use a zener diode to "drop" the part of the range you're not interested in. The circuit would like so:

```  [+ve] battery terminal (20 to 28v)
|
_	Zener Diode
A
|
R1
|
o------> to A/D
|
R2
|
V	 Ground```

Since we want to throw away 20v, let's choose a 20V Zener. A typical Zener rating is 1/4 watt (250 mW).

So the max permissible current is

I = P/E = .25 /20 = 12.5 mA

Max current would occur at 28 v, so the voltage across R1 and R2 would be 28-20 = 8.

This lets us solve for the minimum value for R1 + R2:

(R1+R2) = E/I = 8/.0125 = 640 ohms

Eh... lets try 1000 ohms.

We know from above that the max expected voltage across R1+R2 is 8v, and we want a max of 5v across R2, we can find R2 as

R2 = 1000 * 5/8 = 625. Let's use 620.

Pushing that back into the relationship 5/8:

(R1+R2) = 620 * 8/5 = 992 then R1 = 992 - 620 = 372... nearest standard value is 390.

So... if you used 390 for R1 and 620 for R2, you'd be quite close. As you can see, you can fiddle the resistor values to get the ratio right, as long as you don't exceed the zener current.

##### Share on other sites

..

Edited by asmallri
##### Share on other sites

Please forgive the noob take on all this...

I'm thinking the Zener and voltage divider route is the one I'll take, however this is all great new learning, especially the opamp stuff, so I'm keen to experiment a bit for the fun of it.

If I understand correctly, using an opamp does not change the voltage, but reduces the impedance and 'cleans' the voltage? So the opamp presents a more accurate A/D conversion voltage.

Is it then correct that the +ve voltage supplied to the -ve vref increases the range? It sounds like it would reduce the range (+5V to +5V), unless I missed a +/- conversion somewhere or have misunderstood the opamp stuff I read.

One other thought occurred, if I'm only interested in the 24-28 range, and 28 should be the max voltage, why not just use a 24V Zener on the A/D pin and nothing else?

##### Share on other sites
If I understand correctly, using an opamp does not change the voltage, but reduces the impedance and 'cleans' the voltage? So the opamp presents a more accurate A/D conversion voltage.

Is it then correct that the +ve voltage supplied to the -ve vref increases the range? It sounds like it would reduce the range (+5V to +5V), unless I missed a +/- conversion somewhere or have misunderstood the opamp stuff I read.

One other thought occurred, if I'm only interested in the 24-28 range, and 28 should be the max voltage, why not just use a 24V Zener on the A/D pin and nothing else?

The zener + 2 resistors using my initial values and without any calibration will be accurate to within 5% I believe, but by using a trimpot or software calibration, it could be trimmed to be within 1%of actual, assuming you use precision voltage sources and/or a very accurate voltmeter for setting up the converter.

An op-amp front end will give you a lower driving impedance, and it gives you alot more options in changing and shifting the sampled input voltage. Supplying different reference voltages also gives you more control over the input range. Both these give you more flexibility and control over the input voltage. The only downside is increased circuit complexity. Op-amp buffered/shifted inputs would still require alignment.

Re using a 24v zener: first, you'd still need to include one resistor to ground to ensure enough current flows through the zener... something like 2 to 10 mA. The other point is - doesn't your battery voltage drop below 24 when it's near discharged? Whatever zener you choose HAS to have a lower voltage than the lowest voltage you're interested in. Also, the zener-resistor combination isn't perfectly linear right around the zener voltage, so it's best to choose a zener that's at least a volt lower than the lowest voltage you're expecting to measure.

##### Share on other sites
Is it then correct that the +ve voltage supplied to the -ve vref increases the range? It sounds like it would reduce the range (+5V to +5V), unless I missed a +/- conversion somewhere or have misunderstood the opamp stuff I read.

One other thought occurred, if I'm only interested in the 24-28 range, and 28 should be the max voltage, why not just use a 24V Zener on the A/D pin and nothing else?

You cannot just use a zener or you risk killing the PIC. You need to add at least a resistor from the PIC A/D input to ground.

You are looking at a 4 volt range. Without using the vref inputs your A/D converter will do a conversion from 0 to + 5 volts. Assuming an 8 bit a/d converter the resolution will be 5v/256 = 19.53mv per bit.

If instead you raised -vref to 1.0volts then the conversion range of the A/D is from 1 to 5 volts (4 volt range). IN this case the resolution is 4v/256 = 15.63mv per bit.

If you were only interested in a 2 volt range then you could apply +3.0v to the -vref input. In this case the resolution is 2V/256 = 7.81mv per step.

You could tweak the values to make subsequent maths easier. For example instead of a 4volt range, if you had a 4.096 volt range then the resolution would be 4.096/256 = 16mV per step. 16 is a nice binary number simple maths operations.

What is i wanted 10mV per step? In this case you need a span of 10mV x 256 = 2.56volts.

##### Share on other sites

Ok. I think you have all lost sight of the real problem which is to monitor the battery voltage. You have a 10 bit A/D. I forget the numbers but I think you care about 20V to 25V but with the simple resistor network it is scaled from 4 to 5V. This is 20% or the range of the A/D. So you only get to use 20% of the 1024 point sample range or about 200 points. if knowing the voltate to within 1 part in 200 is good enough (which it is! [25mV resolution]) then you can close your eyes and just use it the way you were.

The other way to deal with this is to set the A/D reference negative voltage to 4V and the reference positive voltage to 5V. This way you get the entire 10-bit range in the 4 to 5V range. You can set the negative reference point with a simple voltage divider that scales 4/5. But, I don't think this is worth it.

I'm done ranting for today. Sorry about that.

Edited by trossin
##### Share on other sites
Ok. I think you have all lost sight of the real problem which is to monitor the battery voltage.

The previous responses were in context of the original question deviating from it when the original poster expressed more interest in understanding the mechanics. Once he undertands the mechanics he can choose the resolution that meets his requirements.

##### Share on other sites

Hi Ted,

The zener + 2 resistor thing that I proposed would convert the range of 20 to 28v to 0-5v for the A/D. Given the 10-bit A/D, i would expect this to have enough resolution to effectively monitor the battery.

Of course if we knew more precisely the range of possible battery voltages we could further reduce the span.

##### Share on other sites

Hi All

Things on this post have strayed a bit but, from my perspective, that's not been a bad thing as I'm learning fast & hopefully it's of use to others too.

I most like the Zener diode and voltage divider approach, as it's simple and gives me the most A/D range for the voltages I'm interested in.

I am primarily interested in the 24 - 27 volt range for my 940 AH battery bank (12 2V batteries).

This is part of a bigger project, to automatically remote start a generator if/when the battery voltage falls to 24.1V or less. Hence less than 24 V should be academic and > 27 volts simply indicates the battery charge state (ish).

I'm using 3 LED's (red, green & orange) to show the 7 voltage states I'm interested in. This solves a long standing issue I have had by being able to determine the state of the battery at a glance. Because the batteries get used for domestic and some power tools, my interest in the state of them borders on obsessional.

> 27 Orange flashing

> 25.6 Orange (battery refresher kicks in at this voltage)

> 25.0 Orange & Green

> 24.4 Green

> 24.2 Green & Red (time to think about turning devices off)

> 24 Red

else Red flashing

I've already done this using a PC & Phidgets, but subsequently discovered PIC processors & BoostC, which better suits my need, PC not always running & chews power, and conveniently provides the learning for the next project - monitoring my main engine & gearbox (pressures/temperatures etc).

FYI & before you ask, the battery bank is old and tends to leak voltage over time, so tracking amps in/out is not really practical.

Thanks to everyone for the help with this, and especially kenn for the Zener diode idea.

##### Share on other sites
Hi All

Things on this post have strayed a bit but, from my perspective, that's not been a bad thing as I'm learning fast & hopefully it's of use to others too.

I most like the Zener diode and voltage divider approach, as it's simple and gives me the most A/D range for the voltages I'm interested in.

I am primarily interested in the 24 - 27 volt range for my 940 AH battery bank (12 2V batteries).

This is part of a bigger project, to automatically remote start a generator if/when the battery voltage falls to 24.1V or less. Hence less than 24 V should be academic and > 27 volts simply indicates the battery charge state (ish).

I'm using 3 LED's (red, green & orange) to show the 7 voltage states I'm interested in. This solves a long standing issue I have had by being able to determine the state of the battery at a glance. Because the batteries get used for domestic and some power tools, my interest in the state of them borders on obsessional.

> 27 Orange flashing

> 25.6 Orange (battery refresher kicks in at this voltage)

> 25.0 Orange & Green

> 24.4 Green

> 24.2 Green & Red (time to think about turning devices off)

> 24 Red

else Red flashing

I've already done this using a PC & Phidgets, but subsequently discovered PIC processors & BoostC, which better suits my need, PC not always running & chews power, and conveniently provides the learning for the next project - monitoring my main engine & gearbox (pressures/temperatures etc).

FYI & before you ask, the battery bank is old and tends to leak voltage over time, so tracking amps in/out is not really practical.

Thanks to everyone for the help with this, and especially kenn for the Zener diode idea.

The zener diode is approach is not suitable for your application. I thought I had already mentioned that in my initial post but having reread it I realized the omission. A zener diode has a tolerance of +/- 5% nominal (some are much worse) as well as a transfer function that varies with temperature. In addition you need to ensure you bias the diode to operate beyond the knee region. Here is a link that discusses these points: http://www.edn.com/article/CA426083.html

In your application a +/- 5% tolerance means that the zener voltage variance, ignoring temperate is 2 volts.

##### Share on other sites

I use the approach in post 7 but with reference diodes instead of zeners. These can be extremely accurate. Farnell sometimes have them in 10v although 5v and less are more common.

I have 2 examples on my site. Follow the 'back' button at the foot of both pages. This will take you to a circuit description which explains how I use them.

http://www.flyelectric.ukgateway.net/bal3-cct.htm

http://www.flyelectric.ukgateway.net/lithtester2-cct.htm

Regards, David.

##### Share on other sites
The zener diode is approach is not suitable for your application. I thought I had already mentioned that in my initial post but having reread it I realized the omission. A zener diode has a tolerance of +/- 5% nominal (some are much worse) as well as a transfer function that varies with temperature. In addition you need to ensure you bias the diode to operate beyond the knee region. Here is a link that discusses these points: http://www.edn.com/article/CA426083.html

In your application a +/- 5% tolerance means that the zener voltage variance, ignoring temperate is 2 volts.

Most of these points have already been considered:

- Initial zener value tolerance can be trimmed out by calibration.

- In the design with the 20v zener, the zener current around the point of interest (24.0 v) is greater than 4 mA which should be sufficient current for most 250 or 500 mW zeners. If more current is required, the values of R1 and R2 could be scaled down. For example if you're using a 500mW zener, you could safely divide the values of R1 and R2 by 2, which would double the current for a given voltage. Also note that the 20 v zener diode would be operating well past the knee portion of its curve for an input of 24v.

- Temperature drift. Yes this would be an important consideration, depending on the temperature drift spec of the given diode, and whether the installed diode is subject to significant temperature fluctuation.

A common choice for the zener diode would be a 1N5250 (datasheet). It has a value tolerance of 5%, and its temperature coefficient is 0.086% per degree Celsius, which at 20 v is a drift of 0.018 V per degree Celsius.

So I think the zener idea is still workable. But of course I'm biased.

##### Share on other sites
I have 2 examples on my site. Follow the 'back' button at the foot of both pages. This will take you to a circuit description which explains how I use them.

http://www.flyelectric.ukgateway.net/bal3-cct.htm

http://www.flyelectric.ukgateway.net/lithtester2-cct.htm

Regards, David.

Hi David

Great site, very useful!

I really like the circuit diagrams.

I also see you have done a temperature monitoring project already. I had been thinking of using LM75's, more complex initially, I think, but better in the long run.

##### Share on other sites

"This is part of a bigger project, to automatically remote start a generator if/when the battery voltage falls to 24.1V or less. Hence less than 24 V should be academic and > 27 volts simply indicates the battery charge state (ish)."

I still think the Zener diode solution to your problem is a mistake. Using less bits of the A/D gives you more precision (and this is precision that you don't need) 1 part in 200 is a 0.5% error vs. 5% error with the Zener and you have to spend more money (and you will have a more complex design that will give you fits later). If you really want the most precision read the data sheet and learn about the +- offset adjust for the A/D. With 4 total resistors you can tune it up to get all 10-bits out of the A/D for your desired voltage range. The +- offset inputs allow you to set what voltage corresponds to 0 and 1023 for the A/D. You said you want 24 to 27 so you need 2 resistors to make a voltage divider to scale 27V down to 5V. (scale by 1/5.4 so a 4.4K and a 1K would do that). Then when the input voltage is 27 you get 5V. When the input voltage is 24 you get 4.44V. Now all you need to do is set the + ref to 5V (default or tie a wire to +5) and the - ref to 4.44V. This can be done with a 3.33K and a 1K resitor between +5 and ground. With this done, you should get a full 10-bits out of the A/D for the 24V to 27V input.

Now the only thing that you need to regulate is the 5V supply. As others have said, you can either ignore this or calibrate it away. Also, you most likely can't find the exact resistors you need so you can calibrate that away as well with software. The input voltage pair look like gain and the second - reference looks like an offset. Take two measurements of different voltages then use the old y=mx+b equation to solve for m and b and you have your calibration done.

I hope this helps.

##### Share on other sites

Ok. I looked at the data sheet for the 16F873A part and see a little snag in that the difference between VRef+ and VRef- has to be 2V to get the 10 bit accuracy out of the A/D. The spec:

VRef+ : min=Vdd-2.5, max=Vdd+2.5

VRef- : min=Vss-0.3, max=VRef+ - 2

So, you can't set VRef- to 4.4V, you have to set it to 3V and loose some bits. If my math works, you are only using 0.6/2.0 of the available range or 30% of the 1024 range or you get 307 steps (1 part/307) which gives a 0.3% error instead of a (1/(0.6/5)*1024) = 0.8% error when using VRef- set to zero. For this application, it hardly seems worth adding two resistors for the VRef- input. The ideal is 0.1% error with a 10 bit A/D. I'm sure you will have much more noise in your system than these errors represent so your "Is it really this easy" solution is the one that makes the most sense.

Edited by trossin
##### Share on other sites

One last smallish point: the battery voltage is itself temperature-dependent. I could only find one online reference to the factor for lead-acid batteries, it was given as -0.030 volts per degree C. The measured battery voltage will also vary depending on its internal resistance and the current being drawn.

So maybe there's no point in going for microvolt precision in battery measurement if the battery voltage is likely to bobble by a quarter volt or more if it's under load.

In all the discussed solutions, we're also dependent on the accuracy and stability of the +5v regulator.

Isn't engineering fun?

(and yes Tim, all of this WILL be on the exam )

Edited by kenn
##### Share on other sites

So gentlemen, after all that, are we agreed that because of noise in the system, temperature and the vagrancies of zener diodes that things are probably best left as they originally were for this particular application, as trossin suggests?

Nice and simple, though not necessarily that precise. I've already noticed that I had to average out over about 15 samples to get some kind of stable reading anyway.

I see, from an engineering point of view, that the application very much determines the design and that I should have been clearer about the objective in the first place.

##### Share on other sites

This was actually a fun little exercise, so first of all thanks to you for posing the question. The valid concerns expressed by others forced me to do the research to analyse and quantify the performance of my "zener drop" suggestion. To my mind, anyway, I think I found enough specs and info to show that it can be acceptably accurate in your application, which is a one-off custom situation. But I also learned more about what can be done by the choice of different reference voltages.

If this was a question about a device that was going to be manufactured in quantity, I would NOT go the zener route, mainly because of the 5%variance in zener voltage which would make expensive calibration of each unit a necessity, except maybe if such calibration could be somehow automated in software and the cal values stored in the PIC ram.

If you have the time, try as many different ideas as possible.

Cheers.

##### Share on other sites
• 1 month later...

A final note on this one, now it has been in use for a while.

The battery monitor is working surprisingly well, and accurately, so thanks to all who contributed.

There is certain satisfaction in hearing my generator kicking off all by itself when the battery voltage gets low.

The ability to tell the battery voltage at a glance from a distance is a bonus.

Anyway, here's the breadboard version shown on my blog, along with a plug for Sourceboost

I'm still working on packaging the whole lot into a neat box from Maplins....

## Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×