Raspberry Pi Pico - Soil Moisture Sensor

In this guide, we will learn to use a moisture sensor with the Raspberry Pi Pico. We will discuss:

Raspberry Pi Pico soil moisture sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Capacitive Soil Moisture Sensor
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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 kinds of moisture sensors.

  • Capacitive humidity sensor
  • Resistive humidity sensor

Both sensors provide information on soil moisture, but they function differently. We recommend using the capacitive moisture sensor for this reason:

  • The resistive soil moisture sensor slowly breaks down. This occurs as there is an electrical current flowing between its probes that causes damage known as electrochemical corrosion.
  • The capacitive soil moisture sensor does not deteriorate over time. This is because its electrodes are protected and do not corrode easily.

The picture shows a moisture sensor for soil, made from a material that wears out and rusts as time passes.

resistive soil moisture sensor corroded

The rest of this tutorial will concentrate 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 Raspberry Pi Pico.
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

The wiring diagram between Raspberry Pi and Pico soil moisture sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-soil-moisture-sensor """ from machine import ADC, Pin import time AOUT_PIN = 26 # The Raspberry Pi Pico GP26 (ADC0) pin connected to the moisture sensor # Initialize the analog pin for reading moisture_sensor = ADC(Pin(AOUT_PIN)) # Infinite loop while True: value = moisture_sensor.read_u16() # Read the moisture level from the sensor value_12bit = value >> 4 # Convert 16-bit reading to 12-bit (real value from 12-bit ADC)by shifting right 4 bits print("Moisture: {}".format(value_12bit)) # Print the 12-bit moisture level to the serial monitor time.sleep(0.5) # Pause the loop for 500 milliseconds

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the soil moisture sensor according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • 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: 2324 Moisture: 2324 Moisture: 2300 Moisture: 2264 Moisture: 2224 Moisture: 2188 Moisture: 2156 Moisture: 2120 Moisture: 2084 Moisture: 2052 Moisture: 2024 Moisture: 2000 Moisture: 1980 Moisture: 1968 Moisture: 1960 Moisture: 1956 Moisture: 1952
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

※ NOTE THAT:

  • Avoid using only water for tests because it does not carry electricity, and it will not change the sensor measurements.
  • Sensor values typically do not drop to zero. For example, They are often between 2000 and 2400. These numbers might change based on the depth of the sensor in the soil or water, the kind of soil or water, and voltage of the power source.
  • Keep the top part of the sensor, which contains the circuit, out of the soil or water to prevent damage to the sensor.

Calibration for Capacitive Soil Moisture Sensor

The measurement from the moisture sensor changes depending on the soil type and how much water it has. To use it properly, we need to adjust it to determine a point that indicates whether the soil is wet or dry.

How to do Calibration:
  1. Run the given code on the Raspberry Pi Pico.
  2. Place the moisture sensor in the soil.
  3. Slowly pour water into the soil.
  4. Watch the Shell at the bottom of Thonny.
  5. Note the value when the soil changes from dry to wet. This value is called 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 Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-soil-moisture-sensor """ from machine import ADC, Pin import time AOUT_PIN = 26 # The Raspberry Pi Pico GP26 (ADC0) pin connected to the moisture sensor THRESHOLD = 2120 # Set threshold value for moisture level # Initialize the analog pin for reading moisture_sensor = ADC(Pin(AOUT_PIN)) # Infinite loop while True: value = moisture_sensor.read_u16() # Read the moisture level from the sensor value_12bit = value >> 4 # Convert 16-bit reading to 12-bit by shifting right 4 bits 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

The result displayed in the Shell at the bottom of Thonny.

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The soil is DRY (2324) The soil is DRY (2300) The soil is DRY (2264) The soil is DRY (2188) The soil is DRY (2156) The soil is WET (2084) The soil is WET (2052) The soil is WET (1968) The soil is WET (1952)
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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!