Analog inputs reading stray voltage?

Hello, has anyone had issues with an analog sensor's voltage moving up and down in accordance with a the turning of a dial on a potentiometer? And if so, what is happening?

In the following sketch, whenever I adjust the potentiometer, the reading from the sensor moves up or down in tandem with the reading from the potentiometer.

// ----- Plant watering system with knob ----- //

int potPin = A0;                                // pot pin on pin A0
int sensorPin = A4;                             // soil sensor on pin A1
int pumpPin = 8;                                // water pump on pin 8
float sensorValue = 0;                          // value read from soil sensor
float potValue = 0;                                 // value from potPin

void setup() {
  pinMode(potPin,INPUT);                        
  pinMode(sensorPin,INPUT);
  pinMode(pumpPin,OUTPUT);
  Serial.begin(9600);       
}

void loop() {
  for(int i=0; i<=100; i++){                                  // counter to 100
    sensorValue = sensorValue + analogRead(sensorPin);        // add new reading from the moisture to the float sensorValue
    potValue = potValue + analogRead(potPin);                 // add new reading from the pot pinto float potValue
    delay(1);   
  }
  sensorValue = sensorValue/100.0;                            // divide the total for sensorValue by 100 and display results - this will give the average of 100 readings
  potValue = potValue/100.0;                                  // average the potValue
  
  if(sensorValue >= potValue){                                // if the soil is drier than the set point from the pot
    digitalWrite(pumpPin,HIGH);                               // turn on the pump for 2 minutes
    delay(2000);                                             
    digitalWrite(pumpPin,LOW);                                // turn pump off
    delay(2000);
  }  
  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);                                  // print moisture level
  Serial.print("  Pot Value: ");
  Serial.println(potValue);                                   // print the moisture setting
  delay(300);

  sensorValue = 0;
  potValue = 0;
}

Hmmmm. Is your supply voltage going up and down with the pump? How is the pump powered, and what specs on the pump?

Definitely you need to provide hardware details.

It's probably a hardware/wiring problem but you can try moving delay() to between the two analog readings. There is only one shared analog-to-digital converter and it takes a little time to "settle".

If the output impedance of the analog sensor is high, there may not be enough time to charge and discharge the sample and hold capacitor charged by the POT.
Since the sample and hold capacitor is not intentionally discharged by uC between each measurement, it can be solved by sandwiching the GND measurement so that it is connected to GND once between measurements, or by connecting a buffer to the output of the your analog sensor.

The following post is not wrote in English, but I have explained something similar in the past.
It may help if you can read it with using Google Translator.

That only changes the order of successive reads. It's in a loop.

Please show us the wiring and a good schematic.

for(int i=0; i<=100; i++){                                  // counter to 100
    sensorValue = sensorValue + analogRead(sensorPin);        // add new reading from the moisture to the float sensorValue
    potValue = potValue + analogRead(potPin);                 // add new reading from the pot pinto float potValue
    delay(1);   
  }

You dont say which ardiuino module you are using; however reading the values alternately in rapid succession is not good practise - you need to let the adc input settle to the new value.
Also for smoothing it makes more sense to use a power of two (eg 64) - microcomputers dont count on their fingers.

You dont need float resolution so you can keep all in integers.

As you are using a soil sensor you dont need a fast response so perhaps try something like this

unsigned int sensorValue = 0;                          // value read from soil sensor
unsigned int potValue = 0;                                 // value from potPin

void loop() {
  for(int i=0; i<=64; i++){                                  // counter to 64
    sensorValue = sensorValue + analogRead(sensorPin);   // add new reading from the moisture sensor       
delay(8);   //this counted loop takes 64*8 msec = 512 msec
  }

for(int i=0; i<=64; i++){                                  // counter to 64
      potValue = potValue + analogRead(potPin);    // add new reading from the pot pin 
       delay(8);    //this counted loop takes 64*8 msec = 512 msec
  }
//all above takes about 1 second

  sensorValue = sensorValue/64;   // divide the total for sensorValue by 64 and display results - this will give the average of 64 readings
  potValue = potValue/64;          

If both pot and sensor have output impedance of 10k or less this should not be a problem. If you provide full details of the hardware we can figure this out.
The sensor might be ratiometric or not - this can be an issue - again full details of the sensor please - and circuit of course.

If your Arduino uses an 8-bit AVR microcontroller you need to use long for sensorValue and potValue as int on these Arduinos cannot count beyond 32767,
and you are summing 100 lots of a value in the range 0..1023, which could be upto 102300

1 Like

@MarkT thanks Mark, yes i'm too used to the ESP32.
I've edited it to be unsigned int - which will be fine with the 64 count loop.