Posts: 7
Joined: Tue Dec 18, 2012 3:22 am

Water soil moisture sensor

Mon Sep 16, 2013 10:27 am

I am trying to make a automated watering systems for my plants, so when I am not at home, they get their water automatically.

My main concern is obtaining a reading from the moisture.

I have searched online and spent hours trying to find documentation which may be able to help me, but I have had no success.

I have purchased a moisture sensor from ebay, this sensor has digital(DO) and analogue(AO) output. I am using the DO only. ... 1555.l2649

My intention is to connect the sensor to the Raspberry Pi. The sensor comes with GND, VCC, DO and AO outputs. I will use the GND, VCC and the DO (Digital Output)

Can someone show me how I can take the readings from the sensor. I want an example in Python because I intend to do the whole project in this language.

So far I followed the following example,

I am not using the section on sending emails, I just want the sensor to give me a reading, i.e 0 or 1.

Depending on what the value is, I then want the RPi to operate a pump.


Posts: 20
Joined: Thu Oct 24, 2013 9:43 pm

Re: Water soil moisture sensor

Tue Nov 19, 2013 9:57 pm

Did you get anywhere with this?

I bought a couple of these sensors and they come with no documentation. I've been having a play.

The circuit board comes with 6 pins. Two at the bottom connect to the sensor and the 4 at the top are for the data. Reading left to right these are:
Analogue out : Digital Out : GND : Vcc

You have two ways of triggering an event, use the digital out and set the threshold with the potentiometer or use the analogue out via something like a MCP3008 and set the threshold in software.

I've gone down the analogue route, connecting the data side of the MCP3008 as per the guide: ... an-mcp3008

On the sensor board, GND and Vcc connect to the corresponding GPIO pins (6 and 1). A0 just goes directly to one of the channels on the MCP3008. I have connected to channels 2 and 3. The following python code outputs a moisture reading:

Code: Select all


# Monitor two soil sensors on MCP3008, ch 2 and 3 
# (pin 3 and 4)

import spidev
import time
import os

# Open SPI bus
spi = spidev.SpiDev(),0)

# Function to read SPI data from MCP3008 chip
def ReadChannel(channel):
	adc = spi.xfer2([1,(8+channel)<<4,0])
	data = ((adc[1]&3) << 8) + adc[2]
	return data

# Main loop - read raw data and display
while True:
	soilOne = ReadChannel(2)
	soilTwo = ReadChannel(3)
	# Output
	print "Soil1=",soilOne," : Soil2=",soilTwo
A value of 1023 is very dry soil, or the detector not in any soil. Dipping in a cup of water give 374.

Turning the potentiometer does not affect analogue reading, but can make red light come on.

D0 is digital output with the threshold set by the pot. When the red light is off, the soil is dry and there is a voltage on D1. When the soil is wet the red light is on and there is no voltage. Although I have not tried it, you should just be able to connect this up to a GPIO pin (with resistor) in the same way you would a push button.

I want to make a remote watering solution for when I am not around. I may want to tweak the thresholds and noticed putting the detectors in the same pot give quite different readings. (It was very dry compost only just watered, so it is likely that moisture is not uniform.) Using analogue allows you to change the threshold remotely in software, thought there is the overhead of using the MCP3008. I plan to graph the output for a few days to get a feel for a reasonable threshold.

One thing that might make this difficult is that I want the Pi in my shed but the sensor out in the garden. Will sticking a long cable on it give too much resistance and make it useless? Is there any way around this?

Posts: 1
Joined: Thu Aug 24, 2017 3:58 pm

Re: Water soil moisture sensor

Thu Aug 24, 2017 4:02 pm

Code: Select all

import RPi.GPIO as GPIO

while True:
		print("watery soil")
		print("Dry Soil")

Posts: 2
Joined: Thu Aug 24, 2017 3:12 pm

Re: Water soil moisture sensor

Sat Aug 26, 2017 3:27 pm

I got some experience about soil moisture sensors since I'm playing with it from some time, I have created a system based on ADC (analog digital converter) attached to Raspberry and a DIY probe.
If interested, here the details: ... asurement/


Return to “Automation, sensing and robotics”