ESP32 use 2 analog pin looks like reading the same pin

I test to read data from 2 analog pin 33 and 35 like this code.

int soil_pin = 35;
int rain_pin = 33;

int SoilRead(){

  int v;
  v = analogRead(soil_pin);
  v = map(v, 0, 4095, 100, 0);
  return v;
}

String RainRead(){

  int v;
  String st;
  v = analogRead(rain_pin);
  v = map(v, 0, 4095, 100, 0);

  st = "No Rain";
  if(v > 0){
    st = "Raining";
  }
  return st;
}



void setup() {
  Serial.begin(9600); // set up serial port for 9600 baud (speed)
  delay(500); // wait for display to boot up
}

void loop() {
  
  Serial.print(SoilRead());
  Serial.println(" %");
  Serial.print(RainRead());
  Serial.println("");
  
  delay(500); //wait for half a second, so it is easier to read
}

when I drop water on rain drop sensor the output of soil sensor also increase. when I wipe the water on rain drop sensor then soil sensor back to 0%. How to fix it?


Raining
0 %
Raining
0 %
Raining
9 %
Raining
15 %
Raining
11 %
Raining
11 %
Raining
11 %
Raining
11 %
Raining
10 %
Raining
10 %
Raining
11 %
Raining
11 %
Raining
12 %
Raining
12 %
Raining
13 %
Raining
11 %
Raining
13 %
Raining
11 %
Raining
13 %
Raining
14 %
14 %
Raining
18 %
Raining
0 %
No Rain
0 %
No Rain
2 %
Raining
0 %
No Rain
0 %

Please post a schematic of your project

Show a pic of your project and a wiring diagram.

Hi,
Put some Serial.print in your two functions to see what is being read.

Tom... :grinning: :+1: :coffee: :australia:

I edit code to show data in function like this.

#define soil_pin 33
#define rain_pin 35

int SoilRead(){

  int v;
  v = analogRead(soil_pin);
  v = map(v, 0, 4095, 100, 0);
  Serial.print(v);
  Serial.println(" %");
  return v;
}

String RainRead(){

  int v;
  String st;
  v = analogRead(rain_pin);
  v = map(v, 0, 4095, 100, 0);
  Serial.print(v);
  Serial.println("");
  st = "No Rain";
  if(v > 0){
    st = "Raining";
  }
  return st;
}



void setup() {
  Serial.begin(115200);
  pinMode(soil_pin,INPUT);
  pinMode(rain_pin,INPUT);
}

void loop() {

  SoilRead();
  Serial.println("");
  RainRead();
  Serial.println("");
  
  delay(500); //wait for half a second, so it is easier to read
}

The output when I drop water on rain sensor

0 %

0

0 %

0

8 %

62

8 %

61

8 %

61

I'm not do anything with soil sensor. why it can show analog value ?

Hi,
Are your devices happy with 3V3 supply?

If you disconnect the sensors and connect Pin33 to gnd and Pin35 to 3V3.
What do you get?
If you disconnect the sensors and connect Pin35 to gnd and Pin33 to 3V3.

Can you please post a link to spec/data of the two devices?

Do you have a DMM?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

I'm not familiar with the ESP32. Maybe the same rules apply as for the AVR based boards where you need to give the Sample-and-Hold circuit time to adjust to the new input before reading it. This is usually done by reading a pin twice and throwing the first result away.

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