DEV Community

Hedy
Hedy

Posted on

How to connect soil moisture sensor to raspberry pi 5?

Connecting a soil moisture sensor to a Raspberry Pi 5 is a common project for smart gardens and plant monitoring.

Crucial Warning First: The Raspberry Pi's GPIO pins are 3.3V and are not 5V tolerant. Connecting a 5V signal directly to a GPIO pin will permanently damage your Raspberry Pi.

The method you use depends entirely on the type of sensor you have. The most common types are resistive and capacitive.

Method 1: For Capacitive Soil Moisture Sensors (Recommended & Safer)
Capacitive sensors (like the popular Capacitive Soil Moisture Sensor v1.2/v2.0) are the best choice for the Raspberry Pi. They are more reliable and, most importantly, they can be easily used with the Pi's 3.3V power.

Why this is the best method:

  • The sensor runs on 3.3V, perfectly matching the Pi's GPIO voltage.
  • It provides an analog output, but we use an ADC (Analog-to-Digital Converter) to read it with the Pi's digital pins.
  • No risk of damaging the Pi from voltage mismatch.

What You'll Need:

  1. Raspberry Pi 5 (any model with GPIO pins)
  2. Capacitive Soil Moisture Sensor (e.g., FC-28, XH-M360, etc. with "capacitive" in the name)
  3. Analog-to-Digital Converter (ADC) - The most common is the MCP3008
  4. Breadboard and jumper wires (Male-to-Female recommended)

Wiring Diagram (Using MCP3008 ADC)

Step-by-Step Guide:

  1. Enable SPI Interface: The MCP3008 uses the SPI protocol.
  • Run sudo raspi-config.
  • Go to Interface Options -> SPI -> Select Yes to enable it.
  • Reboot your Pi.
  1. Wire Everything Up: Carefully follow the wiring table above. Double-check that 3.3V is used for both the MCP3008 and the sensor, NOT 5V.

  2. Install Software Libraries: Open a terminal and install the necessary Python library for the MCP3008.

bash

sudo apt update
sudo apt install python3-pip
pip3 install adafruit-circuitpython-mcp3xxx
Enter fullscreen mode Exit fullscreen mode
  1. Python Code to Read the Sensor: Create a file, e.g., moisture.py, and paste the following code:
python

import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn

# Create the SPI bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# Create the chip select (CS) signal
cs = digitalio.DigitalInOut(board.D8)  # Use GPIO 8 / Pin 24

# Create the MCP3008 object
mcp = MCP.MCP3008(spi, cs)

# Create an analog input channel on channel 0 (CH0)
channel = AnalogIn(mcp, MCP.P0)

# Read and print the raw ADC value and voltage
print("Raw ADC Value: ", channel.value)
print("ADC Voltage: " + str(channel.voltage) + "V")

# To get a percentage, you need to calibrate!
# Define your own min and max values from testing
min_moisture = 25000   # Value in completely dry air
max_moisture = 12500   # Value in a cup of water

# Map the value to a percentage (invert because dry value is higher)
moisture_percentage = ((max_moisture - channel.value) * 100 / (max_moisture - min_moisture))
moisture_percentage = max(0, min(100, moisture_percentage))  # Constrain to 0-100%

print(f"Moisture: {moisture_percentage:.1f}%")
Enter fullscreen mode Exit fullscreen mode
  1. Calibrate Your Sensor:
  • Run the script (python3 moisture.py) with the sensor in dry air.
  • Note the Raw ADC Value. This is your min_moisture value.
  • Run the script again with the sensor in a glass of water (don't submerge the electronics!).
  • Note the new Raw ADC Value. This is your max_moisture value.
  • Update the min_moisture and max_moisture variables in the code with your measured values.

Method 2: For Resistive Sensors (Not Recommended)
Strong Caution: The classic resistive soil moisture sensors (with exposed metal prongs) are not ideal for the Pi. They are prone to corrosion and, more importantly, often run at 5V. Their analog output can also be 5V, which will damage your Pi.

If you must use one, you MUST use a voltage divider circuit to step the 5V analog signal down to 3.3V before it reaches the ADC. This involves using two resistors (e.g., 1kΩ and 2kΩ) to create the divider. This adds complexity and is not beginner-friendly.

It is highly recommended to spend a few extra dollars on a capacitive sensor to avoid the risk of damaging your Pi and to get more accurate, long-lasting readings.

Summary

For your Raspberry Pi 5, Method 1 with a capacitive sensor and an MCP3008 ADC is the correct, safe, and reliable approach.

Top comments (0)