Microbit Robotics & Engineering Sensors & Circuits
Beginner
60 mins
Teacher/Student led
+110 XP
What you need:
Chromebook/Laptop/PC
Microbit

Light Sensing for Plant Health

In this exciting lesson, you will learn how to use a microbit's built-in light sensor to measure the amount of light a plant is receiving. You'll then program the microbit to display these light levels.

Teacher Class Feed

Load previous activity

    1 - Setup your microbit

    Welcome to our exciting project! We're going to use a special tool called a microbit. This is a tiny computer that we can program to do lots of cool things. One of the cool things it can do is sense how much light is around it. Just like how our eyes can tell if it's bright or dark, the microbit can do the same!

    First, we need to set up our microbit. Go to the MakeCode Microbit Editor and start a new project. This is where we'll write our code to tell the microbit what to do.

    Don't worry if you've never done this before, we'll guide you through each step. Remember, coding is like giving instructions to the computer. So, we're going to tell the microbit to look at the light and tell us how bright it is. Let's get started!


    2 - Get your microbit ready to measure light

    First, we need to tell our microbit that we're going to be measuring light. To do this, we're going to make a special container (called a 'variable') where we can keep our light measurements. We're going to name this container 'light' and to start with, we're going to put the number 0 in it.

    This is like telling our microbit, 'Hey, we're going to start measuring light now, but right at this moment, we're not measuring anything yet, so just hold onto this number 0 for us.'

    Here's how you tell your microbit to do this:

    let light = 0

    This is like saying 'Let there be a container called 'light', and for now, put the number 0 in it.'

    3 - Reading and Saving the Light Level

    Now, let's make our microbit smart enough to keep checking the light level around your plant. We'll do this by using a special block of code called 'forever'. This block will keep repeating whatever we put inside it. In our case, it will keep checking the light level.

    Next, we need to remember the light level that our microbit sees. For this, we'll use something called a 'variable'. You can think of a variable as a small box where we can store information. We'll name our variable 'light' and store the light level in it.

    Here's the code to do this:

    {basic.forever(function() {let light = input.lightLevel()})}

    This code means: 'Forever, keep checking the light level and remember it in the 'light' box.'

    4 - Show the Light Level on Your Microbit

    In this step, you will program your microbit to display the light level it measures. This number tells us how much light your plant is getting. We'll use a 'show number' block to display this information. We'll put this block inside a 'forever' block, which means your microbit will keep updating and showing the light level continuously.

    Add the following code:

    basic.forever(function() {let light = input.lightLevel(); basic.showNumber(light);})

    This code does two things: First, it measures the light level and saves it as 'light'. Second, it shows the 'light' number on your microbit's LED screen. The 'forever' part means it keeps doing these two things over and over again.

    Once you've added this code, you can test it out on your virtual microbit. The virtual microbit is a digital version of the physical microbit that you can use to test your code. You'll see it on the screen when you're coding. When you run your code, the virtual microbit will show the light level just like a real microbit would. This is a great way to see if your code is working correctly before you use it with a real microbit and a real plant.


    123learn · Online learning platform

    Unlock the full learning experience

    You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2026. 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