A learning resource fromSpread Robotics Education Foundation

Lesson 07 · 60 minutes · Decisions

Make a sunlight sensor

Measure the light, ask a true-or-false question, and make the micro:bit choose what to show.

New programming concepts

If / else decision

If / else lets a program choose between two paths. It checks a condition. If the condition is true, it follows the first path. Otherwise—else—it follows the second.

Is light level > 100?
YESShow sunNOClear screen
Condition

A question with a true or false answer.

> greater than

A comparison asking whether one number is larger.

Selection

The program selects which path to follow.

Try it here

Move from dark to bright

AB

DarkBright

60 is not greater than 100. The screen is clear.

MakeCode steps

Build the decision

  1. 1

    Start a new project

    Name it Sunlight Sensor.

    Open MakeCode
  2. 2

    Add a decision

    From Logic, place if / then / else inside forever.

  3. 3

    Build the condition

    Add a > comparison. Put light level on the left and 100 on the right.

  4. 4

    Choose the true path

    Inside if, add show icon and choose the sun.

  5. 5

    Choose the else path

    Inside else, add clear screen.

  6. 6

    Test the threshold

    Change the simulator light level. On the board, cover and uncover the LEDs. Adjust 100 if your room needs a different threshold.

Read the condition aloud

“If the light level is greater than 100, show the sun. Else, clear the screen.” Reading code aloud helps learners follow both paths.

For the teacher

A calm 60-minute plan

Human decision
If it is bright, raise a hand; else lower it.

Draw both paths
Use a paper true/false decision tree.

Guided build
Read each block aloud.

Test the threshold
Cover, uncover, and adjust.

Explain
Each team describes true and false paths.

Common misconception

The number 100 is not “correct” everywhere. It is a threshold chosen for the room. Testing and adjusting it is part of the engineering work.

Before you finish

Can you explain it?