Receiving erratic values for potentiometer - WITHOUT touching it/moving it

Howdy,
[EDIT: Earlier I had a ton of script/etc, but realized it's more simple, so I removed all the code and a Fritzing diagram]

I have a Potentiometer connected to A5 and am trying to read its value and set as a variable. However, despite my NOT touching/turning the potentiometer, I get constantly fluctuating values.

Here's the code:

int potvalue;
int curvalue = -1;

void setup(){
  Serial.begin(9600);
  pinMode(A5, INPUT);
}

void loop(){
  potvalue = analogRead(A5);
  if (potvalue != curvalue){
    Serial.print("Pot value is: ");
    Serial.println(potvalue);
    curvalue = potvalue;
  }
}

And the serial output - again, I am not turning or otherwise touching the potentiometer:

Pot value is: 130
Pot value is: 128
Pot value is: 130
Pot value is: 127
Pot value is: 129
Pot value is: 128
Pot value is: 129
Pot value is: 128
Pot value is: 125
Pot value is: 130
Pot value is: 128
Pot value is: 109

So as you can see, it has a pretty wide range that it's reading. Why isn't it getting a single, stable number?

I'm a novice, but i've scanned a lot of these threads, and i'm pretty sure you need to set ranges for the potentiometer,

eg

120-130 = speed x

Qdeathstar:
I'm a novice, but i've scanned a lot of these threads, and i'm pretty sure you need to set ranges for the potentiometer,

eg

120-130 = speed x

Hm, I see what you mean - although I don't think I should have to. The sketch/library I am using is pretty darn detailed, so I'd think they would have considered that and included it in the code.

I am going to create a new sketch and JUST get the potentiometer reading, and see if it still gives me errant ranges, or if it can stay "steady" , to help troubleshoot.

Fritzing diagrams are nearly useless, but it appears that you do not have everything properly grounded to the Arduino. That can cause such behavior.

You seem to have neither ground nor +5V on your breadboard connected correctly.

Piethon:
So as you can see, it has a pretty wide range that it's reading. Why isn't it getting a single, stable number?

This behaviour is almost always a "floating" input.

jremington:
Fritzing diagrams are nearly useless, but it appears that you do not have everything properly grounded to the Arduino. That can cause such behavior.

Would I need a resistor or something? I revised my original post - now I just have the pot. hooked up to A5, with a wire to Ground and a wire to 5V. (hopefully that edit clarified things).

Hm, I see what you mean - although I don't think I should have to. The sketch/library I am using is pretty darn detailed, so I'd think they would have considered that and included it in the code.

Your petulant attitude unfortunately cannot defy the laws of physics, not matter how darn detailed your sketch is. If you sample a constant voltage with an a/d converter, you are going to get successive readings which differ by one or two counts. That's just the way it is. If the person who wrote yoir detailed sketch considered that issue, they obviously never tested it.

"Setting a range" of inputs, as suggested above, isn't going to fix the problem for a pot input that can be turned to any value. Setting a range of 120-130 is going to work when you expect an input value of 125, or very close to it. Having a range is still going to have problems if your input is 130, you might get 131. What you need is logic that locks into a stable value and then ignores changes of +/- 1 or 2, until you get a change which is more than 2.

michinyon:

Hm, I see what you mean - although I don't think I should have to. The sketch/library I am using is pretty darn detailed, so I'd think they would have considered that and included it in the code.

Your petulant attitude unfortunately cannot defy the laws of physics, not matter how darn detailed your sketch is. If you sample a constant voltage with an a/d converter, you are going to get successive readings which differ by one or two counts. That's just the way it is. If the person who wrote yoir detailed sketch considered that issue, they obviously never tested it.

"Setting a range" of inputs, as suggested above, isn't going to fix the problem for a pot input that can be turned to any value. Setting a range of 120-130 is going to work when you expect an input value of 125, or very close to it. Having a range is still going to have problems if your input is 130, you might get 131. What you need is logic that locks into a stable value and then ignores changes of +/- 1 or 2, until you get a change which is more than 2.

Thanks - and I sincerely didn't mean to be petulant or flippant. I apologize that it came off that way. The person who made the code even posted video of them using it - so I (I guess incorrectly) would have thought that this issue would have happened to them as well, and they would have fixed it or made some note somewhere about the input readings.

michinyon:
What you need is logic that locks into a stable value and then ignores changes of +/- 1 or 2, until you get a change which is more than 2.

I'm trying to figure out how to do that - does anyone have any tips or help?

you can try using map to widen the increments...

int potvalue;
int curvalue = -1;

void setup(){
  Serial.begin(9600);
  pinMode(A5, INPUT);
}

void loop(){
  potvalue = map(analogRead(A5),0, 1023, 0, 255);
  if (potvalue != curvalue)
  {
    Serial.print("Pot value is: ");
    Serial.println(potvalue);
    curvalue = potvalue;
  }
}

or create a band around the value:

int potvalue;
int curvalue = -1;

void setup(){
  Serial.begin(9600);
  pinMode(A5, INPUT);
}

void loop(){
  potvalue = analogRead(A5);
  if ((potvalue + 5) <= curvalue || (potvalue - 5) >= curvalue)
  {
    Serial.print("Pot value is: ");
    Serial.println(potvalue);
    curvalue = potvalue;
  }
}

BulldogLowell:
or create a band around the value:

int potvalue;

int curvalue = -1;

void setup(){
  Serial.begin(9600);
  pinMode(A5, INPUT);
}

void loop(){
  potvalue = analogRead(A5);
  if ((potvalue + 5) <= curvalue || (potvalue - 5) >= curvalue)
  {
    Serial.print("Pot value is: ");
    Serial.println(potvalue);
    curvalue = potvalue;
  }
}

Oooh yeah, that seems to do the trick! Thanks!

Now - quick (slightly tangential) question - I have some other sensors (rocker switch, push button, 8x8 LED matrix) wired into digital outputs. When they are wired (although omitted from code), the smallest value I get from the potentiometer is about 129 and the largest is about 903, why isn't it 0 and 1023?

I just removed all wiring except A5, and the 5V and GND, and my potentiometer correctly gives me 0 for smallest value and 1023 for largest - so it seems to me that having other things wired into the Arduino is affecting the readable values? Or am I missing/overlooking something?

my guess is that the LCD display is drawing a lot of power and affecting Vin.

are you powering it with your aduino? you may have to power it separately (still common ground) .

try disconnecting just the LCD display and see

BulldogLowell:
my guess is that the LCD display is drawing a lot of power and affecting Vin.

are you powering it with your aduino? you may have to power it separately (still common ground) .

try disconnecting just the LCD display and see

Good call - it was the LCD.

To power it all together (the potentiometer, push button, rocker switch) - I have a 9V battery with the power and ground connector ( like this one). If I plug that into the Arduino, will that provide enough power?

Or should I use one like this and connect the red/black directly to the breadboard?

edit: I also have a 4 AA battery pack, like this one if that would be preferable over the 9V...

If in fact your grounds are correct and it looks like they aren't or or the pot is very noisy from your measurement data, then making a simple running average might well be your easy solution..
You might also try a smoothing (low pass) capacitor from the pot wiper to ground. 50 to 100 nF is a reasonable starting point and I'd use the smallest value that returned a stable measurement.
I would also if 'twere me... to also try a separate power supply for the controller board only (A battery big enough to supply the same current as the USB power. this will eliminate the possibility of a ground loop causing mains ground noise to modify the measurement.

Doc

Piethon:

BulldogLowell:
my guess is that the LCD display is drawing a lot of power and affecting Vin.

are you powering it with your aduino? you may have to power it separately (still common ground) .

try disconnecting just the LCD display and see

Good call - it was the LCD.

To power it all together (the potentiometer, push button, rocker switch) - I have a 9V battery with the power and ground connector ( like this one). If I plug that into the Arduino, will that provide enough power?

Or should I use one like this and connect the red/black directly to the breadboard?

edit: I also have a 4 AA battery pack, like this one if that would be preferable over the 9V...

you could use a 7805 voltage regulator off the 9V battery to the LCD and wire the arduino in parallel, it has its own voltage regulator.

recommend that you look onto cutting the power to the LCD and activate it on a pushbutton... the LCD will burn up that battery quite quickly.

onlythis one http://zeldor.biz/wp-content/uploads/2012/09/BatteryPack.png is likely to sovle your issues..
For My development work I use a 7.5V 2A power supply plugged into the DC in connector, Mainly because after the protection diode and the linear regulator losses it provides ~ 7V to the regulator which has an ~ .6V dropout voltage which is more than enough to keep the 5V dc supply in regulation (7.2V DC at the regulator input and since only 1 V is the maximum required difference between the source and the load voltage X the load current keeps the loss heating of the AMS1117-5.0 to a minimum .1A X 1.?1 V = ~.1 W or 100 mW energy lost as heat.. IHTH...

Doc

@ BulldogLowell... The 7805 is less than an optimal solution as it has a 2.5 V overhead or dropout voltage thus requiring nearly 8V for proper operation which will not enable use of all the capacity of the battery as it will fail when the Vin drops to 7.5V (Ideally) to the regulator and since most 9V batteries will drop to 8 to 8.4 V rather quickly... I also use a 470 to 1000 uF capacitor across the battery because the internal resistance of a battery must increase as the battery loses capacity due to use...
It can be as much as 30 - 40% with light loads, Worse by far as the load current increases due to the increasing losses due to the changes in the battery
The solution mandates a low dropout voltage linear regulator, ie one with less than ~ .8 V dropout voltage @ full load current, otherwise your reasoning is spot on.
For my style of engineering (Murphy was an Optimist) using a 9V battery on an LM7805 leaves no useful margin...
Whenever possible I use a cellphone charger to the 5V output on the PCB, else a 7.5V wallwart to the Vin or DC in..
I really like the mains operated cell phone chargers as they are nearly free... most all fail due to the device connector wear But I've Never had one fail and put out more than the rated voltage...
Simply because they are free or nearly so... Typically $0.75 at any thrift store in Southern California...

Doc

Docedison:
I really like the mains operated cell phone chargers as they are nearly free... most all fail due to the device connector wear But I've Never had one fail and put out more than the rated voltage...
Simply because they are free or nearly so... Typically $0.75 at any thrift store in Southern California...

Then, how do you then plug the phone charger into the battery? :blush:

I was assuming the OP wanted portable power, and there are many ways to power his Arduino/LCD I was just showing an option to get regulated voltage from 9V to 5. that being said, there are better ways...

You might also try a smoothing (low pass) capacitor from the pot wiper to ground. 50 to 100 nF is a reasonable starting point and I'd use the smallest value that returned a stable measurement.

Thanks! This worked fine for me, I had to use a 22uF capacitor because I need very precise readings! It "drags" the value a little, but it's not too noticeable.