One-sentence summary
A variable is a box the micro:bit remembers something in; a loop is a structure that runs the same steps again and again. Put them together and you can build a counter that goes up every time you press a button.
Why it matters
In the Algorithms module, "sequence, condition and repetition" were the three basic building blocks. In the Python module, we wrote lines such as count = count + 1. Now we will see those same ideas running on a real board.
A program often needs to *remember* something. How many times did you press the button? How many steps did you take? What is the score? We use a variable to store that kind of information. A program also often needs to do a job *over and over*: blinking an LED, reading a sensor continuously, drawing a shape. We use a loop for that.
The micro:bit lets us express both ideas in MakeCode blocks and in MicroPython code. The logic you see in the blocks is exactly the same in the code; only the way it is written changes.
What is a variable?
A variable is like a box with a name written on it. We put a value inside the box, then read it or change it whenever we want.
The box picture
Imagine a box on the table labelled "score." At the start it is empty, meaning 0. For every correct answer you drop a stone into the box, so the number of stones inside goes up. A variable works exactly like this: the value inside can change over time, but the name stays the same.
To create a variable and show it on the micro:bit screen:
on start
set count to 0
show count
The same idea in MicroPython:
from microbit import *
count = 0
display.show(count)
Here count is the variable's name and 0 is its starting value. The command display.show(count) shows the value inside the variable on the 5×5 LED screen.
Changing the value
The most powerful thing about a variable is that its value can change. For example, increasing it by one:
change count by 1
show count
In MicroPython:
count = count + 1
display.show(count)
You can read the line count = count + 1 as "take the box's old value, add 1 to it, and put the result back in the box." This line, which you met in the Python module, works the same way on the micro:bit.
What is a loop?
A loop is a structure that runs the same steps again and again. That way we do not have to write the same command ten times, a hundred times, or even forever by hand.
The "repeat" loop
For a fixed number of repetitions we use a "repeat" loop. Example: blinking the heart icon 4 times.
repeat 4 times
show heart icon
pause half a second
clear screen
pause half a second
The same loop in MicroPython:
from microbit import *
for i in range(4):
display.show(Image.HEART)
sleep(500)
display.clear()
sleep(500)
Here range(4) says the loop will run 4 times. sleep(500) waits half a second (500 milliseconds).
The "forever" loop
Some programs need to run without ever stopping. On the micro:bit we use the "forever" block for this. As long as the board has power, this block repeats its steps from top to bottom.
forever
if <button A is pressed> then
change count by 1
show count
In MicroPython, the "forever" loop is written with while True:
from microbit import *
count = 0
while True:
if button_a.was_pressed():
count = count + 1
display.show(count)
while True means "the condition is always true, so loop forever." And button_a.was_pressed() tells us whether button A has been pressed since the last check.
Combining a variable and a loop: a counter
Now let us put the two ideas together. Our goal: every press of button A makes the number on screen go up by one, and pressing button B resets the counter.
MakeCode blocks
on start
set count to 0
show count
forever
if <button A is pressed> then
change count by 1
show count
if <button B is pressed> then
set count to 0
show count
MicroPython
from microbit import *
count = 0
display.show(count)
while True:
if button_a.was_pressed():
count = count + 1
display.show(count)
if button_b.was_pressed():
count = 0
display.show(count)
Three ideas work together in this program: the variable (count) remembers the information, the loop (forever / while True) checks the buttons without stopping, and the condition (if) changes the value only at the right moment.
Two everyday examples
Counter example: At basketball practice you count how many free throws you take. After each shot you add one to the number you are holding in your head. That "number" in your head is a variable, and the behaviour you repeat after every shot is a loop.
Turnstile example: Think of the turnstile at the entrance of a sports centre. Each person who goes in makes a counter go up by one. At the end of the day the total number of entries is shown on a display. A micro:bit counter works exactly like this; there is just a button A instead of a turnstile.
Mini practice
Let us build a step counter. You can try it on your own micro:bit or in the online MakeCode simulator.
- Create a variable named
stepsand set it to0at the start. - Show this value on the screen at the start.
- Inside a "forever" loop: when button A is pressed, change
stepsby 1 and show it. - When button B is pressed, set
stepsto0and show it. - Run it: press A a few times and watch the number go up; then press B and check that it resets.
When you are ready, try this too: once the number goes above 9, a single digit will not fit on the screen. If you use the "scroll" block instead of "show" for the number, large numbers will slide across from left to right.
Common mistakes
Forgetting to set the variable at the start
If you do not give the variable its first value, the program will not know which number to start from. Always set the variable to 0 in the "on start" block (or before while True).
Changing the value but forgetting to show it
Even if you say change count by 1, you still need a show count so you can see it on screen. Changing and showing are two separate steps.
Choosing the wrong loop
Use "repeat" for a fixed number of repetitions and "forever" for continuous checking. If you put a button check inside "repeat 4 times," the program stops after four checks.
Mixing up was_pressed and is_pressed
In MicroPython, button_a.is_pressed() is true while the button is held down; if you do not lift your finger, the counter rises very fast. For a counter, button_a.was_pressed() is better, because it counts each press once.
Safety note
If you power the micro:bit from a battery pack, plug and unplug the battery together with an adult, and use only the recommended low-voltage (3V) battery pack for the board. If you turn the board into a wearable project such as a step counter by attaching it to a wristband or a bag, fasten it securely and make sure the wires cannot snag and pull loose. Do not handle the board with wet hands, and do not let metal surfaces touch the pins.
Lesson summary
- A variable is a named box that remembers a value while the program runs.
- A line like
count = count + 1is the basic way to update a variable's value. - The "repeat" loop runs a fixed number of times, while the "forever" (while True) loop runs without stopping.
- By combining a variable, a loop and a condition, we can build a counter that goes up on every button press.
- The same logic is identical in MakeCode blocks and MicroPython code; only the writing changes.
Review questions
- What was a variable compared to, and what is it for?
- Explain the line
count = count + 1in your own words. - What is the main difference between a "repeat" loop and a "forever" loop?
- Which step do we need to add in the program to reset the counter?
- In MicroPython, why is
was_pressedbetter thanis_pressedfor a counter?
Answers
- It was compared to a box with a name on it. We put a value inside and, while the program runs, we can read or change that value, so the program remembers a piece of information.
- Take the box's (variable's) old value, add 1 to it, and put the result back in the same box. In other words, it increases the value by one.
- A "repeat" loop runs a set number of times and then stops; a "forever" loop keeps running as long as the board has power.
- We must add a step that sets the variable back to
0(for example, on button B press:set count to 0andshow count). was_pressedcounts each press only once. Becauseis_pressedstays true while the button is held down, the counter rises very fast and out of control.
Source and verification note
For “Variables and Loops”, verification focuses on whether the relationship between What is a variable? and Changing the value remains consistent across examples. MakeCode and MicroPython names can vary slightly by version. Test in the simulator first; when external components are connected, check the board’s pin and voltage limits separately.
Next lesson
The Accelerometer: We will learn how the micro:bit senses movement, tilt and shaking, and grow our counters so they respond to motion.