Meassure with 10 bit resolution between 11 volt and 15 volt?

General discussion on mikroPascal.
Author
Message
Ulf
Posts: 63
Joined: 30 Dec 2007 18:23
Location: Sweden

Meassure with 10 bit resolution between 11 volt and 15 volt?

#1 Post by Ulf » 23 May 2009 18:16

Hi. I am trying to make a potentiometer for my motorcycle. The voltage can vary between approx. 11.5 volt (at start) and 15 volt (when running). I know that I have to do a voltage divider that feeds the PIC with maximum 5 volt. But I guess there must be some way to use the full resolution of the A/D converter between 11 volt and 15 volt. I mean to have 11 volt as 0. Since the voltage never falls below 11 volt it is meaningless to waste resolution on that intervall. I wonder if someone could give some advice how to do this (if I have made myself understood).

Cheers

Ulf

norbie
Posts: 160
Joined: 06 May 2006 22:40
Location: Vista, California

#2 Post by norbie » 25 May 2009 02:53

Hi Ulf,
the ADC channel will always reference to 0V as Zero. The resolution of the ADC in a 10 Bit configuration means that 5V (max) would be divided into 1024 segments. In this case 5/1023 for approx. 5mV per bit. Every time a bit toggles i.e. increments, there has been a voltage change of 5mV on the input.
With a 12Bit ADC (available in some MCU's) the same 5V would be divided by 4096 i.e. 5/4095~1.2mV per bit toggle. This means that you can measure a voltage change of either 5mV (10Bit) or 1.2mV(12Bit). If the noise level is low enough, that is.
It will be somewhat different when using a Voltage divider. It is not a good idea to calculate the divider too tight since a failure on the Voltage regulator side could blow up the MCU.
You could get a higher resolution with 3.3V MCU's but the voltage divider must be recalculated and do you really need Micro Volt measurements?
I strongly suggest to clamp the ADC input to 5.1V via a Zener Diode or similar device so that the ADC input never goes higher than 5 Volt. R1 is calculated for the current needed to trigger the Zener. This can be taken from the Diodes datasheet. If the diode needs 2mA of current to trigger, R1 has to be chosen accordingly. Once R1 has been determined you can calculate R2 for a ratio of 4. An alternator can produce easily 16 Volts or more without a load connected or in a fail condition.

It is OK to divide the worst case voltage by four i.e. 16V/4=4 with appropriate resistor values. Choose current values as low as practical, since you don't want to drain your battery through your meter.
Image
The above image should provide the appropriate voltage, however is meant to be a brain stimulant only.

With the circuit above you will measure about 4VDC at the ADC path when the battery is at 16VDC. This means everything is divide by 4.
If the ADC produces 1023 (10Bit) at 5 Volts the ADC reading would be at 16 Volt Battery Voltage about 1V / 5 mV = (1024-200) = 823. 823 should be measured on the ADC at 16 Volt Battery Voltage. From here one can scale the entire Voltage down to 11 Volts. From 16 Volt to 11 Volt there is a difference of 5 Volts. The 5 Volts need to be divided by 4 to have the actual voltage change on the ADC input. 5 / 4 = 1.25 Volts. At 11 Volt Battery voltage the ADC would see 4 V - 1.25 V = 2.75 V at an ADC count of about 550. Now your Battery good range would be between 823 and 550 ADC count. This is accurate enough for a battery monitor. Most certainly could you scale it down to Zero volts, however this would not be linear anymore and you would have to do some serious calculations with additional components. Also, as you get closer to Zero Volt, noise may be present on you board and falsify the measured values. Unless you can completely eliminate board noise thes ADC's will not work acurately at low voltage levels. Besides, there could be a short cell in your battery and in this case the voltage would be lower than 11 Volt. If you can catch this because you still can measure 8 Volts or less you could make an educated guess and say my Battery isn't worse it because a Cell is Short.
In your firmware this would look somewhat like this

Code: Select all

  while TRUE
    temp_res = Adc_Read(channel)
    Variable = temp_res             ' Store ADC reading in Variable
    Voltage =  temp_res * 20      ' 5mV (10Bit) * 4 (divide ratio)
  wend
NOTE: This is only a suggestion and by no mens a complete working piece of code.

Assuming that the ADC reading was 764 it would mean 15.3 Volts at the battery. If the reading drops to 645 the Voltage at the Battery would be 12.9 Volts and so on.

I didn't really want to design the project, but I guess there is a lot to it even though it looks like a simple application.

Hope this will help you with your project!

Cheers
Cheers,
Norbert

You don't need to know the answer, you just need to know where to find it!

norbie
Posts: 160
Joined: 06 May 2006 22:40
Location: Vista, California

#3 Post by norbie » 25 May 2009 03:17

Sorry, I gave you a code sample in Basic and not in Pascal.
Should be very similar, however.
Cheers,
Norbert

You don't need to know the answer, you just need to know where to find it!

Ingvar
Posts: 22
Joined: 13 Mar 2007 23:08
Location: Sweden

#4 Post by Ingvar » 25 May 2009 14:50

I must disagree with what norbie wrote.
"the ADC channel will always reference to 0V as Zero"

I say that the AD measures between Vref- and Vref+. If you set Vref- to Vss then the statement is true.

This beeing said, i should also point out that you can't just pick any voltages and expect good results. The datasheet gives you voltages you must "respect". For a 16F877A you must have atleast a 2V difference between Vref+ and Vref-. Vref+ cannot be less than 2.5V and not higher than AVdd +0.3V. So the best you can(or rather - should) do with a 16F877A is to set Vref+ to Vdd(5V) and set Vref- to Vref+-2V(3V). Using an external voltagedivider(2R:1R) you can then measure between 9 and 15V with full resolution.

/Ingvar

Ulf
Posts: 63
Joined: 30 Dec 2007 18:23
Location: Sweden

#5 Post by Ulf » 28 May 2009 11:29

Thank you very much for your replies. I will read them carefully and see if I can manage to do what I intend.

Cheers

Ulf

WB6BNQ
Posts: 228
Joined: 12 Jun 2005 21:02
Location: SAN DIEGO, CA - USA

Re: Meassure with 10 bit resolution between 11 volt and 15 v

#6 Post by WB6BNQ » 31 May 2009 02:46

Ulf wrote:Hi. I am trying to make a potentiometer for my motorcycle. The voltage can vary between approx. 11.5 volt (at start) and 15 volt (when running). I know that I have to do a voltage divider that feeds the PIC with maximum 5 volt. But I guess there must be some way to use the full resolution of the A/D converter between 11 volt and 15 volt. I mean to have 11 volt as 0. Since the voltage never falls below 11 volt it is meaningless to waste resolution on that intervall. I wonder if someone could give some advice how to do this (if I have made myself understood).

Cheers

Ulf
Hi Ulf,

Your interest in making better use of the A/D range is a typical design issue that is loaded with problems. What is missing is the purpose of the potentiometer. Here are some questions :

1. What is the relationship between the potentiometer and the voltage range of 11 to 15 volts ?

2. Is the potentiometer floating between these voltages ?

3. Or does it set some other circuitry that controls the range ?

4. Is this potentiometer a device that is going to be set once and left alone ?

5. Or is it something that is going to be adjusted on a constant basis (i.e., like a throttle control) ?

6. If it is a throttle control type application, what is the reason for monitoring the voltage of the control ?

7. Is the potentiometer an integral part of a feedback mechanism (i.e., inside the control loop) ?

8. What exactly do you mean by “make a potentiometer” in your first post ?



The A/D implemented in the PIC 16 and 18 series devices, while functional, have their limitations. As “norbie” alludes to, the {zero} end of the scale is the most problematic because you will never be able to truly reach a zero volt reading using the internal Vref. The A/D has the following issues :

1. First and foremost is the stability of the voltage supply to the PIC when using the internal Vref for the A/D converter. Preferably it would be rock solid, while not possible it should at least be better then 1%. Generally, this is easily met with most 3 terminal voltage regulators if properly used.

2. The impedance of your PCB circuit traces are very important. The concern is current through a conductor causing voltage drops. More specifically, the voltage drop changes that occur due to various functions of the CPU being brought on and off line, not to mention other external circuits further down the trace path. This affects the stability of the internal A/D Vref and the ZERO for the Vref and the A/D input ground reference. Most likely these error issues fall below the smallest step value of the A/D (4.887 millivolts) 90% of the time if proper design techniques are followed.

A word about using external Vref. The spec sheet states the minimum value for Vref+ is 2.5 volts and can be as high as Vdd=0.3 volts. Similarly, the Vref- can be any where between Vss-0.3 volts (i.e., below ground) to a maximum of Vref+ - 2 volts. So, the zero problem could be improved by using the external Vref function. However, you would need to generate a negative voltage supply. While not a big problem, if you are going to go to that kind of trouble and additional circuitry, you would be better off to use an external A/D with better capabilities. Using external Vref does not get rid of the zero problem for the A/D input circuit, as it is still tied to the CPU ground pin. Also, there are impedance considerations when using the external Vref.

3. The, by far, largest error issue most people have is not understanding the input impedance requirements of the A/D. This is covered under the “A/D Acquisition Requirements” in the DATA sheet. The recommended maximum source impedance is 2k5 and should really be lower. The measurement point for the A/D is the sampling capacitor and its charge time is affected by the source impedance.

This is one important point “norbie” failed to recognize in his battery monitoring circuit. The R1 value should have been 2k0 or less. This presents a problem with his zener idea. A larger zener would need to be used that can handle the larger current. However, a zener will start to conduct before it reaches its stabilized level and that will affect the true value of the voltage divider. Another point to consider is the input impedance of the PIC A/D at 5 volts is around 7k5 and must be considered in the divider value equation as well.

If this project is a one time deal it may not be a big issue when considering catastrophic issues, that is, a PIC is not that expensive. If this is going to be a marketable product, then such issues should be considered. There are much better ways to handle the input protection, but that would entail additional circuitry and up the cost.

In reference to my first 7 questions and to answer your original question, the way to handle the scaling is to use an opamp in the non-inverting mode ahead of the A/D input. Here you would set the opamp’s negative input to 11 volts and look at the potentiometer with the plus input. When the potentiometer is at 11 volts the opamp’s output would be zero volts. When the potentiometer is at 15 volts the opamp’s output would be at 5 volts. The opamp’s feedback resistors would set the scaling factor of 3 to 1. Opamps are fairly cheap and, in some cases, can handle an overload and would provide for a cheap protection scheme for the PIC.

A point about resolution, depending upon the application, too much resolution could spell disaster. For instance, assuming a throttle control on a motorcycle, too high a resolution could cause the control to be sensitive to vibration and natural hand flexing causing unwanted variation in motor speed. Likewise, too small of a resolution could also produce jerky control. So you need to carefully consider the resolution issue. It would be better to have more resolution and handle the issue in the software by ignoring every other step change or some similar concept.


All of the above is dealing with a physical potentiometer that already exists. BUT the way you worded your first question could mean you want to simulate a potentiometer. Could you clarify your meaning please ?

Bill....WB6BNQ

joseLB
Posts: 444
Joined: 02 Apr 2006 05:56
Location: Riode Janeiro, Brasil

Re: Meassure with 10 bit resolution between 11 volt and 15 v

#7 Post by joseLB » 08 Jun 2009 20:22

WB6BNQ wrote:... The spec sheet states the minimum value for Vref+ is 2.5 volts and can be as high as Vdd=0.3 volts. Similarly, the Vref- can be any where between Vss-0.3 volts (i.e., below ground) to a maximum of Vref+ - 2 volts. ...
Hi Bill
Based on your statement I looked carefully at 16F88 manual and you correct for the 16F88.

But I did 2 circuits (before knowing that), where accuray was not a issue (just 10%-20% accuracy needed), and they seems to be working ok, even being implemented as follows...

The vref is a 10K resistor in 5 V, driving a LED that gives 1.67Volts that is used as Vref+ (vref-=ground).

The first circuit I'm measuring a 1.5V battery, up to when it reaches 1V. Seems that I have 0.1V of accuracy.

The other is the same vref circuit, but the item to be measured is a LM05 termometer, in the range 0.2V = 20 degrees and 0.4V= 40 degrees, and comparing with a normal thermometer seems to be ok, and seems I have about 1 degree of accuracy (the lm05 gives 0.1 degree, I believe).
Why they are working?
Thanks -> Jose

Ingvar
Posts: 22
Joined: 13 Mar 2007 23:08
Location: Sweden

#8 Post by Ingvar » 09 Jun 2009 10:35

Things will still work even with other voltages but microchip can't guarantee the accuracy of the AD. This means that the errors can/will be greater thereby decreasing the accuracy you were trying to improve by messing with the Vref voltages in the first place. So it's quite often no use going beyond the specs. However sometimes you get a chip that's really good. Fine for a one off project but no good for commercial production.

Ulf
Posts: 63
Joined: 30 Dec 2007 18:23
Location: Sweden

#9 Post by Ulf » 09 Jun 2009 11:53

Hi everybody and thank you for your input.

In response to WB6BNQ I can say that this is just a hobby project for me where I am trying to build a volt-meter for my motorcycle and display the voltage on a seven segment display. The voltage is usually around 12 volts at start and when running around 14.8 volts. The voltage never drops below 11 volts of course unless the motor is not running and the battery dead.

Currently I have set the Vref- to 3.3 volts with a zener diode feeding the PIC via a voltage follower op amp (LM324). So far this seems to work.

Cheers Ulf

WB6BNQ
Posts: 228
Joined: 12 Jun 2005 21:02
Location: SAN DIEGO, CA - USA

#10 Post by WB6BNQ » 10 Jun 2009 08:39

Hello Jose and Ulf,

Unfortunately, Microchip gives very little information about the internal circuits in their product. Unless they give us more info, we can only infer by reading between the lines coupled with whatever experience we have.

Near as I can determine the A/D topography used in the 16F and most 18F devices are a Successive Approximation Register (SAR) A/D. An SAR uses a Sample & Hold circuit into one input of a comparator and drives the other comparator input with a Digital to Analog Converter (DAC) whose source is Vref. The digital setting to the DAC is the final output value reported.

Without knowing the internal circuitry of the PIC cpu, we have to assume the specifications tell us what is considered the reliable operating points. As real estate is limited in the PIC cpu, I suspect they have implemented just what is necessary to achieve very basic operation for the A/D. Keep in mind that they intend these devices for non critical applications.

What happens if you run a Vref lower then the spec’d value ? First, the Vin being measured cannot exceed the Vref value. Second, and probably due to DAC switch constraints, the DAC becomes nonlinear and the accuracy suffers. Third, the signal to noise will go up contributing to the loss of accuracy. While it will probably work most of the time, it is not to be considered as standard practice.

Jose, I am wondering why you used a LED in place of zener for a reference ? They are not stable and will vary with the supply voltage. In your first example, if 10 or 20 % is considered ok, then you could have easily used the internal reference and achieved better resolution. The basic A/D resolution of 0.005 millivolts would have given you ½ % at 1 volt.

In your second example, I am not sure what you mean by a normal thermometer, but a decent glass thermometer is at best ½ degree of accuracy unless you pay big dollars, then they get down to maybe .01 degree for a very limited range. If your reference thermometer was ½ degree accurate then your 1 degree comparison could be anywhere from only ½ degree off to as much as 1 & ½ degrees off. This is not counting any variation with the LED reference. You would have been better off using an opamp on the input to boost the signal level and, again, use the internal Vref. However, I suppose you could use software to calibrate your circuit affording some improvement.

Ulf, I do not understand the reason for your circuit direction. Setting Vref for 3.3 volts does not help because you then need to divide the input even further causing additional loss of resolution. Besides, a common zener is only good for 5 % accuracy, completely defeating your interests.

As you have an opamp (actually 4 in the package), I would consider my original suggestion of using the opamp to expand the measurement for the range of interest (11 to 16 volts) increasing your resolution. A decent device to use for the 11 volt reference to the opamp is a cheap active shunt zener like the TL-431 that has a 50 microvolt stability level. If you cannot find any or a substitute in Sweden, let me know and I will send you a few. It is adjustable and will allow for adjusting the “new” zero point.

As for the motorcycle platform, you should provide a properly filtered regulated supply to run the PIC and associated circuitry. The noise level of a vehicle system is totally dependent upon the quality of the battery and its internal impedance. The noise in such a system is never low enough, particularly if the diodes in the alternator are leaky, a typical situation I have seen in most cars with some age.

Bill....WB6BNQ

Ulf
Posts: 63
Joined: 30 Dec 2007 18:23
Location: Sweden

#11 Post by Ulf » 10 Jun 2009 14:19

Hi Bill and thank you for your interest.

I have a voltage divider that at 15 volts feed the PIC (via the op amp) 5 volt. At 9 volts the voltage divider outputs 3.3 volt, thats why I have the Vref- set to 3.3 volts. Thus I have a resolution between 9 and 15 volts i.e. 15 - 9 = 6 gives 6/1024 = 0.005859375 so my equation in the pic is; motorcycle voltage = (AD_result * 0.005859375) + 9. If the ADC_read gives lets say 543 the voltage displayed would be 543 * 0.005859375 + 9 = 12.181640625 volts (not that many digits of course). I hope I have made myself understod. I am fairly new at PIC programming and electronics but it is a fascinating hobby.

Ulf

WB6BNQ
Posts: 228
Joined: 12 Jun 2005 21:02
Location: SAN DIEGO, CA - USA

#12 Post by WB6BNQ » 11 Jun 2009 02:13

Ulf wrote:Hi Bill and thank you for your interest.

I have a voltage divider that at 15 volts feed the PIC (via the op amp) 5 volt. At 9 volts the voltage divider outputs 3.3 volt, thats why I have the Vref- set to 3.3 volts. Thus I have a resolution between 9 and 15 volts i.e. 15 - 9 = 6 gives 6/1024 = 0.005859375 so my equation in the pic is; motorcycle voltage = (AD_result * 0.005859375) + 9. If the ADC_read gives lets say 543 the voltage displayed would be 543 * 0.005859375 + 9 = 12.181640625 volts (not that many digits of course). I hope I have made myself understod. I am fairly new at PIC programming and electronics but it is a fascinating hobby.

Ulf
Hi Ulf,

I was originally confused by some of the terms you were using. Finally I figured out that you were using the “A/D Voltage Reference Configuration bits {VCFG}” set to 01, meaning the positive Vref is using Vdd and the negative Vref is an external value you have set to 3.3 volts.

This is certainly a valid arrangement and satisfies the requirement that the highest voltage read is at or below the positive Vref value. Then you would be looking at a 1.7 volt range (5 - 3.3) divided by 10 bits (1023 - not 1024) equaling 0.001661779 volts per step. Your formula then becomes [A/D value * 0.001661779 * 3 + 9.3], or [A/D value * 0.004985337 + 9.3].

However, the data sheet does say that the negative Vref value should be 2 volts lower then the positive Vref value (see data sheet table 18-13). Thus I would use the active shunt zener diode, I mentioned, which has a minimum voltage of 2.75 volts, is more stable and can be adjusted upward to exactly 3 volts. Having an adjustable negative Vref value allows you to calibrate your system to compensate for the actual positive Vref value. Don’t forget to adjust the +9.3 in your formula. While this method works, I still think the other way may have advantages.

Use of the buffer opamp you already have could set both the range and do the division. Here the A/D would work in its normal way of looking at the full 5 volt range. The active shunt zener would be used to set bottom value of the opamp (11 volts). The resolution would be the same or actually a bit better (if we are splitting hairs). This allows the upper and lower end to be calibrated, reduces your formula by getting rid of the +9 term and frees up a PIC pin for other uses.

Good luck with your project !


Bill....WB6BNQ

Ulf
Posts: 63
Joined: 30 Dec 2007 18:23
Location: Sweden

#13 Post by Ulf » 11 Jun 2009 13:32

Hi again Bill

You say I should divide by 1023 instead of 1024. But is there not 1024 steps from 0 to 1023?

Cheers

Ulf

WB6BNQ
Posts: 228
Joined: 12 Jun 2005 21:02
Location: SAN DIEGO, CA - USA

#14 Post by WB6BNQ » 12 Jun 2009 05:38

Ulf wrote:Hi again Bill

You say I should divide by 1023 instead of 1024. But is there not 1024 steps from 0 to 1023?

Cheers

Ulf
Hi Ulf,

Technically you are correct and I was being sloppy in what I was trying to say, it happens. I tend to treat the "zero" value as not being real -- I know -- a bad habit !

What I was really referring to is covered in the following article. In the first part of this article under DC accuracy is a section talking about the "The ideal transfer function" and the paragraph immediately after the figure 1 picture titled "Figure 1: Ideal transfer function of a 3-bit ADC."

http://www.embedded.com/columns/technic ... tid=157197

Just food for thought and the reason for my bad habit. In actuality you only get 1023 steps (like the 7/8ths in the article) with which to describe the voltage. You do not have an additional real bit to get the top value as illustrated in the above article. So in effect it is like throwing away the LSB in a manner of speaking.

Bill....WB6BNQ

joseLB
Posts: 444
Joined: 02 Apr 2006 05:56
Location: Riode Janeiro, Brasil

#15 Post by joseLB » 14 Jun 2009 14:35

Thanks Bill for these classes in AD converters made practical.
Hi, Ulf, what about to change topic name to something like AD considerations/tricks/practical explanations?
I will try to explain why a led and other stuffs. Follows part of the circuit of interest.
Image
There are 3 x 1.5V batteries being Vdd (4.5V when charged, 3V when discharged). At AN0 I measure one of these batteries. When it reaches 1V, the PIC must take some measures and end it's operations.

The less I consume power, the best, more the batts will last. So:
1- Vdd varies from 4.5V to 3.0V -> internal Vref cant's be used, must be something the most possible independent of Vdd.
2- I need to measure A/D from 1.5V to 1.0V
2- I tried zenners I had (2.7V), but at this current (3v/10K ohms), they vary a lot with Vdd. In fact they don't work.
3- leds, as show the measured table, where the less variant device I had at hands (4.5 -> 3V):

Code: Select all

VIN	VLED
5,23	1,63
4,70	1,62
-------                     begin area of interest
4,18	1,62
3,69	1,61
3,23	1,6
--------                    end  area of interest
2,81	1,59
2,42	1,58
4- in order to consume as low as possible, see that led is only feeded at measure time. That is, at 500Khz, Rb4 is made OUT=1 during some microseconds (4 instructions), and about 250miliseconds in tristate (IN).
5- as this circuit will not be mass produced, J7 is a measure point where I get Led's real voltage and feed it in source code and recompile/burn PIC

Post Reply

Return to “mikroPascal General”