Let’s get ready to code your Automatic Irrigation project. To follow the coding tutorial on Makecode, click the Start Coding button below.

Makecode workspace for the automated watering tutorial with a "start coding" button.

Objective

Use the moisture sensor to measure the moisture level of the plant’s soil. Then, use the water pump to water the plant when the moisture level is low. Use the LED light to act as a grow light for the plant.

Coding Description

Use an if statement to determine the moisture level of the soil, and if the moisture level is below a certain amount, run the pump motor for a certain amount of time. Change the LED to your desired colour and keep it on forever.

The code runs in a forever loop, repeatedly executing the contained instructions. Within this loop, it sets the colour of all pixels in an LED ring to a purplish hue. It then checks whether the moisture level read from a moisture sensor is above a threshold of 50. A happy face icon will be displayed on the micro:bit’s LED matrix if the moisture level exceeds the threshold.

If the moisture level is not above the threshold, it shows a sad face icon, activates a water pump motor for 500 milliseconds, pauses for 500 milliseconds, and clears the LED matrix display. This code monitors soil moisture, provides visual feedback through icons and the LED ring, and briefly activates the water pump if moisture is insufficient.

If you’d like to try coding your Automated Watering System without following a tutorial, you can code openly in the traditional Makecode workspace by clicking the link below.