ADC anamoly

Thread Starter

allahjane

Joined Sep 19, 2012
75
hi there,

I have 4 rotary type variable resistors each attached to one of the
ADC channel of an atmega8 (PC5-PC2).

An unsigned integer array of size 4 named as "min" holds the minimum ADC value for each of the resistors (i.e when resistance is maximum)

similarly an unsigned integer array of size 4 named as "max" holds the maximum ADC value for each of the resistors (i.e when resistance is minimum)

Both of above are measured during runtime.

The problem arises under following case

4 LEDs are set to glow when respective resistor's ADC value crosses the minimum value.

but here only three of the LEDs glow when min their repective min value is crossed while the 4th one one takes much higher ADC value than minimum measured for it to glow (almost maximum)!

why does it happens so ?

again to cross check my circuit I set the LEDs to glow when the ADC value is more than mid of min and max values (syntactically (max+min)/2)

this time it works correctly and the LED glows after its respective ADC value reaches midway

also If I specifically set only the 4th LED to glow when the ADC value of its associated resistor goes above half of the min value then it glows after ADC has crossed about 1/4th of min to max

how is this possible ? why there is such anomalous behaviour?

here are code snippets
Rich (BB code):
//taking min values
		 for(int i=1;i<5;i++)
		 {
		 min[i-1]=getADC(i);//getADC returns ADC value for i+1 th channel
		 }
//similar for max
Rich (BB code):
//check ADC value
for(short i=1;i<5;i++)
{reading[i-1]=getADC(i);
		
if(reading[i-1]>min[i-1])
{ledSelective(i,1);}
else
{ledSelective(i,0);}
 //led Selective glows up LED for resistor i
Rich (BB code):
//check against mid way value (WORKING ONE)

for(short i=1;i<5;i++)
{reading[i-1]=getADC(i);
		
if(reading[i-1]>((max[i-1]+min[i-1])/2))
{ledSelective(i,1);}
else
{ledSelective(i,0);}
 

thatoneguy

Joined Feb 19, 2009
6,359
Odd ADC behavior happens when there isn't a bypass cap on the IC, try a 0.1uF / 100nF cap between V+ and GND

For the odd display, how often are the two functions called? I don't see a test for minimum before you put value in the minimum array? If both are essentially the same function, and called within thousandths of a second from each other, won't they be the same values for min and max of each resistor?
 

ErnieM

Joined Apr 24, 2011
8,377
An unsigned integer array of size 4 named as "min" holds the minimum ADC value for each of the resistors (i.e when resistance is maximum)

similarly an unsigned integer array of size 4 named as "max" holds the maximum ADC value for each of the resistors (i.e when resistance is minimum)
Such an array has elements 0, 1, 2, and 3.

Your code is accessing elements 1, 2, 3, and 4.

Let me guess: the pot connected to element 4 is the one with strange response, correct?
 
Top