Raspberry Pi - Soil Moisture Sensor

This tutorial instructs you how to use a moisture sensor with Raspberry Pi. Specifically, we will look at:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Capacitive Soil Moisture Sensor
1×ADS1115 ADC Module
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B

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: the resistive moisture sensor and the capacitive moisture sensor.

Both sensors offer soil moisture information. However, their methods of operation are not similar. We strongly suggest utilizing the capacitive moisture sensor, due to the following:

  • The resistive soil moisture sensor is prone to corrosion over time. This is because electrical current passes between its probes, which leads to electrochemical corrosion.
  • The capacitive soil moisture sensor does corrode with time much slower than the resistive soil moisture sensor. This is because its electrodes are not exposed and are comparatively resistant to corrosion.

This is a picture of a resistive soil moisture sensor that has been damaged due to corrosion.

resistive soil moisture sensor corroded

The remainder of this tutorial will utilize the capacitive soil moisture sensor.

Capacitive Soil Moisture Sensor Pinout

The capacitive soil moisture sensor has three pins:

  • GND pin: This should be connected to the GND (0V)
  • VCC pin: This should be connected to VCC (5V or 3.3v)
  • AOUT pin: This is the analog signal output pin which produces a voltage that is proportional to the soil moisture level. This should be connected to a Raspberry Pi's analog input pin.
capacitive soil moisture sensor pinout

How It Works

The amount of water in the soil has an effect on the voltage in the AOUT pin; the greater the water content, the lower the voltage will be.

Wiring Diagram

The wiring diagram between Raspberry Pi and soil moisture sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code reads value from soil moisture sensor.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Install the Adafruit_ADS1x15 library by running the following commands on your Raspberry Pi terminal:
sudo pip install Adafruit-ADS1x15
  • Create a Python script file soil_moisture.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-soil-moisture-sensor """ import time import Adafruit_ADS1x15 # Create an ADS1115 ADC object adc = Adafruit_ADS1x15.ADS1115() # Set the gain to ±4.096V (adjust if needed) GAIN = 1 # Main loop to read the analog value from the soil moisture sensor and print the raw ADC value try: while True: # Read the raw analog value from channel A3 raw_value = adc.read_adc(3, gain=GAIN) # Print the raw ADC value print("Raw Value: {}".format(raw_value)) # Add a delay between readings (adjust as needed) time.sleep(1) except KeyboardInterrupt: print("\nExiting the program.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 soil_moisture.py
  • Bury the sensor in soil, then pour water into the soil. Or slowly submerge it into a cup of salt water.
  • Check the result on the Terminal.
PuTTY - Raspberry Pi
Raw Value: 48520 Raw Value: 47235 Raw Value: 46032 Raw Value: 44047 Raw Value: 43195 Raw Value: 42074 Raw Value: 41084 Raw Value: 41072 Raw Value: 40808 Raw Value: 40634 Raw Value: 40800 Raw Value: 40512

※ NOTE THAT:

  • Do NOT use the pure water for testing since it doesn't conduct electricity, which means it won't impact the sensor readings.
  • The sensor readings typically don't drop to zero. It's normal for them to stay within the range of 40000 to 50000, but this might change based on factors such as how deep the sensor is placed, the type of soil or water, and the voltage of the power supply.
  • Never bury the circuit part (found on top of the sensor) in soil or water, as this could harm the sensor.

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Calibration for Capacitive Soil Moisture Sensor

The value obtained from the moisture sensor is not absolute. It varies depending on the soil's composition and water content. Therefore, it is necessary to carry out calibration in order to determine a boundary between wet and dry conditions.

Instructions for Calibration for Capacitive Soil Moisture Sensor:

  • Run the code on Raspberry Pi
  • Place the moisture sensor into the soil
  • Gradually add water to the soil
  • Monitor the values on the Terminal
  • Note the value when you feel the soil changes from dry to wet, for example: 45000. This is referred to as the THRESHOLD.

Determine if the soil is wet or dry

  • Create a Python script file soil_moisture_dry_wet.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-soil-moisture-sensor """ import time import Adafruit_ADS1x15 # Create an ADS1115 ADC object adc = Adafruit_ADS1x15.ADS1115() # Set the gain to ±4.096V (adjust if needed) GAIN = 1 # Single threshold for wet/dry classification (adjust as needed) THRESHOLD = 45000 # Function to determine the wet-dry level based on the soil moisture percentage def wet_dry_level(soil_moisture): if soil_moisture > THRESHOLD: return "DRY" else: return "WET" # Main loop to read the analog value from the soil moisture sensor try: while True: # Read the raw analog value from channel A3 raw_value = adc.read_adc(3, gain=GAIN) # Determine the wet-dry level based on the raw ADC value level = wet_dry_level(raw_value) # Print the results print("Raw Value: {} \t Wet-Dry Level: {}".format(raw_value, level)) # Add a delay between readings (adjust as needed) time.sleep(1) except KeyboardInterrupt: print("\nExiting the program.")
  • Update the THRESHOLD value that was noted in the calibration to the code.
  • Save the file and run the Python script by executing the following command in the Terminal:
python3 soil_moisture_dry_wet.py

The output displayed on the Terminal.

PuTTY - Raspberry Pi
Raw Value: 48520 Wet-Dry Level: DRY Raw Value: 47235 Wet-Dry Level: DRY Raw Value: 46032 Wet-Dry Level: DRY Raw Value: 44047 Wet-Dry Level: WET Raw Value: 41072 Wet-Dry Level: WET Raw Value: 40634 Wet-Dry Level: WET Raw Value: 40512 Wet-Dry Level: WET

Video Tutorial

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