Capacitive Soil Moisture Sensor Raspberry Pi

 Model Picture

1. Introduction

The Capacitive Soil Moisture Sensor measures the moisture content in soil using capacitive sensing, which is more durable than resistive sensors. Since it gives an analog output, we use the MCP3008 to interface it with the Raspberry Pi.


2. Components

  • Raspberry Pi 4 Model B
  • MCP3008
  • Capacitive Soil Moisture Sensor
  • Breadboard
  • Jumper wires

3. Circuit and Connections

Raspberry Pi Pinout Diagram
Raspberry Pi Pinout Diagram

Soil Moisture Sensor Pinout Diagram
Soil Moisture Sensor Pinout Diagram

MCP3008 Wiring Diagram
MCP3008 Wiring Diagram

MCP3008 ADC Pinout Diagram
MCP3008 ADC Pinout Diagram

Sensor to MCP3008

Sensor PinConnection
VCC3.3V
GNDGND
AOUTMCP3008 CH0

MCP3008 to Raspberry Pi (SPI Connection)

MCP3008 PinFunction    Raspberry Pi Pin
16VDD            3.3V
15VREF            3.3V
14AGND            GND
13CLK    GPIO11 (SCLK)
12DOUT    GPIO9 (MISO)
11DIN    GPIO10 (MOSI)
10CS/SHDN    GPIO8 (CE0)
9DGND            GND


4. Detailed Step By Step Circuit Working

  1. Sensor measures soil moisture based on capacitance
  2. Output voltage changes with moisture level
  3. MCP3008 converts analog voltage to digital value
  4. Raspberry Pi reads digital data via SPI
  5. Higher value → Dry soil, Lower value → Wet soil

5. Libraries to be Included

pip3 install spidev

6. Code (Python)

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1350000

def read_channel(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data

try:
while True:
moisture = read_channel(0)

print("Soil Moisture Value:", moisture)

if moisture > 700:
print("Dry Soil")
elif moisture > 400:
print("Moderate Moisture")
else:
print("Wet Soil")

print("---------------------")
time.sleep(1)

except KeyboardInterrupt:
spi.close()

7. Detailed Step By Step Code Working

  • Reads analog value via MCP3008
  • Converts to digital (0–1023)
  • Compares values to determine moisture level
  • Prints status (Dry / Wet)
  • Loops continuously

8. Tips

  • Do not power with 5V (use 3.3V)
  • Avoid immersing full sensor in water
  • Calibrate values for your soil type
  • Use corrosion-resistant sensor (capacitive type)
  • Keep wiring short

9. Uses

  • Smart irrigation systems
  • Agriculture automation
  • Plant monitoring
  • Greenhouse control
  • IoT farming projects

10. Conclusion

The capacitive soil moisture sensor is reliable and long-lasting. With MCP3008, Raspberry Pi can easily monitor soil conditions for smart agriculture systems.

തുടക്കക്കാർക്കായുള്ള ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.