Arduino MicroPython Soil Moisture Sensor

This guide shows you how to use a moisture sensor with Arduino and MicroPython. You will learn:

Arduino MicroPython soil moisture sensor

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Capacitive Soil Moisture Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

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 in different ways. We recommend using the capacitive moisture sensor because:

  • The resistive moisture sensor can rust over time. This happens because an electric current flows 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 rust 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 capacitive 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 signal that changes based on soil moisture. Connect this to the analog input on an Arduino.
capacitive soil moisture sensor pinout

How It Works

The more water in the soil, the lower the voltage at the AOUT pin of the capacitive soil moisture sensor.

Wiring Diagram

The wiring diagram between Arduino MicroPython soil moisture sensor

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-soil-moisture-sensor """ from machine import ADC, Pin import time # Initialize the analog pin for reading moisture_sensor = ADC(Pin('A0')) # The Arduino Giga WiFi pin A0 connected # Infinite loop while True: value = moisture_sensor.read_u16() >> 4 # Read the 16-bit analog value from the sensor and convert back to 12-bit ADC 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 run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the soil moisture sensor according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • 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: 2222 Moisture: 2253 Moisture: 2275 Moisture: 2310 Moisture: 2353 Moisture: 2388 Moisture: 2391 Moisture: 1261 Moisture: 1323 Moisture: 1276 Moisture: 1266 Moisture: 1253 Moisture: 1260 Moisture: 1269 Moisture: 1362 Moisture: 1251 Moisture: 1264 Moisture: 1261 Moisture: 1254 Moisture: 1239 Moisture: 1264 Moisture: 1307 Moisture: 1488
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

※ NOTE THAT:

  • Don’t use pure water for testing; it doesn’t conduct electricity and won’t affect the sensor readings.
  • Sensor readings will never be zero. They usually range from 1200 to 2300, but this can change based on how deep the sensor is, the type of soil or water, and the power supply voltage.
  • Keep the circuit part (the top of the sensor) out of the soil or water to prevent damage.

Calibrating the Capacitive Soil Moisture Sensor

The sensor's readings vary depending on the soil type and how much water it contains. To use it correctly, we need to set a point that indicates if the soil is wet or dry.

Steps to Calibrate:
  1. Run the provided code on the Arduino.
  2. Insert the moisture sensor into the soil.
  3. Slowly add water to the soil.
  4. Check the message in the Shell at the bottom of Thonny.
  5. Note the value when the soil changes from dry to wet. This value is called 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 Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-soil-moisture-sensor """ from machine import ADC, Pin import time THRESHOLD = 1500 # Set threshold value for moisture level # Initialize the analog pin for reading moisture_sensor = ADC(Pin('A0')) # The Arduino Giga WiFi pin A0 connected # Infinite loop while True: value = moisture_sensor.read_u16() >> 4 # Read the 16-bit analog value from the sensor and convert back to 12-bit ADC 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 (2121) The soil is DRY (2004) The soil is DRY (1862) The soil is DRY (1589) The soil is DRY (1554) The soil is WET (1481) The soil is WET (1457) The soil is WET (1363) The soil is WET (1250)
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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!