Microbit Robotics Sensors & Circuits
Advanced
45 mins
95 points
What you need:
  • Chromebook/Laptop/PC
  • Microbit
  • Moisture sensor

Water plants project

In this project we connect the Soil Moisture Kit to our Microbits and create a program that monitors the soil moisture of a potted plant. Our Microbits will tell us when we need to water the plant!

1 - Create a new Microbit project.

The Kitronik Soil Moisture Kit is a sensor board that can be once you connect it to your Microbit, it can monitor the moisture present in the soil. The two conductive tines are placed into the soil. Any water or moisture in the soil will conduct to give an analogue voltage that can be read by the Microbit.

After you connected the Soil Moisture Kit to your Microbit, create a new Microbit project.


2 - Create a variable called 'moisture'

We will need a variable to store the current level of moisture that is in the soil.

Create a variable called 'moisture' and then add the following code to get the moisture reading from the Soil Moisture Kit and store it in the variable.

let moisture = 0
basic.forever(function () {
    moisture = pins.analogReadPin(AnalogPin.P1)
})


3 - Check the moisture

If the moisture is less than 400 then the soil is too dry and needs to be watered. Otherwise, if it is greater than 400, there is enough moisture in the soil for the plant to it does not need to be watered yet!

Add the following code to check the moisture level and show a happy or sad face.

let moisture = 0
basic.forever(function () {
    moisture = pins.analogReadPin(AnalogPin.P1)
    if (moisture <= 400) {
        basic.showIcon(IconNames.Sad)
    } else {
        basic.showIcon(IconNames.Happy)
    }
})

4 - Put in a pause

To ensure that the moisture sensor has a long and fulfilling life, it is better to write your code to perform a moisture check every so often rather than continuously. When the check is performed continuously it promotes rapid erosion of the electrodes on the prongs.

Add a pause (ms) 5000  block after the if then block so that you only check the moisture reading every 5 seconds

let moisture = 0
basic.forever(function () {
    moisture = pins.analogReadPin(AnalogPin.P1)
    if (moisture <= 400) {
        basic.showIcon(IconNames.Sad)
    } else {
        basic.showIcon(IconNames.Happy)
    }
    basic.pause(5000)
})

5 - Try it out with a plant!

That's all the code so you can put it onto your Microbit now and try it out in with a plant in your house or school!


Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more