Inconsistent Pot reading

Hi, I am having trouble getting a steady value from my pot.

I read that it can often help to average the value so I added a little loop.

Even with this code, my values are fluctuating.

potVal displays from 0 - 1023 but often the value is not steady.

I have tried other pots and get similar issues.

Any ideas?

Thanks.

Phil

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



}

void loop() {
  
  
  
  int potAvg=0;
  int potVal=0;

for (int i=0; i< 25 ; i++) potAvg += analogRead(2); // take an average
potVal = potAvg / 25 ;

Serial.println (potVal);

delay (100);
}

Pots seldom give a "steady" value - in fact, the whole ADC rarely gives the same number twice in a row.

The problem is that of noise. ADCs have a rating of "+/- X LSB" where X is the number of bits of inaccuracy you can expect - in essence these are bits of just noise.

You can "ignore" the noise by reducing the resolution of the ADC - the easiest way is by shifting the result by X bits to the right.

For example:

int adcreading = analogRead(0);
adcreading = adcreading >> 2;

will reduce the resolution from 10 bits (0-1023) to 8 bits (0-255), but with much less noise.

That did work better but it was still a little noisey, I combined this with some averaging and it worked great.

Thanks.

Phil

What is the environment surrounding where you are doing this? Things like Florescent lights can really add a lot of electrical noise to the local environment. You might rtry putting a piece of aluminum foil under a piece of poster board and grounding the alluminum foil. Use this as a works surface and see what happens. Sometimes a slight modification of the environment can make a big difference.

what is the value of the pot?

10k pot

Well, 10k should be o'k. How it's wired and is any high current load arduino controls (motors, solenoid)? I mean, what else have you connected to inputs/outputs except pot?

ADCs have a rating of "+/- X LSB" where X is the number of bits of inaccuracy you can expect

Not quite true, accuracy and repeatability are two different things.

What the OP was referring to was repeatability. Any analogue to digital converter will return plus or minus the least significant bit. Any extra variations are don to noise pickup. This can be improved by adding a 0.1uF capacitor from the analogue input to ground. Also as mentioned averaging can also help get a more repeatable reading.

Accuracy is how close to the voltage reading is to the actual voltage being input.

Thanks, there is very little else going on. I have a small shield that I made that has 3 transistors for controlling LEDs but even when they are not receiving power I get the issue.

The shifting bits and averaging works great for now.

Thanks,

Phil

You can use shielded cable to help especially if the cable is more than very short.