Soil Moisture Sensor

Basics: Sensors & Buttons

Project Setup

What You Need And How To Connect It All

Reading soil moisture values with a simple moisture sensor.

A Micro:Bit and soil moisture sensor connected with three wires (female connector to crocodile clip).  pin0 goes to the middle connector marked SIG; the 3V (power) pin goes to the connector marked VCC; and the GND pin goes to the connector marked GND.

Project Details

The What, Why And How Of It

Connect the Micro:Bit to the soil moisture sensor with three wires.

All three of the connectors on the sensor are used...

pin0 goes to the middle connector marked SIG
the 3V (power) pin goes to the connector marked VCC
the GND pin goes to the connector marked GND

The following MicroPython code shows three ways to display the readings taken with this setup, from simply displaying the value to showing a message or an image based on that value...

Project Code

The Code That Makes It Happen (Hopefully)


###################################
#
#  MB Plant Moisture Sensor
#   Give basic reading on
#   button press.
#
###################################

# import MB module
from microbit import *

# main loop         
while True:

    # whichever button pressed
    if (button_a.is_pressed()) or (button_b.is_pressed()):
        # read the moisture value
        reading = pin0.read_analog()
        # and display it
        display.scroll(reading)

# END main loop
          

###################################
#
#  MB Plant Moisture Sensor
#   Give basic reading on
#   button press A but show
#   DRY/OK on button B.
#
###################################

# import MB module
from microbit import *

# minimum moisture value
min_moisture = 650

# main loop
while True:

    if button_a.is_pressed():
        reading = pin0.read_analog()
        display.scroll(reading)
    # END button a if

    if button_b.is_pressed():
        reading = pin0.read_analog()
        # scroll text based on moisture value
        if reading < min_moisture:
            display.scroll("DRY")
        else:
            display.scroll("OK")
    # END button b if

# END main loop
          

###################################
#
#  MB Plant Moisture Sensor
#   Give basic reading on
#   button press A but show
#   an image on button B.
#
###################################

# import MB module
from microbit import *

# minimum moisture value
min_moisture = 650

# main loop
while True:

    if button_a.is_pressed():
        reading = pin0.read_analog()
        display.scroll(reading)
    # END button a if

    if button_b.is_pressed():
        reading = pin0.read_analog()
        # show images based on moisture value
        if reading < min_moisture:  
            display.show(Image.SAD)
            sleep(2000)
            display.clear
        else:
            display.show(Image.HAPPY)
            sleep(2000)
            display.clear
    # END button b if

# END main loop