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:

ESP32 MicroPython soil moisture sensor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Capacitive Soil Moisture Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Soil Moisture Sensor Sensor

capacitive moisture sensor vs resistive moisture 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.

resistive soil moisture sensor corroded

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.
capacitive soil moisture sensor pinout

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
The wiring diagram between ESP32 MicroPython soil moisture sensor

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and soil moisture sensor

ESP32 MicroPython Code

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-soil-moisture-sensor """ from machine import ADC, Pin import time AOUT_PIN = 36 # The ESP32 pin GPIO36 (ADC0) connected to the moisture sensor # Initialize the analog pin for reading moisture_sensor = ADC(Pin(AOUT_PIN)) # Set the ADC width (resolution) to 12 bits moisture_sensor.width(ADC.WIDTH_12BIT) # Set the attenuation to 11 dB, allowing input range up to ~3.3V moisture_sensor.atten(ADC.ATTN_11DB) # Infinite loop while True: value = moisture_sensor.read() # Read the moisture level from the sensor print("Moisture: {}".format(value)) # Print the 12-bit moisture level to the serial monitor time.sleep(0.5) # Pause the loop for 500 milliseconds

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.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Moisture: 2223 Moisture: 2255 Moisture: 2279 Moisture: 2313 Moisture: 2350 Moisture: 2383 Moisture: 2395 Moisture: 1260 Moisture: 1325 Moisture: 1271 Moisture: 1261 Moisture: 1254 Moisture: 1261 Moisture: 1264 Moisture: 1360 Moisture: 1258 Moisture: 1263 Moisture: 1266 Moisture: 1258 Moisture: 1239 Moisture: 1264 Moisture: 1307 Moisture: 1488
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

※ 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:
  1. Run the provided code on the ESP32.
  2. Insert the moisture sensor into the soil.
  3. Gradually add water to the soil.
  4. Watch the message in the Shell at the bottom of Thonny.
  5. 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.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-soil-moisture-sensor """ from machine import ADC, Pin import time AOUT_PIN = 36 # The ESP32 pin GPIO36 (ADC0) connected to the moisture sensor THRESHOLD = 1500 # Set threshold value for moisture level # Initialize the analog pin for reading moisture_sensor = ADC(Pin(AOUT_PIN)) # Set the ADC width (resolution) to 12 bits moisture_sensor.width(ADC.WIDTH_12BIT) # Set the attenuation to 11 dB, allowing input range up to ~3.3V moisture_sensor.atten(ADC.ATTN_11DB) # Infinite loop while True: value = moisture_sensor.read() # Read the moisture level from the sensor if value_12bit > THRESHOLD: # Compare sensor reading to threshold print("The soil is DRY ({})".format(value_12bit)) # Print dry status if above threshold else: print("The soil is WET ({})".format(value_12bit)) # Print wet status if below threshold time.sleep(0.5) # Wait for half a second before next read

Check out the message in the Shell at the bottom of Thonny.

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The soil is DRY (2124) The soil is DRY (2000) The soil is DRY (1864) The soil is DRY (1588) The soil is DRY (1556) The soil is WET (1484) The soil is WET (1452) The soil is WET (1368) The soil is WET (1252)
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

※ 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.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!