Recently, I have become fascinated with the ESP32 development board and wanted to learn more about it. I happened to purchase a resistive soil moisture sensor, which is shown below:
I thought of using the ESP32 to test this sensor.
The sensor has four pins: VCC (power), GND (ground), DO (digital signal), and AO (analog signal).
Therefore, when connecting the wiring, AO needs to be connected to a pin on the ESP32 that supports analog signals. The various interfaces of the ESP32 are as follows:
Thus, I connected DO and AO to D32 and D33, respectively.
I also connected a 0.96-inch OLED display to show values and information.
The circuit connection is as follows:
Then the code is as follows:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width in pixels
#define SCREEN_HEIGHT 64 // OLED display height in pixels
// Declaration of the SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F(“SSD1306 allocation failed”));
for(;;);
}
delay(500);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
}
void loop() {
int counts = analogRead(32);
Serial.println(counts);
display.clearDisplay();
display.setCursor(0, 15);
display.println(counts);
if((int)counts<2000){
display.print(“damp!!!”);
}else{
display.print(“dry!!!”);
}
display.display();
delay(2000);
}
Set the display to show “damp” when the moisture value is below 2000, and “dry” when above 2000.
Next, I used dry and wet paper towels to conduct a comparative experiment:
Ultimately, we can observe the values and information measured by the sensor based on the wetness of the paper towels.