Adc Accuracy

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times

Adc Accuracy

Post by Creative25 »

Hi
I am experiencing some differences in ADC readings. (pic 16F1939)
About three in four times the reading is accurate but randomly it is out by about 0.1 volts.
Currently I use Vdd as reference voltage. It is between 4.95 and 4.96 volts.
What accuracy can I expect from a 10 bit reading?
How could I improve accuracy?

Best Regards:
Uli

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Adc Accuracy

Post by Benj »

Hello Uli,

What version of Flowcode are you using? If it's v4 then look inside the ADC component properties and there are things you can play with. e.g. sample time and conversion speed.

10-bit reading should have an accuracy around 0.0048V per bit running at 5V.
8-bit reading should have an accuracy around 0.019V per bit running at 5V.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: Adc Accuracy

Post by Spanish_dude »

Just for info, accuracy is calculed as follows:
Vcc / 2^n
n = # bits of the ADC

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times

Re: Adc Accuracy

Post by Creative25 »

Thanks Benj.
I just don't know where to start.
The default time it 40 if i run the chip on 16Mhz Should I increase the timing?
What about conversion speed should I change from FRC to Fosc?
I see there is a table about Fosc values in the Dadasheet. In this case between Fosc /16 and Fosc 64
What is the more accurate conversion speed? 1us 2us or 4us.
What is more accurate FRC or Fosc.
On the Datasheet it says Frc 1-6 us. What does that mean?

Bes Regards:
Uli

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times

Re: Adc Accuracy

Post by Creative25 »

Hi
I am wondering how do you enable the internal reverence voltage on a Pic 1939?
I wold like to use 4.096 volts.
Best regards:
Ueli Meier

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Adc Accuracy

Post by medelec35 »

Creative25 wrote:Hi
I am wondering how do you enable the internal reverence voltage on a Pic 1939?
I wold like to use 4.096 volts.
Hi Uli,
Similar topic was asked here:
http://www.matrixmultimedia.com/mmforum ... =29&t=8955
However Since 16F1939 uses PIC_ADC_18.c and not PIC_ADC_23.c
Then you will need this updated PIC_ADC_18.c If you keep a copy of the original then replace with updated version (in \Matrix Multimedia\Flowcode V4\Components), Hopefully it will work?
I'm not an expert like Ben is, I have only copied what he has done, but also made some of my own modifications since not all register's involved have the same name.
So If it does not work... sorry I tried, and if it does work then Ben has played a bit part of it.

Martin
Attachments
PIC_ADC_18.c
(8.46 KiB) Downloaded 422 times
Martin

Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times

Re: Adc Accuracy

Post by Werner »

Hi Uli, These Guys have some great points and I would do those things first! For me a lot of times I have to resort to filtering with caps and better quality parts. I always check my pots to be sure they are doing what I expect like making sure I am getting 0 ohm and 5k ohm in the case of a 5k pot. More than once I have found a pot that does not go to 0 ohm and that alone can drive a person crazy. One of my favorite programs is “Electronics Assistant” you can find it at: http://www.electronics2000.co.uk/ and best of all its free, although I do recommend making a small donation. Among the many Calculators it has one of them is a RC Filter Calculator. This will help you to quickly build a Low Pass or High Pass frequency filter. One other thing I found is that taking my projects to circuit board often also help tremendously when dealing with ADC noise by keeping connection as short as possible. Of course a board with a lot of copper ground will make it even better.
Good Luck Buddy!
Werner
Attachments
mail.jpg
mail.jpg (57.8 KiB) Viewed 11938 times
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Adc Accuracy

Post by medelec35 »

Werner has got valid points regarding accuracy. Also Accuracy is not the same thing as resolution.
You could have an increase in resolution (using lower Vref) but the accuracy may still be out.
One thing, Could it be the formula or input resistance or supply voltage that's causing the discrepancies?
How are you determining the accuracy is out?
Are you sending the ADC value direct to LCD with no formula?
If you don't mind sharing, you could post your flowchart, we we can have a look for you.

@Werner That's a great link. Amongst other things the Electronics assistant and EPE index is very handy to me, so thanks for that :)


Martin
Martin

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times

Re: Adc Accuracy

Post by Creative25 »

Hi
Thanks for all your advice.
I have checked this topic, just was not sure if all of this applies to the Pic 16f1939
However at this point I think I have a problem with some noise on the supply line from the battery.
The device is powered from a 12v solar battery.
I discovered that when there is no load on the battery then the ADC reading is perfect.
But if I put a load on the Battery. In this case a 12 Volt Danfoss compressor then the readings are differing often.
However with a multimeter the voltage does not fluctuate.
The line from the Battery to the Voltmeter is about 2m and the compressor is connected to the same line.
I thinks the voltage on the Line fluctuates because the compressor does not draw the same power all the time, and so the voltage drop on the line differs. Depending on which position the rotor is at the time the ADC reading is taken the voltage would differ.
Also I guess the multimeter shows an average.

How do I solve this problem?
With a filter?
Or can I configure the Adc in such a way that it takes an average?

Regards:
Uli

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Adc Accuracy

Post by Benj »

Hello Uli,

You can make a simple low pass filter using a resistor and a capacitor as shown here.

http://en.wikipedia.org/wiki/Low-pass_filter

You can also do ADC averaging by taking multiple samples, adding the results together in a INT variable and then dividing by the number of samples. Remember not to let the INT variable accumulate more then 32767 or the values will start turning negative.

If the sample count is a factor of two then you can do a right shift instead of a divide to save a few program clock cycles.

eg "samples = samples >> 3" is the more efficient way of achieving "samples = samples / 8"

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times

Re: Adc Accuracy

Post by Creative25 »

Thanks for the Ideas.
I bulit a low pass filter and accuracy is better.

Question if you have a ripple frequency of 33Hz and you want a nice average what frequency should the low pass filter have.
The same, half, one third?
Regards Uli

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times

Re: Adc Accuracy

Post by daveb0360 »

Hi Guys,
Could I just add a footnote to your discussion......based on my own experiences today.
I had a 12F series chip lose an A/D channel today which resulted in a useless circuit that would not run code although it programmed ok.

Now I did some failure analysis and then applied some age old knowledge that I had forgotten.

Basically, I had a 1uF capacitor connected to input pin of A/D port of the chip.........to act as filter for the measurement line.....a resistive divider. After about the fifth power down of the circuit....it refused to function again.

Capacitors, as we all know, hold charge and in a power down state, the charge in the cap has to go somewhere!. It will attempt to discharge through anything connected to it....including your pic. Depending on the impedances and timings involved.....this is extremely risky for I/O ports as they can not withstand the energy discharge from caps.

Basically, although it compromises the measurement accuracy of your A/D a small degree.........always place a resistor between any capacitor connected to your precious pic ports. This will limit current flow to within safe limits and save your device.....as well as protecting ports from inadvertent shorts.

I learned the hard way.

Dave

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Adc Accuracy

Post by medelec35 »

That's a very good tip indeed. Thanks for sharing that with us.

More tips like these would be great!
Martin

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times

Re: Adc Accuracy

Post by daveb0360 »

You're welcome Martin, If only I was so knowledgeable on coding.......lol.
My input port had gone low impedance to ground......basically meaning the energy stored in the cap was enough to overwhelm the lower input steering diode that's there to protect against esd.

Seen it many times when I worked for Analog Devices......their failure analysis dept. was next door.......diamond saws, electron beam microscopes etc.........used to seeing images of esd fried gates and structures .......amazingly !....many of them in otherwise working chips !!!
So....when someone says to you they have never killed a chip with static, don't believe them......they just don't know it yet. The device will almost certainly fail prematurely in service!


Dave

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times

Re: Adc Accuracy

Post by Creative25 »

daveb0360 wrote:Hi Guys,
Could I just add a footnote to your discussion......based on my own experiences today.
I had a 12F series chip lose an A/D channel today which resulted in a useless circuit that would not run code although it programmed ok.

Now I did some failure analysis and then applied some age old knowledge that I had forgotten.

Basically, I had a 1uF capacitor connected to input pin of A/D port of the chip.........to act as filter for the measurement line.....a resistive divider. After about the fifth power down of the circuit....it refused to function again.

Capacitors, as we all know, hold charge and in a power down state, the charge in the cap has to go somewhere!. It will attempt to discharge through anything connected to it....including your pic. Depending on the impedances and timings involved.....this is extremely risky for I/O ports as they can not withstand the energy discharge from caps.

Basically, although it compromises the measurement accuracy of your A/D a small degree.........always place a resistor between any capacitor connected to your precious pic ports. This will limit current flow to within safe limits and save your device.....as well as protecting ports from inadvertent shorts.

I learned the hard way.

Dave
Hi
That is quite interesting and raises a few Questions.
What value of resistor do you use?
If the Capacitor between VDD and VSS discharges slower than the one between the ADC and VSS i guess you should be fine.
But How much bigger must the one between VDD and VSS be that it will be slower? Since they do not have an equal load on them?
Of course this will only work if you apply a voltage that comes from an external source and is not fed by the capacitor that is connected to VDD.
Best Regards:
Uli

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times

Re: Adc Accuracy

Post by daveb0360 »

Hi Uli,

I use the maximum current 'into' any pin specification from the datasheet.
This is basically defined as the maximum forward current of the steering diodes that are at the i/o pins (except MCLR).
Most pic's.....and indeed, many other logic families and processors, define this as 10mA max.

Using basic laws, this means that on a 5V driven circuit, a resistor of 500 ohms will limit current to the 10ma max. (nearest preferred value 470R)
Therefore, it would seem wise to add 470R between any I/O pin and any capacitor tied to that port. There are other factors such as capacitor type, esr etc. but as a general rule, this would seem sensible design rule IMHO.

In the case of A/D convertors, this will degrade accuracy to a degree that is related to the input current requirements of the port pin and the impedance of the preceding circuitry. This is usually in the uA range and can be largely ignored unless you require better than 12 bit accuracy.......roughly!

Dave

Post Reply