In this tutorial, we will learn how to use the SunFounder ESP32 along with a soil moisture sensor to measure the moisture level in soil, which can help in automating irrigation systems. By monitoring the moisture levels, we can trigger a buzzer when the moisture falls below a certain threshold, indicating that the plants need watering. This project is valuable for maintaining plant health and can be expanded for various environmental sensing applications. (in video at 00:00)
soil_mositure_module
We will be using a capacitive soil moisture sensor, which operates by measuring the change in capacitance between two plates that varies based on the moisture content of the surrounding medium. This sensor will provide an analog output that reflects the moisture level, allowing us to make decisions based on the readings. The ESP32 will read the analog output and activate a buzzer if the soil is too dry.
Hardware Explained
For this project, the primary components are the SunFounder ESP32 microcontroller, the capacitive soil moisture sensor, and a buzzer. The ESP32 features built-in Wi-Fi and Bluetooth, enabling remote monitoring and control. The soil moisture sensor consists of two plates that measure capacitance, which changes depending on the moisture level in the soil. This change in capacitance alters the output voltage, which the ESP32 reads through its analog input. The buzzer will alert us when the moisture level drops below a set threshold.
soil_mositure_module_schematic
Datasheet Details
Manufacturer
SunFounder
Part number
Soil Moisture Sensor
Operating voltage
3.3 to 5 V
Output type
Analog
Signal range
0 to 3.3 V
Response time
Fast
Dimensions
Approx. 60 x 20 mm
Notes / variants
Capacitive type sensor
Ensure the sensor does not get fully immersed in water to prevent damage.
Use appropriate pull-up resistors if necessary for stable readings.
Calibrate the sensor for your specific soil type for accurate moisture readings.
Maintain proper wiring connections to avoid signal loss.
Be cautious of the power supply to avoid over-voltage situations.
Wiring Instructions
ESP32-24-soild-moisture-wiring
To wire the soil moisture sensor to the ESP32, connect the VCC pin of the sensor to the 3.3V pin on the ESP32. Next, connect the GND pin of the sensor to a GND pin on the ESP32. The analog output pin from the soil moisture sensor should be connected to the analog pin 35 on the ESP32. For the buzzer, connect the positive pin to digital pin 13 on the ESP32 and the negative pin to GND. Ensure all connections are secure to avoid any intermittent issues during operation.
Code Examples & Walkthrough
Below is a brief excerpt from the code that initializes the serial communication and reads the moisture sensor value:
void setup() {
// Initialize serial communication at 115200 bits per second:
Serial.begin(115200);
}
This snippet sets up the serial communication, allowing us to view the readings from the moisture sensor in the serial monitor.
Next, we have the main loop where the analog value is read from the sensor:
void loop() {
// Read the analog value
int analogValue = analogRead(35);
// Print out the values
Serial.printf("Analog value = %d\n",analogValue);
delay(300); // delay between reads for clear read from serial monitor
}
In this loop, the code continuously reads the analog value from pin 35 and prints it to the serial monitor every 300 milliseconds. This allows us to monitor the moisture level in real-time.
Demonstration / What to Expect
When the moisture sensor is placed in dry soil, the analog value will be higher, indicating low moisture. Conversely, when the sensor is inserted into moist soil, the value will drop significantly. If the reading falls below the defined threshold (e.g., 2000), the buzzer connected to pin 13 will activate, signaling that the plant needs watering. Be cautious of floating inputs and ensure the sensor is not exposed to water beyond its recommended limits (in video at 02:30).
Video Timestamps
00:00 Start
1:51 Introduction to the project
5:53 Wiring explained
7:23 Arduino Code explained
9:09 Selecting ESP32 Board and COM port in Arduino IDE
10:47 Project Demonstration on the plant pot
14:30 Turning buzzer ON when pot is dry
图像
ESP32-24-soild-moisture-wiring
ESP32-24-soild-moisture-schematic
soil_mositure_module
soil_mositure_module_schematic
ESP32-24-soild-moisture-wiring
ESP32-24-soild-moisture-schematic
soil_mositure_module
soil_mositure_module_schematic
823-ESP32 Tutorial 24/55- Arduino code for soil moisture measurement