Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 02: Capacitive Soil Moisture Module
In this lesson, you will learn how to use a capacitive soil moisture sensor with an ESP32 Development Board to read the moisture level of soil. We’ll cover connecting the sensor to pin 25, reading its analog value, and interpreting these readings to determine the soil’s moisture level. This project is ideal for beginners as it provides hands-on experience in working with sensors and understanding analog input on the ESP32 platform.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
Wiring
Code
Code Analysis
Defining the sensor pin:
This line of code declares a constant integer
sensorPinand assigns it the value of25, which is the pin the sensor is connected to.const int sensorPin = 25;
Setup function:
The
setup()function is executed once when the program starts. It initializes serial communication at 9600 baud rate. This setup is necessary for sending data to the serial monitor.void setup() { Serial.begin(9600); }
Loop function:
The
loop()function runs continuously aftersetup(). It reads the sensor value from pin A0 usinganalogRead()and prints this value to the serial monitor. Thedelay(500)statement pauses the loop for 500 milliseconds before the next reading, thus controlling the rate of data acquisition.void loop() { Serial.println(analogRead(sensorPin)); delay(500); }