CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to

extremly fluctuation on ADC Value

Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message

Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

extremly fluctuation on ADC Value
PostPosted: Sat Feb 12, 2011 8:35 am     Reply with quote

I'm using the code to run a ADC value.
the code is as follows:-


#include <18F4520.h>
#device ICD=TRUE ADC=10
#use delay (clock=8000000)
#include <flex_lcd.c>
#include <stdlib.h>
void main()

int16 adc_value;
float volts;
set_adc_channel( 0 );

   adc_value = read_adc();
   volts = (float)(adc_value * 5)/1023.0;   
   printf(lcd_putc, "\f%3.2f", volts);

but the adc values are fluatuating to a great extent.

I'm not sure how to use a Vref.
if that mite be causing the variation on the LCD.

Please let me know how to go about with it.
I have used decoupling capacitors at the ADC input.


Joined: 01 Jul 2010
Posts: 9136
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 10:04 am     Reply with quote

Ok.. what is the source of the variable voltage? If from some 'device', remove it and just use two resistors chosen for say 1/2 scale( 2 10Ks will do)
Then run your program and see the results.If stable, the you know the problem is the 'device', or wiring to it.

Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 10:19 am     Reply with quote

Also, since I remember that device sharing AVdd and Vdd through the same pin, you want more than 100nF decoupling on the Vdd pin.

Try 10uF if you have it... possibly in parallel with 100nF.

Dazed and confused? I don't think so. Just "plain lost" will do. :D

Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 11:04 pm     Reply with quote

Thanks for your reply.

I'm used a 10uF in parallel to 100nF and it quite made a difference...
but again there was a slight variation of 0.3V though the input was steady.
Is there anything more I should add in the circuit?
Moreover I'm failing to understand the concept of Vref.
Can someone please explain me in brief about the same.

Thanks once again.

Joined: 11 Mar 2010
Posts: 19267

View user's profile Send private message

PostPosted: Mon Feb 14, 2011 4:09 am     Reply with quote

You want to measure the size of a wall in your room. You haven't got a tape measure handy, so you decide to use a piece of string. Take this down the builder's yard, and use this to buy your shelf. When you get it back, you find it is too long. What has happened?. The string stretched more when pulled in the yard (and perhaps got a bit damp), than it did when you were in your room. So 'try again', you try a bit of wire this time. Much better, but now the shelf is a tiny fraction too short. This time you realise that the wire shrank in the cold out in the yard, and also that this time there was a wind blowing, making the length also vary, and there was vibration from somebody using a pneumatic drill nearby. You also had problems getting a helper to hold the other end of the wire still, using a thin pole as a guide, that kept wobbling as other people walked past.

Everything we measure has to use some form of 'measuring stick'. For the ADC, this is the Vref. The ADC, does not return voltages, it returns a number, saying that the sampled voltage is a fraction of the Vref.
Now, the quickest way to use the ADC on the PIC, is to use the supply voltage as it's reference. problem is that this is _never_ accurate. The best supply chips will give (say) +/-1% 'accuracy'. But you have to remember this is 'at' the chip, and that the regulator takes time to respond to changes in the load. By the time you have wires connecting this supply to the PIC, there _will_ be noise added (the fact that the 10uF made a significant difference, says your circuit _is_ quite noisy.....), and other circuits will be putting loads on the supply, which will be modulating the supply. This latter is like the 'wind' in the builder's yard. To use the Vref as separate from the supply, you have to select the option in the PIC setup, and feed your Vref, into the Vref input pin.

So, to get a stable reading needs multiple things:
1) A stable Vref - best solution, is to use a laser trimmed bandgap reference diode feeding the 'Vref' pin of the PIC, separately from the supply rail - equivalent to changing to using the wire, rather than the string, and making sure that no wind is blowing when you use it. If you have to use the supply rail, use a good quality regulator, well laid out, with adequate smoothing.
2) A stable source - this is why an earlier poster suggested testing with resistors, rather than from your real source, to see if this was where the problem lay.
3) Select the right clock for your ADC. ADC_CLOCK_INTERNAL, is _not_ the recommended clock for running a PIC at 8MHz, unless you are sampling in a more complex way (using sleep). This is the pneumatic drill problem. The internal ADC clock, is not synchronised to the internal PIC clock. as things happen in the PIC (the vibration from the pneumatic drill), you get variations in the reading. Look at the PIC's data sheet, and select the right clock.
4) A good _ground_. This is equivalent to the problems with getting the other person to hold the end of the wire without moving. You need to be careful both in having a low impedance ground path, and in the _routing_ of this path. The stick wobbling as people walked past, is exactly what happens to the ground rail, as other bits of electronics operate. Look at radial ground layout, or the use of a ground 'plane', and even when using the latter, 'think', about how the current's run inside the plane, when dealing with high power circuitry in particular.
5) ADC input source that meets the minimum impedance specifications. If driving from many high impedance devices, buffering _will_ be needed. This is like 'how tight you pull the string'....

Finally as a comment (doesn't affect the stability, but does affect the accuracy), rethink the formula used to convert from the ADC 'count' to the voltage. This is a very common error made by a lot of people here.

Two separate problems. The 'fence post' versus the 'fence panel' count, and the PIC ADC, behaves differently from a lot of other ADC's. Microchip did a nice little application note describing the best transfer function (the formula used to get from a ADC 'count', to a voltage - AN21841), but this was only ever published in paper to my knowledge, and has never appeared on line. AN546, for the PIC16C71, actually contains some parts of this as well, and has the graphs showing the problem.
The key point is, that the line representing this function, actually doesn't pass through '0', but is displaced half a count down at the zero point.
The best fit simple transfer function for the PIC ADC, is:


May not matter, but it is a common error here when using the ADC, and does degrade the accuracy, if you are spending money adding an external reference....

Best Wishes

Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Mon Feb 14, 2011 10:19 am     Reply with quote

Excellent description Ttelmah !! One thing I would add is that some of the PICs (like the 18F14K22) do have an internal Vref you can use that is better than just using Vss and Vdd. While not perfect, it is a definite improvement over just using the 5v supply as a reference (unless you are sensing a resistive divider also referenced to the 5v in which case the variations cancel out ).

-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3

Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

PostPosted: Tue Feb 15, 2011 3:01 am     Reply with quote

thanks alot for the actually made the concept quite clear for me.. thanks alot once again Smile

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group