ESP32 MicroPython Soil Moisture Sensor
This tutorial instructs you how to use a moisture sensor with the ESP32 and MicroPython. In detail, We will learn:
- Differences between resistive and capacitive moisture sensors
- How to connect the soil moisture sensor to the ESP32
- How to write MicroPython code for ESP32 to read value from the soil moisture sensor
- How to write MicroPython code for ESP32 to check if soil is wet or dry
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Soil Moisture Sensor Sensor
There are two types of moisture sensors:
- Resistive moisture sensor
- Capacitive moisture sensor
Both sensors measure soil moisture, but they work differently. We strongly recommend using the capacitive moisture sensor for these reasons:
- The resistive moisture sensor rusts over time. This happens because an electric current passes between its probes, causing corrosion.
- The capacitive moisture sensor does NOT rust over time. Its parts are protected, and no electric current flows between them.
The image below shows the rusting on a resistive soil moisture sensor.
The rest of this guide will focus on how to use the capacitive soil moisture sensor.
Capacitive Soil Moisture Sensor Pinout
A capacitative soil moisture sensor has three pins:
- GND pin: connect to GND (0V)
- VCC pin: connect to VCC (5V or 3.3V)
- AOUT pin: sends a changing signal based on soil moisture. Connect to the analog input on a ESP32.
How It Works
The more water there is in the soil, the lower the voltage at the AOUT pin.
Wiring Diagram
- How to connect ESP32 and soil moisture sensor using breadboard
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and soil moisture sensor using screw terminal block breakout board (powered via USB cable)
ESP32 MicroPython Code
Detailed Instructions
Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Confirm that MicroPython firmware is loaded on your ESP32 board.
- If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
- Connect the ESP32 board to the soil moisture sensor according to the provided diagram.
- Connect the ESP32 board to your computer with a USB cable.
- Open Thonny IDE on your computer.
- In Thonny IDE, go to Tools Options.
- Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
- Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
- Copy the provided MicroPython code and paste it into Thonny's editor.
- Save the code to your ESP32 by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device.
- Name the file main.py.
- Click the green Run button (or press F5) to execute the script.
- Insert the sensor into the soil and water it, or you can dip it slowly into a cup of salt water.
- Check out the message in the Shell at the bottom of Thonny.
※ NOTE THAT:
- Don’t use pure water for testing, as it doesn’t conduct electricity and won’t impact the sensor readings.
- Sensor readings will never be zero. It's normal for the values to range between 1200 and 2300, but this can change based on factors like how deep the sensor is placed, the type of soil or water, and the power supply voltage.
- Keep the circuit part (at the top of the sensor) out of the soil or water to avoid damaging the sensor.
Calibrating the Capacitive Soil Moisture Sensor
The sensor's readings change based on the soil type and its water content. To use it correctly, we need to set a point that shows if the soil is wet or dry.
Steps to Calibrate:
- Run the provided code on the ESP32.
- Insert the moisture sensor into the soil.
- Gradually add water to the soil.
- Watch the message in the Shell at the bottom of Thonny.
- Write down the value when the soil changes from dry to wet. This value is known as the THRESHOLD.
Determine if the soil is wet or dry
After you calibrate, update the THRESHOLD value you recorded with this new code. This code determines if the soil is wet or dry.
Check out the message in the Shell at the bottom of Thonny.
※ NOTE THAT:
This tutorial demonstrates how to use the adc.read() function to read values from an ADC (Analog-to-Digital Converter) connected to a soil moisture sensor. The ESP32's ADC is suitable for projects that do not require high precision. However, if your project needs accurate measurements, keep the following in mind:
- The ESP32 ADC is not perfectly accurate and may require calibration for precise results. Each ESP32 board may vary slightly, so calibration is necessary for each individual board.
- Calibration can be challenging, especially for beginners, and might not always yield the exact results you desire.
For projects requiring high precision, consider using an external ADC (e.g., ADS1115) with the ESP32 or opt for an Arduino, which has a more reliable ADC. If you still wish to calibrate the ESP32 ADC, refer to the ESP32 ADC Calibration Driver.