Soil Moisture Sensor with ESP32 Lolin32
Posted: Tue Jun 08, 2021 7:41 am
Hey guys! I have the following ESP32 and I have to connect multiple sensors to it. So far, all my sensors required digital pins , and I’ve used pins like GPIO 32 33 25 etc, both as input and outputs.
I have a sensor for soil moisture that requires analog input, so I can read his values and map it so I have a percentage display. I tried different pins and none works , and I got really confused which ones I can actually use for analog input data.
The reading I get is 4095 , probably because all pins output 3.3V and converted to digital is 4095.
So, which pins can I use to read analog values ?
Thanks !
I have a sensor for soil moisture that requires analog input, so I can read his values and map it so I have a percentage display. I tried different pins and none works , and I got really confused which ones I can actually use for analog input data.
The reading I get is 4095 , probably because all pins output 3.3V and converted to digital is 4095.
So, which pins can I use to read analog values ?
Code: Select all
// Sensor pins
#define sensorPower 26 // I use this to power the sensor only when I read data. The lifespan of this sensor is really low , I do need to buy a better one, but this has to do for now.
#define sensorPin **CANT FIND A PIN THAT WORKS**
void setup() {
pinMode(sensorPower, OUTPUT);
// Initially keep the sensor OFF
digitalWrite(sensorPower, LOW);
Serial.begin(9600);
}
void loop() {
//get the reading from the function below and print it
Serial.print("Analog output: ");
Serial.println(readSensor());
delay(1000);
}
// This function returns the analog soil moisture measurement
int readSensor() {
digitalWrite(sensorPower, HIGH); // Turn the sensor ON
delay(10); // Allow power to settle
int val = analogRead(sensorPin); // Read the analog value form sensor
digitalWrite(sensorPower, LOW); // Turn the sensor OFF
return val; // Return analog moisture value
}