Variables on the micro:bit: temperature, light and the LED palette

Lesson 4 of 14 · micro:bit

In the previous lessons you saw how to use data that can be shown on the micro:bit as a number or as text, but that is not enough, because we did not save the value in memory to use it later. The values we use for data can also be given names, so that they are easier to use later. To make this possible, data has to be created as variables. In the MakeCode environment the Variables palette of commands is used to manage variables.

For example: so far we have shown a number on the micro:bit only with the functions show number [8] and show string [text], as in the following pictures:The show number blockThe show string blockwhere instead of 0 we would put the number we want, for example 8, and instead of "Hello" the text we want. We have shown data such as numbers and text on the micro:bit screen, but we have not saved these values to use them later. Using variables lets us store values that we can use again and change more easily. In the MakeCode environment the Variables palette helps you create and work with variables.

For example, instead of showing the number 8 directly with the function show number, we can assign that value to a variable. This is how we create the variable number and set it to 8:

let number = 8;
basic.showNumber(number);

Using variables in micro:bit projects

In this lesson we use MakeCode for the micro:bit, where we program with blocks, as in Scratch, and the same program can also be seen as JavaScript or Python code.

MakeCode is not a separate programming language but an environment for block coding that also supports JavaScript and Python for text programming. In the MakeCode editor users can choose between blocks (which are intuitive for beginners) or switch to JavaScript and Python, which are more powerful languages for more advanced coding.

Example 1: A step counter

Task: make a program that counts steps by registering the shaking of the micro:bit and increasing the value of the variable steps by 1 with every shake. Show the number of steps on the micro:bit with every step.

Solution: In the Variables palette:

  1. create a variable called steps
  2. set the starting value to 0 with: set [steps] to [0]
  3. create an on shake event
  4. put these commands inside the block for that event:
    1. change [steps] by [1] - from the Variables palette and
    2. show number - from the Basic palette

Video 1: A step counter (SR)

Description: to make a step counter, first create a variable that keeps a whole number – the number of steps. A step shakes the micro:bit, so we create the gesture event "on shake". As a reaction to this event the number of steps, which is zero at the start, increases by 1. Follow these instructions:

Instructions:

  1. Create a new project called "Step counter"
  2. In the Variables palette create a new variable
  3. Two more commands will now appear in the palette (see picture 2):
    1. set [steps] to [0]
    2. change [steps] by [1]

The step counter exercise: defining the variable for the number of steps

Picture 1: The "Step counter" exercise: defining the variable

The step counter exercise: the Variables palette

Picture 2: The "Step counter" exercise: the Variables palette

4. Put the "on start" block on the workspace.

5. Choose set [steps] to [0] to set the value of the variable steps to zero at the start.

6. Add the "on shake" block from the Input palette and drag it onto the workspace. This block is the event of "shaking" the micro:bit, which happens with every step of the person wearing the micro:bit.

7. Add the command change [steps] by [1] and put it into the "on shake" block. This means that the value of the variable "steps" changes by 1 when the "on shake" event happens.

8. Add the command show number [steps] twice: 1) inside the "on start" block, so that a zero is shown on the micro:bit at the very start, and 2) inside the "on shake" block, so that the value is shown on the micro:bit every time the number of steps changes.

The complete code now looks like picture 3:

The step counter exercise: the complete code in blocks

The same program in JavaScript (MakeCode):

let steps = 0
basic.showNumber(steps)

// every time the micro:bit is shaken, the number of steps grows by 1
input.onGesture(Gesture.Shake, function () {
    steps += 1
    basic.showNumber(steps)
})
Picture 3: The "Step counter" exercise: the complete code

The picture shows the state after three steps, so the micro:bit simulator shows the number 3. The Python code is shown in picture 4:

The program in Python (MakeCode):

def on_gesture_shake():
    global steps
    steps += 1
    basic.show_number(steps)
input.on_gesture(Gesture.SHAKE, on_gesture_shake)

steps = 0
basic.show_number(steps)
Picture 4: The "Step counter" exercise: the complete code in Python

Measuring the light in a room

You can use the micro:bit to measure the light level in a room.

The micro:bit uses its LED grid as a light sensor, because it has no separate light sensor. The LED screen detects the level of the light around it by measuring the light that falls on it. This can be used in programs with functions such as input.lightLevel() in JavaScript or in the MakeCode environment. Reading the light level returns a value from 0 (complete darkness) to 255 (maximum brightness), which lets the device react to changes of the light in the room.

We will show the light level as a number, so we will use the show number command, but instead of a number we can insert the "light level" block from the Input palette, as shown in picture 5:

Programming the micro:bit: measuring the light level with the light level block

The same program in JavaScript (MakeCode):

// "light level" is in the Input palette – a value from 0 (dark) to 255 (very bright)
basic.showNumber(input.lightLevel())
Picture 5. Programming the micro:bit: measuring the light level with the light level block

Example 2: Measuring the light and the temperature at the press of a button

In this task you program the micro:bit to show the light level when button A is pressed, and the current temperature when button B is pressed.

Instructions:

  1. Step 1: Press button A.
  2. Step 2: The current light level in the room will be shown on the screen.
  3. Step 3: Press button B.
  4. Step 4: The current temperature in degrees Celsius will be shown on the screen.

Additional explanations: the micro:bit can measure the temperature with its built-in temperature sensor, which reads the temperature of the microprocessor and uses it as an approximate value of the temperature around it.

Using the temperature block from the Input palette or the command input.temperature() (in the MakeCode environment or in JavaScript), the micro:bit reads and shows the temperature in degrees Celsius.

The solution of the example is shown in picture 6:

Programming the micro:bit: measuring the light level – the solution

The same program in JavaScript (MakeCode):

input.onButtonPressed(Button.A, function () {
    basic.showNumber(input.lightLevel())    // light level
})
input.onButtonPressed(Button.B, function () {
    basic.showNumber(input.temperature())   // temperature in °C
})
Picture 6. Programming the micro:bit: measuring the light level – the solution

The program in Python (MakeCode):

def on_button_pressed_a():
    basic.show_number(input.light_level())
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_button_pressed_b():
    basic.show_number(input.temperature())
input.on_button_pressed(Button.B, on_button_pressed_b)
Picture 7. Programming the micro:bit: measuring the light level – the solution in Python

Video 2: Exercise – measuring the light level and the temperature (SR)

Independent work: measuring temperature and light at intervals

Program the micro:bit to measure and show the light level and the temperature automatically every 10 seconds, without pressing the buttons.

Tasks for independent work:

  1. Programming the interval: Set up the program so that it measures the light level and the temperature every 10 seconds.
  2. Showing the results: Show the light level on the screen, and right after that show the temperature.
  3. Testing: Put the micro:bit in different places (for example next to a window and far from the light) and compare the readings.

Note: Use the commands input.lightLevel() and input.temperature() to read the values, and the function basic.pause(10000) for a 10-second pause between the readings.

This task lets you check and record the changes of the temperature and the light in a room at certain time intervals, which can be useful for understanding how the conditions around us change.

The LED palette of commands

On the micro:bit the LEDs are arranged in a 5x5 grid, which forms a simple coordinate system. The coordinates go from (0,0) in the top left corner to (4,4) in the bottom right corner. This arrangement lets every LED be controlled individually with X and Y values: X increases from left to right, and Y from the top to the bottom.

In the MakeCode environment the LED palette of commands offers different options for working with the LEDs, including blocks for switching specific points on and off (the "plot" and "unplot" commands), as well as showing symbols and animations.Programming the micro:bit: the coordinate system of the LEDs

Picture 8. Programming the micro:bit: the coordinate system of the LEDs

The LED palette of commands in the MakeCode environment for the micro:bit has these basic commands:

  1. plot - Switches on a certain LED at the given coordinates (x, y).
  2. toggle - Switches the state of an LED between on and off.
  3. unplot - Switches off the LED at the given coordinates.
  4. point - Tells the state of the LED (on / off) at the coordinates x and y.
  5. plot bar graph - Shows a bar graph based on the given value compared with the maximum value.

Programming the micro:bit: the basic commands of the LED palette

Picture 9. Programming the micro:bit: the basic commands of the LED palette

If we click the "more" button we get more commands:

  • plot brightness - Switches on the LED at the coordinates x and y with a specific light intensity.
  • point brightness - Reads the brightness of the LED at the coordinates x and y.
  • brightness - Gets the brightness of the LED screen when it is on.
  • set brightness - Sets the brightness of all the LEDs to a value from 0 to 255.
  • led enable [false or true] - Switches the LED screen on or off.
  • stop animation - Stops all the animations currently running on the LED screen.
  • set display mode - Sets the display mode to black and white or to greyscale.

Programming the micro:bit: the additional commands of the LED palette

Picture 10. Programming the micro:bit: the additional commands of the LED palette

Example 3: The LED palette – showing a picture on the LEDs

Task: Make a program for the micro:bit that switches on the lights in the shape of an upside-down letter L, as shown in picture 11:

Programming the micro:bit: the LED palette – drawing an upside-down letter L
Picture 11. Programming the micro:bit: the LED palette – drawing an upside-down letter L

Solution

For this task the solution switches on certain LEDs at the given coordinates in the 5x5 grid. Every coordinate defines the position of an LED that should be lit, and the task is to show the picture using these coordinates.

An explanation of the solution:

  1. Understanding the positions of the LEDs: The LED grid is a coordinate system where (0,0) is in the top left corner, and the values increase to the right for the x axis and downwards for the y axis.
  2. Switching on the LEDs at the given coordinates:
    • The horizontal row of LEDs: switched on at the coordinates (1,1), (2,1) and (3,1).
    • The vertical row of LEDs: switched on at the coordinates (3,1), (3,2) and (3,3).
  3. Implementation in MakeCode: The plot(x, y) commands from the LED palette are used to switch on exactly the LEDs at these coordinates, which shows the wanted shape on the micro:bit screen.

This way of showing helps you understand the positions in the grid, and every position is entered in the code with coordinates (x, y), which can be useful for drawing different shapes or icons. The solution is shown in picture 12.

Programming the micro:bit – the solution: the LED palette, drawing an upside-down letter L

The same program in JavaScript (MakeCode):

// x – column (0–4, from left to right), y – row (0–4, from the top down)
led.plot(1, 1)
led.plot(2, 1)
led.plot(3, 1)
led.plot(3, 2)
led.plot(3, 3)
Picture 12. Programming the micro:bit – the solution: the LED palette, drawing an upside-down letter L

In Python the solution would be as shown in picture 13:

The program in Python (MakeCode):

led.plot(1, 1)
led.plot(2, 1)
led.plot(3, 1)
led.plot(3, 2)
led.plot(3, 3)
Picture 13. Programming the micro:bit – the solution in Python: the LED palette, drawing an upside-down letter L

Example 4: Setting the brightness of one LED

Task: Create a program that sets the light intensity of one LED on the micro:bit. Switch on the LED at position (2,4) and set its light intensity to 200.

Instructions for solving:

  1. Understanding the problem: The positions of the LEDs are defined by the coordinates x and y, where x is the horizontal position and y the vertical one (values from 0 to 4). The light of an LED can be set from 0 (off) to 255 (maximum brightness).
  2. Writing the code:
    • Start a new project in MakeCode.
    • In the on start block add the plot x y brightness block from the LED palette (under "more").
    • Set the coordinates to (2,4) and the brightness to 200.

With these steps you will learn how to control individual LEDs and their light intensity.

Programming the micro:bit – the solution: the LED palette, setting the brightness of one light on the LED board

The same program in JavaScript (MakeCode):

led.plotBrightness(2, 4, 200)   // switches on the light in column 2, row 4, with brightness 200 (0–255)
Picture 14. Programming the micro:bit – the solution: the LED palette, setting the brightness of one light on the LED board

A task for independent work: creating a "loading" animation on the micro:bit

Task description: Create an animation that shows a loading process on the micro:bit, using a circular shape on the LED grid. The animation should repeat without stopping, imitating a circular loading bar.

Instructions for solving:

  1. Start a new project in the MakeCode environment for the micro:bit.
  2. In the forever block put the code that runs the animation.
  3. Use the plot (x, y) block to show LEDs in a circular arrangement.
  4. Add a pause (ms) between the LEDs to get the animation effect.
  5. At the end add clear screen to clear the screen before the next repetition.

Experiment with different coordinates and pauses to adjust the speed and the look of the animation as you like.

Test 2: Check your knowledge (SR)

Click the Start button to begin the test! First look at the pictures that go with questions 3 and 4. The test is in Serbian; the English quiz is below it.

Programming the micro:bit – pictures for test 2, questions 3 and 4
Picture 15. Programming the micro:bit – pictures for test 2, questions 3 and 4

Check your knowledge

Open the quiz

Variables, sensors and the LED palette

1. What does the block change steps by 1 do?




2. In which range is the value of the light level block?




3. What does led.plot(2, 4) do?




4. What is the coordinate of the top left light?