Not stabe pot readings ...how to fix this?

Hello ,
I am trying to read the potentiometer value, but the readings are not stable.
The board is arduino nano . I try with external power supply , same ...
in serial monitor i have some like this :
1023
1022
1023
1022


int analogInput = 0;

int value ;
int last;
void setup() {

  

pinMode(analogInput, INPUT);
Serial.begin(9600);
delay(1000);
}

void loop() {
  
   value = analogRead(analogInput); // read the value at analog input
    
   if(value!=last){
    last=value;
   Serial.println(value);
   }
     
       ```

Check the 328P datasheet. Analogue readings can always vary by at least on bit.

Your topic has nothing to do with Installation and Troubleshooting (which is about the IDE) and has therefore been moved to a more suitable location on the forum.

SimpleKalmanFilter - Arduino Reference <<< I use this one with 32 bit MCU's.

thank you i was confuse because i read Troubleshooting . I will know now ...

Do you think if i use atmega32u4 i will be able to use this code ?

Any analog-to-digital conversion can be on the "hairy edge" between two digital values and be unstable +/- 1 count.

Smoothing/filtering "slows down" any changes/variations but it's not always perfect.

There is another software method called hysteresis where you essentially ignore small changes.

A capacitor between the analog input and ground can filter some of the analog noise.

Short connections and lower resistance pot values help to reduce analog noise pick-up.

int analogInput = 0;

Does that work?

Normally you should define an analog pin:

int analogInput = A1;  // Assign pin A1 as analogInput

Yes. But you will have the same problem. See reply by @DVDdoug; mine was not clear enough from that perspective.

Thank you ! I will try this ...
I think that we can`t talk about analog noice because the pot is next to the board 3 sm cables .
I try with capacitor before write in this forum , but nothing change ....
will try to add hysteresis in code .
And yes , code work now , but with unstable wast digit ...

Your expectations are unreasonable.
(Assuming wast == last)

I found a solution. I tested it and it works very well

Glad you found the 0-100% version helpful.
I also made a 0-255 version.
Leo..

// converts the position of a 10k lin(B) pot to a byte (0-255)
// pot connected to A0, 5volt and ground

int rawValue; // raw reading
int oldValue; // for deadband
byte Byte; // final byte
byte oldByte; // for printing

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

void loop() {
  // rawValue = analogRead(A0); // add dummy read, if needed
  rawValue = analogRead(A0); // read pot
  if (rawValue < (oldValue - 2) || rawValue > (oldValue + 2)) { // add some deadband
    oldValue = rawValue; // update value
    Byte = oldValue >> 2; // convert 10-bit to 8-bit
    if (oldByte != Byte) { // only print if value changes
      Serial.print("Byte is: ");
      Serial.println(Byte);
      oldByte = Byte; // update value
    }
  }
}

Usually, a combination of a low-pass filter and hysteresis works best. For example: Arduino Helpers: 1.FilteredAnalog.ino

Welcome to the world of analog. Unlike digital measurements, analog measurements are only stable within a small amount.

If your Arduino is powered from 5V then one bit (i.e. 1023-1022 = 1 bit) is 4.88 millivolts. A very small difference.

Any electrical noise or interreference from perturbations on the power supply will result in small changes similar to what you are seeing.

Also note the maximum reading is 1023 so you are really at the maximum input.

It also depends what you want to do with the 10-bit pot reading.
If you're sending it to an 8-bit PWM pin then you can afford loosing a few rogue bits.
Leo..

i want BIN value of the pot to be visualised on Digital pin 13 to pin 7 ( 7 bit )
Lets say if i have pot percentige 127 all pins from 13 to 7 to be = 1 ( high ) 127 is 1111111
If i have 0 this is 0000000 all pins off
If i have 10 -> 0001010
Just to have bin value on PORTD13 to PORTD7 or from D2 to D9 ... doesn`t matter .

// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground

int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;

void setup() 
{

Serial.begin(115200); // set serial monitor to this baud rate, or change the value
DDRD=OUTPUT;
digitalWrite(PORTD,LOW);
}

void loop() 
     {
  
  rawValue = analogRead(A0);
  rawValue = analogRead(A0);  // read input twice
 
  rawValue = constrain(rawValue, 8, 1015);   // ignore bad hop-on region of a pot by removing 8 values at both extremes
 
  if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4))   // add  hysteresis
     {
    oldValue = rawValue;
   
    potPercentage = map(oldValue, 8, 1008, 0, 127);  // convert to percentage

   
    if (oldPercentage != potPercentage)  // Only print if %value changes
       {
      Serial.print("Pot percentage is: ");
      Serial.print(potPercentage);
      Serial.print("% BIN ");
      oldPercentage = potPercentage;
       
        for(int i=6; i>=0; i--)
           {
               bool m = bitRead(potPercentage, i); 
              Serial.print(m, BIN);  //shows: 7 bits only    
           }
    
    PORTD=(potPercentage,BIN);
   Serial.println();
    }  
  }

}

See cppreference: comma operator

In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded.

The type, value, and value category of the result of the comma expression are exactly the type, value, and value category of the second operand, E2.

In other words, what you wrote is equivalent to PORTD = BIN.

Use PORTD = potPercentage instead (but change the variable name, since it's not a percentage).

it was my mistake ... because varible m is a binary , no need -> Serial.print(m, BIN);
only Serial.print(m) work ...
I did it ... with one more varible , decrement for loop ...

you are right ... i must read cppreference: comma operator , thank you !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.