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

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Capacitive Soil Moisture Module

BUY

Breadboard

BUY

Wiring

../_images/Lesson_02_Capacitive_Soil_Moisture_Module_esp32_bb.png

Code

Code Analysis

  1. Defining the sensor pin:

    This line of code declares a constant integer sensorPin and assigns it the value of 25, which is the pin the sensor is connected to.

    const int sensorPin = 25;
    
  2. 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);
    }
    
  3. Loop function:

    The loop() function runs continuously after setup(). It reads the sensor value from pin A0 using analogRead() and prints this value to the serial monitor. The delay(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);
    }