fluctuation in values of potentiometer/knob

Hi All,

I have connected a potentiometer/knob to arduino. the circuit is doing perfect. When I am uploading AnalogInOutSerial from the examples>analog, I can see the values of the potentiometer in the serial monitor.

However I have noticed that the values of the potentiometer are fluctuating even if I do not rotate the potentiometer/knob.

Can someone please explain the cause for this and also the solution.

Thanks,

shanky

However I have noticed that the values of the potentiometer are fluctuating even if I do not rotate the potentiometer/knob.

Over what range? A variation of +/- 1 in the output of analogRead() is normal/expected. A larger variation is not.

and also the solution.

That depends on what the problem is. Why is the variation causing you problems?

i also get that.
maybe you can get someone else to explain you why it happens (i actually have no idea. i think something to do with how the voltave is mapped into the value range of the pot, but i maybe be super wrong!!!)

Maybe you could put your

  analogWrite(analogOutPin, outputValue);

inside a if statment. something like:

const int analogInPin = A0; 
const int analogOutPin = 9;

int mappedPotValue = 0;
int noFloat = 2;
int lastPotValue = 0;
int currentPotValue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  currentPotValue = analogRead(analogInPin);            
  mappedPotValue = map(currentPotValue, 0, 1023, 0, 255);  

  if (abs(mappedPotValue - lastPotValue) >= noFloat) {
    analogWrite(analogOutPin, currentPotValue);           

    Serial.print("sensor = " );                       
    Serial.print(currentPotValue);      
    Serial.print("\t output = ");      
    Serial.println(currentPotValue); 

    lastPotValue = mappedPotValue;
  }
}

But i am a really noob, so please take my advice with caution! :wink:

mappedPotValue = map(currentPotValue, 0, 1023, 0, 255);

That seems like an unnecessary way to shift the value to the left by two:

mappedPotValue = currentPotValue >> 2

Thank You to all for replying.

@PaulS : That depends on what the problem is. Why is the variation causing you problems?

Well I am controlling a flash animation through the potentiometer/knob. Since the x direction of the movieclip is dependent on the value of the potentiometer/knob, the variation in the values of potentiometer/knob makes the animation to shake, giving it a weird feel. This is a problem for me. The variation is +/- 1.

Shanky

You could average a number of readings, or perform a rolling average.

The below is for using pots to control servos, but may be similar to what you are doing. The code has a dead band setup that filters out the small variations seen to limit hunting behavior. The code has a delay so the output and changes are more easily seen on the serial monitor for testing. You don't have to have servos attached to try the code.

//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;
Servo myservo2;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;

int newval1, oldval1;
int newval2, oldval2;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){  
    myservo1.write(newval1);
    Serial.print("1- ");
    Serial.println(newval1);
    oldval1=newval1;
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }
  delay(50);
}