You can use the micro:bit Python Editor to write MicroPython code for the Soil Moisture Sensor. The microbit library lets you easily access all of the pins and features on the micro:bit.
- micro:bit 3V to sensor 3V (red wire)
- micro:bit GND to sensor GND (black wire)
- micro:bit 0 to sensor OUT (white wire)
Code
The MicroPython code reads the analog moisture value for 5 seconds on pin 0. If the moisture level is greater than 200, the LED matrix shows a happy face. Otherwise, it shows a sad face, letting you know that the soil is dry.
# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# pylint: disable=wildcard-import, undefined-variable
"""MicroPython Simple Soil Sensing Demo for micro:bit"""
from microbit import *
while True:
moisture = pin0.read_analog()
if moisture > 200:
display.show(Image.HAPPY)
else:
display.show(Image.SAD)
sleep(5000)
When you're ready to send the code to the micro:bit, you'll click the Send to micro:bit button at the bottom of the window.
Connect your micro:bit to your computer with a known good data micro-B USB cable. Once you're connected, click Next.
Advanced Sensing
One disadvantage to the soil sensor is that the soil probes can oxidize over time. There is a "hack" you can do to prolong the life of the probes. You can connect the sensor 3V pad to a digital pad on the micro:bit and turn it "on" only when you're getting an analog reading and then turn it "off" when not measuring.
- micro:bit 2 to sensor 3V (red wire)
- micro:bit GND to sensor GND (black wire)
- micro:bit 0 to sensor OUT (white wire)
Advanced Sensing Code
The MicroPython code turns the sensor on by toggling pad 2 high. Then, it reads the analog moisture value on pad 0. If the moisture level is greater than 200, the LED matrix shows a happy face. Otherwise, it shows a sad face, letting you know that the soil is dry. Pad 2 is set low to turn off the soil sensor and help keep the soil probes from oxidizing.
# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# pylint: disable=wildcard-import, undefined-variable
"""MicroPython Advanced Soil Sensing Demo for micro:bit"""
from microbit import *
while True:
pin2.write_digital(1)
sleep(100)
moisture = pin0.read_analog()
if moisture > 200:
display.show(Image.HAPPY)
else:
display.show(Image.SAD)
pin2.write_digital(0)
sleep(5000)
Page last edited July 14, 2025
Text editor powered by tinymce.