Arduino uno Adc problem

hello everybody,

i am working on project in which joystick is used to control two bipolar stepper motors with A4988 drivers. But the problem is output of the ADC from both x axes and y axes pins is fluctuating . For x axis pin it is between 514 and 516 and for y axis pin it is between 519 to 521.
when joystick is at rest position.

This is the code i am using :

 int joyPin1 = 0;                 // slider variable connecetd to analog pin 0
 int joyPin2 = 5;                 // slider variable connecetd to analog pin 5
 int value1 = 0;                  // variable to read the value from the analog pin 0
 int value2 = 0;                  // variable to read the value from the analog pin 1

 void setup() {

  Serial.begin(9600);
 }


 void loop() {

    value1 = analogRead(joyPin1);   
    delay(10);             
    value2 = analogRead(joyPin2);   

  Serial.print("x value :");
  Serial.print(value1);
  Serial.print("  ");
  Serial.print("y value :");
  Serial.println(value2);
  
  delay(1000);
 }

i have tested the above code on the following boads

  • Arduino Uno clone
  • simple atmega328p with usbasp as programmer on perfboard
  • original Arduino Leonardo

Only original Arduino Leonardo is giving clean ADC output with value of 515 for x axis and 520 for y axis when joystick is at rest .
atmega328p with usbasp as programmer has more noisy outputs than my Arduino UNO clone.

Is this a known problem with ADC of arduino Uno ?
Is there any special circuitry on Arduino Leonardo for noise Reduction ?

It is known, yes. But that's normal for every ADC you drive down to the last LSB. Also, especially when on the 5V range, every change in supply voltage changes the reading. So +- 1 LSB is nothing to worry about. If you want a more stable reading then just average it.

So maybe the ATmega32u4 has a better ADC or a better regulator but most of all it's just luck.

And with a joystick it's not a problem. The change of the stick not returning to exactly the same position/resistance is bigger then the ACD error.

:slight_smile:

problem solved.

This is what i did :

int joyPin1 = 0;   // slider variable connecetd to analog pin 0
 int joyPin2 = 5;   // slider variable connecetd to analog pin 1
 int value1 = 0;    // variable to read the value from the analog pin 0
 int value2 = 0;    // variable to read the value from the analog pin 1

 void setup() {

  Serial.begin(9600);
 }


 void loop() {

  int valueX ;
  int valueY; 

    value1 = analogRead(joyPin1);   
    delay(100);             
    value2 = analogRead(joyPin2);   


    valueX =value1 ;
    valueY =value2 ;
 

if(value1 >516){
    
    Serial.print("x value :");
    Serial.print(valueX-515);
    Serial.print("  ");
}

else if(value1 <514){
    
    Serial.print("x value :");
    Serial.print(515-(valueX));
    Serial.print("  ");
}

else {

  Serial.print("x value :");
  Serial.print(0);
  Serial.print("  ");
}

if(value2 >521){
    
    Serial.print("y value :");
    Serial.println(valueY-520);
}
else if(value2 < 519){
    
    Serial.print("y value :");
    Serial.println(520-(valueY));
}

else{
  Serial.print("y value :");
  Serial.println(0);
}
  delay(1000);
 }

so instead of taking average i simply defined dead-zones and neglected +1/-1 values.