Skip to main content

Case 16: Smart Soil Moisture Device


Introduction

Use the Jacdac soil moisture sensor to detect soil water content in real time. The Jacdac LED ring color and brightness visually indicate the soil moisture status: Red high brightness (100%) = soil is dry and urgently needs watering (moisture < 20); Blue medium brightness (80%) = soil is slightly dry (moisture 20–50); Green low brightness (60%) = soil is moist and suitable (moisture 50–70); LED off = soil is overly wet (moisture > 70). A simple three-color indicator so watering no longer relies on guesswork.


Learning Objectives

  1. Understand multi-threshold graded judgment (if-elif-elif-else) — executing different actions based on which range a value falls into.
  2. Learn LED color coding — using the three colors red/blue/green to intuitively convey "dry/medium/wet" information.
  3. Explore the water-saving significance of precision irrigation in modern agriculture.

Required Materials

ItemImageQuantity
micro:bit V21
Jacdac Expansion Board1
Jacdac 25cm Connector Cable2
Jacdac Soil Moisture Sensor1
Jacdac LED Ring1
USB Cable1

Sensor Principle

Soil Moisture Sensor

The Jacdac soil moisture sensor estimates water content by measuring the dielectric constant of the soil. Water molecules are polar; the higher the water content, the greater the soil's dielectric constant. The sensor converts this physical quantity into an analog numerical output.

Moisture RangeSoil ConditionLED IndicationRecommendation
0–20%DryRed LED, brightness 100%⚠️ Water urgently needed!
20–50%Slightly dryBlue LED, brightness 80%💧 Can water moderately
50–70%💚 MoistGreen LED, brightness 60%✅ Moisture suitable; no watering needed
70+%💧 Overly wetLED off🛑 Stop watering; prevent root rot

The suitable moisture range for most plant root systems is between 50%–70%. Below 20%, water stress begins; above 80%, roots lack oxygen and may rot. Precisely controlling soil moisture is the most critical aspect of agricultural irrigation.


Assembly Steps


Model Principle

Soil Moisture Sensor — The Plant's "Thirst Meter"

In traditional farming, people insert a finger into the soil to judge wetness — it's crude and imprecise. The soil moisture sensor acts as the plant's "electronic thirst meter":

Traditional MethodSensor Method
Finger test; subjective judgmentPrecise numerical value; objective quantification
Can only check a few times a day24/7 continuous monitoring
Can only judge the surface layerCan measure moisture at root depth
Cannot record historyConnectable to IoT for long-term tracking

This case converts sensor readings into an intuitive three-color LED display — red stop (water shortage), green go (suitable), blue alert (slightly dry, needs attention) — as simple and easy to understand as a traffic light.


Connection Diagram

As shown below, insert the micro:bit V2 board into the Jacdac expansion board, and use cables to connect the soil moisture sensor and LED ring to the Jacdac expansion board's edge connector.

Connection Diagram


Programming Software

Microsoft MakeCode


MakeCode Programming

Step 1: Adding the Jacdac Extension

  1. Go to Microsoft MakeCode and click "New Project".

    New Project

  2. Enter a project name in the pop-up window and click "Create".

    Create Project

  3. Click "Extensions" in the code drawer.

    Extensions

  4. In the pop-up interface, type "Jacdac" and click the search icon, then select the Jacdac software library as shown.

    Search Jacdac


Step 2: Connecting the Hardware

  1. Use a USB cable to connect the micro:bit V2 board to the computer.

    Note: If this is the first time running a Jacdac program on the micro:bit V2, please pre-load a blank Jacdac program onto the board first. Otherwise, skip this step.

    Connect micro:bit

  2. Connect the sensor. MakeCode will automatically detect and simulate it in the simulator area, where sensor status is displayed in real time.

    Sensor Simulation

  3. Click "ADD BLOCKS" to add the sensor extension module.

    Note: When connecting a new sensor, repeat the "click 'ADD BLOCKS'" workflow once.

    ADD BLOCKS


Write the Program as Shown

Program Screenshot


https://makecode.microbit.org/_0JXiAC5rCeYd

You can also download the program directly from the webpage below. Once downloaded, you can start running the program.


Result

Insert the soil moisture sensor probe into dry soil, and the LED ring lights up red (brightness 100%) — "Water shortage! Watering needed." Slowly add water to the soil; as the moisture value gradually rises, the LED changes from red to blue (brightness 80%) — "Soil absorbing water." Continue adding water to reach a suitable moisture level, and the LED changes to green (brightness 60%) — "Moisture just right." If too much water is added, the LED turns off — "Too wet, stop watering!" The entire process requires no reading of any numbers — the soil condition can be judged by the LED color alone.


Think

  1. Besides LED ring color, what other methods could be used to indicate soil moisture? (Hint: sound, micro:bit 5×5 LED matrix displaying emoji expressions)
  2. If you wanted to turn this device into a true "automatic watering system," what additional actuator would you need?

Program Logic

Trigger ConditionActionSource
Soil moisture < 20 (dry)LED ring lights red at 100% brightness — urgent watering warningJacdac Soil Moisture Sensor
Soil moisture 20–50 (slightly dry)LED ring lights blue at 80% brightness — slightly dry; can water moderatelyJacdac Soil Moisture Sensor
Soil moisture 50–70 (suitable)LED ring lights green at 60% brightness — moisture suitableJacdac Soil Moisture Sensor
Soil moisture > 70 (overly wet)LED ring turns off — over-wet warning; stop wateringJacdac Soil Moisture Sensor

Knowledge Extension: From "Finger Test" to "Electronic Sensing" — The Precision Irrigation Revolution

I. Global Water Crisis and Agricultural Water Use

Agriculture is the world's largest consumer of freshwater, accounting for approximately 70% of human freshwater withdrawals. However, about 50%–60% of water used in traditional irrigation methods is wasted through evaporation, seepage, and runoff — never actually reaching plant roots.

Irrigation MethodWater Use EfficiencySuitable Scenarios
Flood irrigation30%–40%Traditional agriculture
Sprinkler irrigation60%–70%Large-scale farmland
Drip irrigation90%–95%Greenhouses, orchards, high-value crops
Sensor-based precision irrigation95%+Smart greenhouses, vertical farms

Global agricultural water accounts for approximately 70% of freshwater withdrawals. Promoting sensor-based precision irrigation could save hundreds of billions of cubic meters of water annually.

II. if-elif-else — The Programming Paradigm of Multi-Level Classification

The four-level moisture judgment in this case (if-elif-elif-else) is the programming embodiment of multi-level classification thinking:

if moisture < 20: Red LED → Dry
elif moisture < 50: Blue LED → Slightly dry
elif moisture < 70: Green LED → Suitable
else: LED off → Overly wet

This structure has countless applications in daily life:

Application ScenarioClassification BasisMulti-Level Output
Grade levelsScore90+ Excellent / 80+ Good / 60+ Pass / <60 Fail
Weather warningsTemperatureRed / Orange / Yellow heat alerts
Air qualityAQI indexGood / Moderate / Unhealthy for Sensitive / Unhealthy / Very Unhealthy / Hazardous
Phone batteryPercentage100% green / 20% yellow / 10% red flashing

III. Soil Moisture — More Than Just Watering

Plant roots require a delicate balance of air and moisture. Soil that is too dry prevents roots from absorbing water and nutrients; soil that is too wet deprives roots of oxygen, leading to root rot. Different plants have entirely different soil moisture requirements:

Plant TypeSuitable Moisture RangeWatering Frequency
Cactus / Succulents20%–40%Once a week or less
Tomato / Pepper50%–70%Every 1–2 days
Rice80%–100% (flooded)Continuous flooding
Orchids40%–60% (well-aerated)Every 3–5 days

Reflection: The numerical value returned by the soil moisture sensor (0–100 or 0–255) is not the true soil water content percentage, but a relative value output by the sensor. In real applications, calibration using standard methods such as the "oven-drying gravimetric method" is required to convert sensor readings into accurate soil water content percentages. Calibration is an indispensable key step in all sensor engineering.