dark mode light mode Search Menu

Thirsty Plants Love micro:bit!

Les Pounder

What will you need for this project?

  • Any computer with an Internet connection
  • A micro:bit
  • A speaker with 3.5mm headphone jack
  • 5 x Crocodile clips
  • 3 x Female to Male Jumper Wires (Du Pont connectors)
  • A Soil Moisture Sensor
  • A USB power bank or micro:bit AAA battery pack.
  • A plant

All of the code for this project as well as circuit diagrams can be found at

You can also try out the code in the editor.


Continuing from our previous issue, we continue our look at the micro:bit. In this issue we shall create a plant protector. A device that will measure the moisture of our soil and alert us when it drops below a certain level. To do this we shall use a sensor to constantly monitor the conductivity of the soil. Wet soil is a good conductor of electricity and will mean that our plant is happy. Dry soil is a poor conductor and our plant protector invention will alert us to this.

Remember that water and electricity don’t mix. NEVER submerge your micro:bit, sensor of battery pack in water as this will lead to damage. Also do not water the plant while the plant protector is in the soil.

By completing this project you will learn more about


  • To repeat portions of code


  • Measuring conductivity of soil based on its moisture.


  • Using the screen to visually indicate the state of the soil.
  • Using a “heartbeat” to alert the user that the plant requires water.


  • Learn how water affects the conductivity of soil.

Building the Circuit

Our Soil Moisture Sensor can be found on various online auction sites and via sellers such as Adafruit. The sensor receives 3V and GND from the micro:bit, effectively providing the sensor with power. This is then passed from one tip of the sensor, through the soil, to a receiving tip, which then sends the voltage as an output to our micro:bit. This is then read on Pin 1 and converted to a value of 0 to 1023, where 0 means that the plant has no water, and 1023 the plant is saturated with water. Please refer to the circuit diagram and the photo which shows how the wires are connected.

Our headphones require two croc clips. Our black croc clip connects to the base of the headphone jack, and then it connects to GND on the micro:bit. Out white croc clip connects to the tip of the headphone jack and then to Pin0 of our micro:bit.

Getting Started PXT

To code our project we shall open a web browser and visit https://pxt.microbit.org and we start with a blank project.

The micro:bit pxt blocks interface looks and feels like the Scratch editor

pxt 101

The pxt editor is block based, meaning that we click on the block menu, just left center of the screen. To open a menu, click on it and you will see the blocks that it contains. This will show an expanded window that will briefly lie over the coding area, to the right of the screen. Blocks can be moved from the menu to the coding area and connected in the same manner as with Scratch.

On the left of the screen we have an interactive micro:bit simulator that enables us to test our code before we deploy it to the real hardware. Very handy!

The Block Menu

Here we can see the Basic menu expanded, showing all of the blocks in that menu

PXT Project

We start the code for this project by first clearing the “on start” block from the coding area, drag the block to the menu and drop it to tidy it away. We shall write our code inside the “forever” loop and our first task is to create a Variable. So go to Variables and click on “Make a Variable”. Call the variable “soil” and then drag the block “set soil to 0” and place it inside the loop. If you can’t see this block, it might be called “set item to 0”, use that block and click on the dropdown arrow to change the variable to “soil”. Now we need to store some data inside the “soil” variable. In this case the variable is used to store the conductivity of our soil, this is a value from 0 to 1023, which represent the voltage conducted via the soil.

Our sensor is connected to Pin 1 of the micro:bit, so to access the pin click on “Advanced” and then find the “Pins” menu. From there drag the “analog read pin P0” and place it over the zero of “set soil to 0”. Now change the pin to P1, this means that the value being read by the sensor will be stored in our variable, “soil”.

In order to check the value given by the sensor against what we consider to be “fairly dry” and “very dry” we need to use a conditional test call “if”. Go to the “Logic” menu and drag the “if true, then” block and place it inside the “Forever loop” and underneath our previous code. Now you can see a blue “cog” in the top left of this block. Click on it and you will see if, else if and else blocks. Assemble them so that we have from top to bottom, “if”, “else if” and “else”. To close the pop up click on the cog.

We now need to use an “ __ and __” block from “Logic”, drag and place this over the “true” of our conditional test. In the first blank space of “__ and __” we shall go back to “Logic” and drag a “0 < 0” block and place it in the blank. This block tests to see if the value in the first blank is less than the value in the second blank. Our first value will be the data stored in our “soil” variable, so go ahead and get that block and place it in the first blank. In the second blank we need to set a limit, in this case we typed the value “500” as this is roughly half the value of conductivity, meaning our soil may be a little dry. Now grab another “0 < 0” block and place that in the other blank space or our “and” block. Again the first value is our “soil” variable, but remember to change the < to a > symbol as we need to check that the soil moisture, and its conductivity is between 200 and 500 for this condition to be evaluated as True. In the last blank space type “200” to set the lower limit.

If the soil moisture reports to be between 200 and 500 then the “if” condition is “True” and the code inside the condition is activated. In this case we need to show the user that the plant is unhappy, we use the “show icon” block from “Basic” and set it to “sad” We also go to the “Music” menu and drag “play tone Middle C for 1 beat” so that it connects under the previous block. This will play a slow “heartbeat” indicating the plant requires water.

For the “else if” condition we shall once again use the “0 < 0” block from the “Logic” menu, but this time we only compare the value inside our “soil” variable with < 200, this means that the plant is in dire need of water. If this condition is True, then we use another “show icon” block from “Basic” and set it to “angry” and we also use a “play tone Middle C for 1 beat” block from “Music” but we change the tempo to “½ beat” to quicken the heartbeat.

For the final “else” condition we do not need to set any values to compare against. Else is used in case all of the previous conditions are False. If all are False, then Else MUST be True. In this case we use it to show that the plant is healthy and well watered. We use the “show icon” block from Basic, and give the plant a happy face. With this final block we now have all of the code for this project. So now click on Download and then connect your micro:bit to your computer. Copy the file from your Downloads folder to the micro:bit and this will cause it to reboot and then run the code.

You can now disconnect the micro:bit from your computer and power it using a USB power bank or the official micro:bit AAA battery pack. Place the sensor into the soil and listen to the health of your plant!

Congratulations you have completed! During these projects we learnt that

  • Soil when wet is a good conductor of electricity.
  • We can connect sensors to the micro:bit and measure the environment around us.
  • Creating an invention does not require lots of code or expensive equipment.
  • Plants need lots of water to grow.

How could we take this further?

from microbit import *
import music
while True:
    soil = pin1.read_analog()
    if soil < 500 and soil > 200:
    elif soil < 200:

Dare you take on the challenge that is Python? In the download for this project we have included the code to recreate the plant protector project using Micro Python. To use this code with your micro:bit you will need to go to http://python.microbit.org/ and write the code. Then plug in your micro:bit and click on Download to download the code, copy the file to your micro:bit and it will reboot and run the plant protector code. There is no need to change the wiring of the project.

Learn More

This Project on Github


micro:bit Python


Micro:bit Foundation Official Website