One-sentence summary
We learn event-driven programming: making the micro:bit do specific things when its A and B buttons are pressed.
Why does it matter?
In the previous lesson you showed hearts, arrows and your own drawings on the 5×5 LED matrix. But those programs only went one way: the micro:bit simply followed the order you wrote and never asked you anything.
Buttons change that. Now the board can wait for what the user does. Press one button and something happens; press another and something else happens. This is the simplest way to make a program interactive.
Look around and you will see the same idea everywhere. You press a lift button and the cabin arrives. You press a key on a game controller and the character jumps. You press a doorbell and a sound plays inside the house. They all share one thing: an event happens and the program responds to it. In this lesson we will start thinking about the micro:bit in exactly this way.
What is an event?
An event is a situation the program is waiting for and reacts to when it happens. "When button A is pressed" is an event. "When the board is shaken" is an event too.
In event-driven programming we separate two things:
- The event: What happened? (For example: button A was pressed.)
- The handler (response): What should happen next? (For example: show a heart icon.)
Everyday example: A doorbell
A doorbell is a perfect event-and-response example.
- Event: Someone pressed the bell.
- Response: A tune plays inside the house.
The bell does not ring when nobody presses it. So the program does not constantly say "do something"; it runs only when the event happens. The micro:bit buttons work the same way.
Everyday example: A light switch
Think of the light switch in your room.
- Event: You pressed the switch.
- Response: The light turns on.
Press it again and the light turns off. The same switch can give a different result depending on the situation. On the micro:bit, too, the same button can do different things.
The micro:bit's buttons
On the front of the micro:bit, on either side of the LED matrix, there are two physical buttons:
- Button A: On the left.
- Button B: On the right.
You can use each button on its own, and you can also create a third option by pressing both at the same time: A+B.
The "on button pressed" block in MakeCode
In MakeCode every event has its own block. Whatever you place inside the "on button A pressed" block runs only when A is pressed.
Example: show a heart with A and a star with B.
on button A pressed
show icon heart
on button B pressed
show icon diamond
Notice there is no forever block here. The board waits until you press. The moment you press, the matching block runs once.
A third option with A+B
We can add the A+B event to the same program. When both buttons are pressed together, let us clear the screen.
on button A pressed
show icon heart
on button B pressed
show icon diamond
on button A+B pressed
clear screen
Now one board gives us three different commands: heart, star and clear.
The same program in MicroPython
We can write the same logic in MicroPython. An important tool here is the button_a.was_pressed() function. It answers the question "has A been pressed since the last check?" with True or False.
In MicroPython we check the events ourselves inside a loop:
from microbit import *
while True:
if button_a.was_pressed():
display.show(Image.HEART)
elif button_b.was_pressed():
display.show(Image.DIAMOND)
if button_a.is_pressed() and button_b.is_pressed():
display.clear()
There are two different methods here:
was_pressed(): Catches the press event. It returnsTrueonce after you press and release.is_pressed(): Checks whether the button is pressed right now. This is handy for testing A+B at the same time.
The logic is exactly the same as the MakeCode blocks; only the way we write it changes.
Mini practice
Build your own "mood card." The goal: show three different faces using the two buttons and A+B.
Task steps:
- Show a happy face when A is pressed.
- Show a sad face when B is pressed.
- Show a surprised face when A+B is pressed.
MakeCode draft:
on button A pressed
show icon happy
on button B pressed
show icon sad
on button A+B pressed
show icon surprised
MicroPython draft:
from microbit import *
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.show(Image.SURPRISED)
elif button_a.was_pressed():
display.show(Image.HAPPY)
elif button_b.was_pressed():
display.show(Image.SAD)
Ideas to try: add a counter (A increases, B decreases), or show a different letter each time you press A. Test it in the simulator first, then flash it to the real board.
Common mistakes
Checking A+B after the other events
In MicroPython, if you check A and B on their own first, the program may already catch A and act before you manage to press both. Put the A+B check first; only then does pressing both together work as a separate event.
Confusing was_pressed and is_pressed
is_pressed() returns True again and again as long as the button is held, so the screen can flicker quickly. If you want one response per press, use was_pressed().
Showing inside "forever" and not waiting
In MakeCode, if you put the icon in the forever block, the board keeps showing the same thing without ever looking at the button. For a button response, put the command inside the "on button pressed" block.
Never clearing the screen
When you show a heart and then a star, some of the old LEDs may stay lit. If needed, use the clear screen command before showing a new shape.
Safety note
The micro:bit runs on low voltage and is a safe board, but a few points still matter:
- Ask an adult for help when you connect the board to a computer by USB or attach a battery pack. Be careful not to reverse the poles (+ and −).
- Use only battery packs made for the micro:bit and low-voltage batteries; never connect it to mains electricity.
- Do not place the board on bare metal surfaces, and be careful when touching the pins and solder points.
- If you attach the board to clothing or a bag to carry it, fasten it securely and do not let loose cables catch and pull it down.
Lesson summary
- An event is a situation the program waits for and responds to when it happens.
- The micro:bit has three button events: A, B and A+B.
- In MakeCode, every event has its own "on button pressed" block.
- In MicroPython, we check events ourselves with
was_pressed()andis_pressed(). - By attaching different responses to the same button, we make a program interactive.
Check questions
- In event-driven programming, what do "event" and "response" mean?
- How many button events does the micro:bit have, and what are they?
- In MakeCode, which block do you put the command in to show a heart when A is pressed?
- In MicroPython, what does the
button_a.was_pressed()function check? - What is the difference between
was_pressed()andis_pressed()?
Answers
- An event is a situation we wait to happen (for example, A being pressed). A response is what the program does when that event happens (for example, showing a heart).
- There are three button events: A, B and A+B, which happens when both buttons are pressed at the same time.
- You put the command inside the "on button A pressed" block, not the
foreverblock. - It checks whether button A has been pressed since the last check; it returns
Trueif it has andFalseif it has not. was_pressed()catches a press event (a press and release) once;is_pressed()checks whether the button is pressed right now and keeps returningTruewhile it is held.
Source and verification note
For “Buttons and Events”, verification focuses on whether the relationship between What is an event? and Everyday example: A light switch 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
Variables and Loops: By building a counter that increases with a button, we will learn to store information in a variable and repeat it with loops.