Skip to content

3. Micro:bit Iteration (Event Loop)

3.1 What is a Loop?

A loop is used to repeat code.

A common loop in MicroPython is:

1
while True:

This means:

  • Run the code forever
  • The program keeps repeating until stopped

3.2 Basic Event Loop Example

1
2
3
4
5
6
7
from microbit import *

while True:
    display.show(Image.HEART)
    sleep(500)
    display.clear()
    sleep(500)

What happens

  • The program shows a heart
  • Waits
  • Clears the screen
  • Repeats forever

3.3 How the Loop Works

Step-by-step:

  1. The program enters the loop (while True)
  2. Runs the code inside
  3. Goes back to the top
  4. Repeats again

This continues forever unless:

  • The micro:bit is reset
  • Power is removed

3.4 Indentation (VERY IMPORTANT)

Python uses indentation to define what belongs inside the loop.

Correct Example

1
2
3
4
5
6
7
from microbit import *

while True:
    display.show(Image.HAPPY)
    sleep(1000)
    display.show(Image.SAD)
    sleep(1000)

Both images repeat because they are inside the loop


Incorrect Example

1
2
3
4
5
6
7
from microbit import *

while True:
    display.show(Image.HAPPY)

sleep(1000)
display.show(Image.SAD)

What happens

  • HAPPY repeats forever
  • SAD never runs

3.5 Fix the Indentation

1
2
3
4
5
6
7
from microbit import *

while True:
    display.show(Image.HAPPY)
    sleep(1000)
    display.show(Image.SAD)
    sleep(1000)

3.6 Using Loops with Buttons

Loops are often used to check inputs like buttons.

1
2
3
4
5
6
7
from microbit import *

while True:
    if button_a.is_pressed():
        display.show(Image.YES)
    else:
        display.show(Image.NO)

What happens?

  • The program constantly checks button A
  • Shows different images depending on input

3.7 Combining Loops and Functions

We can call functions inside a loop.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from microbit import *

def flash(times):
    for i in range(times):
        display.show(Image.HEART)
        sleep(200)
        display.clear()
        sleep(200)

while True:
    if button_a.is_pressed():
        flash(3)

What happens?

  • The loop runs forever
  • When button A is pressed → function runs

3.8 Example: Random Behaviour

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from microbit import *
import random

def random_delay():
    return random.randint(1, 3)

def show_face(delay):
    display.show(Image.HAPPY)
    sleep(delay * 1000)

while True:
    d = random_delay()
    show_face(d)

What happens?

  • A random number is generated
  • The display shows for a random time
  • Repeats forever

3.9 Summary

Loops allow us to:

  • Repeat actions continuously
  • Create interactive programs
  • Build event-driven systems

Key Concepts

  • while True: → infinite loop
  • Indentation → controls what repeats
  • Functions → can run inside loops
  • Inputs → (e.g. buttons) work well with loops

3.10 Practice Ideas (Micro:bit)

  1. Create a loop that:

    • Flashes a smiley face continuously
  2. Create a program that displays "Hello "