Introduction: Measuring Soil Moisture With Raspberry Pi 4

Do you know how often to water plants? Or outpoured plants and lost them. To solve this I thought it would be more circumstantial if we can get the value of water content inside the soil in order to make a decision for watering the plants appropriately.
In this project lets try to build a circuit which can measure the water content value of the soil eventually control the flow using Raspberry Pi.


  1. Raspberry Pi 2/3/4
  2. Soil moisture sensor
  3. MCP3008 IC
  4. Jumpers

Step 1: Circuit Connection

  • MCP3008 GND to GND
  • MCP3008 CS to RPI 8
  • SoilMoisture GND to GND
  • SoilMoisture VCC to +3V
  • SoilMoisture A0 to MCP3008 CH0
  • MCP3008 VCC to +3V
  • MCP3008 VREF to +3V
  • MCP3008 AGND to GND
  • MCP3008 CLK to RPI 11
  • MCP3008 DOUT to RPI 9
  • MCP3008 DIN to RPI 10

Make all the connections and power up the Raspberry Pi. If you want to learn how to set up a Raspberry Pi check out how to set up Raspberry Pi 4.

Step 2: Essential Packages

Before you run the code you have to install few libraries, if you already have the `Adafruit_Python_MCP3008` installed then move on to the next step or follow the below commands to install them.

pi@raspberrypi: sudo apt-get update
pi@raspberrypi: sudo apt-get install build-essential python-dev python-smbus git 
pi@raspberrypi: cd ~ 
pi@raspberrypi: git clone <a href="" rel="nofollow">>
pi@raspberrypi: cd Adafruit_Python_MCP3008 
pi@raspberrypi: sudo python install

If you have a problem cloning the repository you can manually download the repository and continue the steps later. If you see an error go back and carefully check all the previous commands and run again.

You should see the library install succeed and finish with a message.

If you prefer installing using pip(This is not required if you have followed the above steps for installation), open the terminal on the Raspberry Pi and execute the following commands:

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus python-pipsudo 
pip install adafruit-mcp3008

Step 3: The Code

pi@raspberrypi: nano

Once the library has been installed it's time to execute the code. Open terminal make a new file by typing "nano" and enter the below code.

import RPi.GPIO as GPIO<br>from time import sleep
import Adafruit_MCP3008

am = Adafruit_MCP3008.MCP3008(clk = 11, cs = 8, miso = 9, mosi = 10)

while True:
  moisture_value = am.read_adc(0) # Get the analog reading from the soil moist sensor
  per = moisture_value * 100 / 1023  # Converting the moisture value to percentage
  print("Recorded moisture value is %s percentage" % per)
  if moisture_value >= 930:
    print(" No water, Can you plaease water me")
  elif moisture_value < 930 and moisture_value >= 350:
    print(" I'm sufficient ")
  elif moisture_value < 350 :
    print(" Stop drowning me!")

Click "ctrl+o" to save the file and "ctrl+x" to exit.

pi@raspberrypi: python

Command "python" to run the code. You should be able to see the values from soil moisture sensor on the terminal window, place the soil moisture sensor inside the water and in the dry soil to understand the difference .

Step 4: Video Tutorial

Hurray! the circuit is done. If you have any questions don't hesitate to comment below.

Happy Circuiting!