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:
- Differences between resistive and capacitive moisture sensors
- Steps to program a Raspberry Pi Pico to measure moisture using a capacitive sensor
- Instructions to calibrate a capacitive moisture sensor
- Methods to determine if soil is moist 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 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.
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.
How It Works
The more water there is in the soil, the lower the voltage at the AOUT pin.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code
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.
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:
- Run the given code on the Raspberry Pi Pico.
- Place the moisture sensor in the soil.
- Slowly pour water into the soil.
- Watch the Shell at the bottom of Thonny.
- 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.
The result displayed in the Shell at the bottom of Thonny.