Soil Moisture

A soil moisture sensor module is designed to measure the amount of water in the soil by reading its resistance. It passes an electric current through the two prongs and measures the resistance of the soil.

Components

Which Sensor?

Soil Moisture Sensors

A search on eBay or Amazon for soil moisture sensor arduino will yield many different types of sensors. The one used in this example should have three components on the prongs itself. There are three variations of the same type above.

Which Connectors?

Female Heade Pins

The Soil Moisture Sensor has three header pins. These are labelled VCC, Output and GND. To connect wires to the sensor you will need female header pin connectors. Pictured right are examples of hookup wires with female header pin connectors.

Notes of Caution

Supplying constant power to the sensor drastically reduces its lifespan. The metal on the prongs slowly degrade dues to electrolysis. It’s much better to only power the sensor when you want to take a reading. In the example on this page, the sensor is turned on just before a reading is taken and then turned off.

Electronics

Hook up the sensor to your microbit:

Moisture Sensor PinMicrobit Pin
VCCPIN0
GNDGND
SIG or OUTPIN1

Code

Establish a Threshold

We need to establish what the sensor’s reading is when the plant needs to be watered (the soil is dry) and when the plant needs to be watered (the soil is wet).

Open REPL
  1. Open mu
  2. Flash a blank program (this can just read `from microbit import *)
  3. Click ‘REPL’ button
Reading for Dry Soil

We need to know what sensor reading should trigger the need to water it. Attach VCC to 3V pin and SIGNAL to pin0. Put the sensor in dry soil and type in pin0.read_analog() in REPL. The return will look something similar to this:

MicroPython v1.7-9-gbe020eb on 2016-04-18; micro:bit with nRF51822
Type "help()" for more information.
>>> pin0.read_analog()
80

80 is the reading for dry soil on our sensor.

Reading for Moist Soil

We also need to establish a ‘good’ reading for adequately watered soil. Repeat the above but with soil that has been watered. With my sensor, a well watered plant gave a reading of 211.

Using the Threshold

With the data we obtained we can now say:

With these reading we can conclude a soil sensor reading of less than 180 requires the soil to be watered. Using these reading we can now write our code.

Final Code

Show a sad face if the sensor reading is less than 180 (dry).

from microbit import *

# sensor reading at which the soil needs watering
threshold = 180

while True:
    pin0.write_digital(1) # turn on sensor
    sleep(2000) # wait for sensor to settle

    if pin1.read_analog() < threshold: # needs watering
        display.show(Image.SAD, clear=False)
    else:
        display.show(Image.HAPPY, clear=False)

    pin0.write_digital(0) # turn off sensor
    sleep(300000) # sleep for 5 minutes

Experiment

Notes of Caution

Share On
Facebook Twitter Google Plus

Published: December 23, 2016 Updated: December 23, 2016

Acknowledgements: Teaser image by Adafruit (CC-BY). Sensor image by spark fun (CC-BY). Dupont connector by Adafruit (CC-BY).

Soil Moisture

How to use a moisture sensor to read the amount of water within a potted plant. Useful to make an alert when the plant needs watering.

Jez Dean
Teacher