Measuring Soil Moisture using Raspberry Pi
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.
Hardware:
- Raspberry Pi 2/3/4
- Soil moisture sensor
- MCP3008
- Jumpers
Circuit Connection:
- MCP3008 GND to GND
- MCP3008 CS to RPI 8
- MCP3008 DIN to RPI 10
- MCP3008 DOUT to RPI 9
- MCP3008 CLK to RPI 11
- MCP3008 AGND to GND
- MCP3008 VREF to +3V
- MCP3008 VCC to +3V
- SoilMoisture A0 to MCP3008 CH0
- SoilMoisture VCC to +3V
- SoilMoisture GND to GND
Make all the connections and power up the Raspberry Pi. If you want to learn how to connect a Raspberry Pi check out my previous post on how to Set Up Raspberry Pi 4 Through Laptop/pc Using Ethernet Cable(No Monitor, No Wi-Fi)1.
Essential Packages
Before you run the code you have to install few libraries, move on with the following steps.
sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus git
cd ~
git clone https://github.com/adafruit/Adafruit_Python_MCP3008.git
cd Adafruit_Python_MCP3008
[sudo](sudo) python setup.py install
The Code
Once the library has been installed it’s time to execute the code. You can use the below code or download it from here2.
import RPi.GPIO as GPIO
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)
per = moisture_value * 100 / 1023
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!")
sleep(1.5)
Youtube Video Tutorial:
youtube ahQhEWf1PRI
You can rewrite the code and change the parameters for your requirements. If you have suggestions or any trouble with the project, feel free to comment below. Happy Circuiting!