MicroPython — Mini projects

Lesson 12 of 17 · MicroPython and the micro:bit

In this lesson you combine everything you have learned about the micro:bit so far into mini projects and games.

In the earlier lessons you learned how to use:

  • the LED matrix and showing text/pictures
  • buttons A and B
  • sensors: acceleration, tilt, compass, temperature
  • radio communication between devices

Now it is time to put all of that together in mini projects and games.

Mini projects are the step from single commands to real, working applications. Through them students develop:

  • logical thinking
  • combining several concepts in one program
  • understanding communication between devices
  • creativity and experimenting

Unlike the earlier examples, which demonstrated one feature, here you will do projects that use at the same time:

  • loops (while True)
  • conditions (if, elif, else)
  • working with the buttons
  • radio communication
  • showing things on the LED matrix
  • random values (random)

The goal is not only to copy the code, but to:

  • understand how it works
  • change the parameters
  • add new features
  • create your own version of the project


How to do mini projects?


Before you run the code:

  • Read the description of the project carefully
  • Check that you have all the devices you need
  • Think about what will happen before you run the program

After running it:

  • Test different situations
  • Make a mistake on purpose and watch what happens
  • Try to add at least one new feature

In this way projects become small research tasks, not just ready-made solutions.



The idea of a standard mini project


To make things easier to understand, every mini project is presented with the same structure:

  • Project title
  • Short description
  • Requirements (devices, pins, modules)
  • Code (a working example)
  • Explanation (what each important line does)
  • Extensions (ideas for further development)

This structure makes possible:

  • easier following of the lesson
  • finding your way in the code faster
  • a clear organisation of knowledge
  • gradual progress towards more complex projects


The "Hidden number" game


Short description: One micro:bit "thinks of" a number, and the others try to guess it using radio communication.

Requirements: 2 micro:bit devices, a radio connection.


# The device that thinks of a number
from microbit import *
import radio
import random

radio.on()
radio.config(group=1)

secret_number = random.randint(1, 9)

while True:
    message = radio.receive()
    if message:
        guess = int(message)
        if guess == secret_number:
            radio.send("Correct!")
        elif guess < secret_number:
            radio.send("Higher!")
        else:
            radio.send("Lower!")

# The device that guesses
from microbit import *
import radio
import random

radio.on()
radio.config(group=1)

while True:
    if button_a.was_pressed():
        number = random.randint(1, 9)
        radio.send(str(number))
    message = radio.receive()
    if message:
        display.scroll(message)

Explanation:

  • The radio module sends and receives messages between the devices.
  • The while True loop keeps checking the state of the buttons and the messages.
  • The random function chooses the secret number and the guesses.

Extensions: add more players, a timer or animations when a player guesses the number.


4. Integrated challenges (a higher level)


Now try projects that combine several sensors + radio + decision logic. These are tasks for students who want more of a challenge.


Project 1 — A smart alarm

  • If the temperature is above 28°C → send a radio warning
  • If the device is shaken → start an alarm animation
  • The other micro:bit receives the message and shows a warning

Concepts: sensors + conditions + radio + animation


Project 2 — A team step counter

  • Each device counts its own steps
  • It sends the number over the radio from time to time
  • One device adds up the results of the whole team

Concepts: variables + radio + data processing


Project 3 — A digital compass game

  • The player must turn the device towards north
  • When they hit the direction → it sends a radio message
  • The first to send the message gets a point

Concepts: compass + radio + scoring



5. The structure of a serious mini project


When you make a bigger project, think in steps:

  1. What is the goal of the project?
  2. Which sensors do I use?
  3. What makes the decision (if/else)?
  4. How do the devices communicate?
  5. How do I test each part separately?

This is a real introduction to the engineering way of thinking.



6. Projects for independent work (no ready-made code)


Try to come up with a solution for the following ideas yourself:

  • A mini weather station with radio data transfer
  • A reaction game (the first to press the button after a signal)
  • A radio motion detector (reports if the device is moved)
  • A voting system (A or B over the radio)

Do not look for ready-made code straight away. First write the algorithm:

  • Which variables are needed?
  • Which conditions?
  • Which radio messages?


7. What have you actually learned?


  • Working with hardware (sensors and buttons)
  • Working with communication (radio)
  • Decision logic
  • The structure of bigger programs
  • The basics of distributed systems

Now you are ready for:

  • bigger team projects
  • advanced games
  • robotics
  • IoT systems

MicroPython is no longer just a list of commands — it becomes a tool for building systems.

The final challenge — A big system with 3 micro:bit devices


Now you build a real small distributed system. Three micro:bit devices have different roles and communicate with each other.


The idea of the system

  • Device 1 — Sensor (measures data)
  • Device 2 — Controller (makes decisions)
  • Device 3 — Actuator (reacts)

The system works like a real IoT model:

Sensor → Controller → Actuator

An example scenario — A smart security station

Device 1 (Motion sensor)

  • Detects shaking or tilt
  • Sends the message: "MOTION"

Device 2 (Central controller)

  • Receives the message
  • Checks the temperature
  • If the temperature is high → sends "ALARM"

Device 3 (Alarm system)

  • If it receives "ALARM" → shows a warning
  • It can activate a servo or an LED signal

An example of the basic structure (Device 1)


from microbit import *
import radio

radio.on()
radio.config(group=7)

while True:
    if accelerometer.was_gesture("shake"):
        radio.send("MOTION")
        display.show(Image.SQUARE)
        sleep(500)
        display.clear()

An example of the structure (Device 2)


from microbit import *
import radio

radio.on()
radio.config(group=7)

while True:
    message = radio.receive()

    if message == "MOTION":
        if temperature() > 25:
            radio.send("ALARM")

An example of the structure (Device 3)


from microbit import *
import radio

radio.on()
radio.config(group=7)

while True:
    message = radio.receive()

    if message == "ALARM":
        display.show(Image.SKULL)

Advanced challenges (for the strongest students)

  • Add device identification (an ID)
  • Add an acknowledgement of a received message (ACK)
  • Add more types of alarm
  • Add a time limit
  • Add a system reset

Questions to think about

  • What if the controller does not receive the message?
  • How do you avoid false alarms?
  • How do you test the system part by part?
  • How do you scale the system to 5 or 10 devices?

What have you mastered with this?

  • Radio communication
  • Working with sensors
  • Decision logic
  • Distributed systems
  • The basics of IoT architecture

This is no longer just a school task — it is a model of a real smart system.

Mini projects — Section 1

A digital thermometer with graphics

Short description: Showing the temperature from the micro:bit sensor and visualising it on the LED matrix or in Python Processing graphics.

Requirements: A micro:bit, the temperature sensor, a USB connection (or a serial port for the graphics), optionally Python Processing for a graphical display.


from microbit import *
import time

while True:
    t = temperature()
    display.scroll(str(t) + "C")
    sleep(1000)

Explanation:

  • The function temperature() returns a value in degrees Celsius.
  • The LED matrix shows the current temperature or a trend indicator.
  • Combined with Python Processing, a chart of the temperature can be drawn in real time.

Extensions: add writing to a CSV file, showing the minimum/maximum values, a graphical display of the trend.


Radio multiplayer — An advanced project

Short description: Two or more micro:bit devices take part in a game, sending data to each other by radio. It can be a scoring game or an interactive challenge.

Requirements: 2+ micro:bit devices, the radio module, the LED matrix, buttons A and B.


from microbit import *
import radio

radio.on()
radio.config(group=10)

score = 0

while True:
    if button_a.was_pressed():
        score += 1
        radio.send(str(score))
    incoming = radio.receive()
    if incoming:
        display.scroll("Opponent: " + incoming)

Explanation:

  • The loop keeps checking the buttons and the received messages.
  • The radio module sends the current score to the other device.
  • The LED matrix shows the opponent's score in real time.

Extensions: add more game levels, different kinds of interaction, sounds and animations.


Mini projects — Section 2

An IoT weather station

Short description: The micro:bit measures the temperature (the built-in sensor) and the soil moisture (an external analog sensor), and sends the data by radio to another micro:bit, which can pass it on to a computer for further analysis.

Requirements: 2 micro:bit devices, an analog soil moisture sensor on pin P0, optionally a serial connection with a Python program on the computer.


from microbit import *
import radio

# micro:bit MicroPython has no dht module (DHT11/DHT22 sensor),
# so we use the built-in temperature sensor and an analog moisture sensor
radio.on()
radio.config(group=5)

while True:
    temp = temperature()            # the built-in temperature sensor
    moisture = pin0.read_analog()   # an analog moisture sensor (0–1023)
    radio.send("T:{}C M:{}".format(temp, moisture))
    sleep(2000)

Explanation:

  • The sensors are read at regular intervals.
  • The radio sends the data to other micro:bit devices or to a Python program for visualisation.
  • The data can be recorded in a CSV file and shown graphically.

Extensions: add more sensors (pressure, light), a real-time chart, an alarm for extreme values.


Robotics with servo motors

Short description: The micro:bit controls servo motors, which makes basic robot functions possible, such as moving arms, doors or wheels.

Requirements: A micro:bit, 1-4 servo motors, a power supply, cables, possibly a robot chassis.


from microbit import *

# a servo on pin P0: a pulse every 20 ms (50 Hz)
pin0.set_analog_period(20)

def set_angle(angle):
    # 0° ≈ a 0.5 ms pulse, 180° ≈ a 2.5 ms pulse → a value from 26 to 128 (out of 1023)
    pin0.write_analog(int(26 + angle * (128 - 26) / 180))

while True:
    if button_a.was_pressed():
        set_angle(0)
    if button_b.was_pressed():
        set_angle(180)

Explanation:

  • Pressing the buttons changes the angle of the servo.
  • The function set_angle() gives precise control of the angle.
  • More servos can be added for coordinated movements.

Extensions: automatic movement sequences, a motion sensor for activation, integration with micro:bit games or visualisation in Processing.


Mini projects — Section 3

The micro:bit as a controller for a Python Processing game

Short description: Using the micro:bit as a joystick or controller, the player can control objects in a Python Processing game over a serial or radio connection.

Requirements: A micro:bit, a USB connection to the computer, Thonny + py5 (see the lesson micro:bit → py5 over the serial port), optionally the radio module for wireless control.


# Micro:bit code
from microbit import *

while True:
    x = accelerometer.get_x()
    y = accelerometer.get_y()
    # print() sends a line of text to the computer over the USB serial connection (115200)
    print("{},{}".format(x, y))
    sleep(100)

# py5 code (Thonny) – moving an object according to the micro:bit data
# The pyserial library does not work in Processing Python Mode, so this part is for py5.
from serial import Serial

ser = Serial('COM3', 115200)  # change the port
x_pos = 0
y_pos = 0

def setup():
    size(500, 500)

def draw():
    global x_pos, y_pos
    background(200)
    if ser.in_waiting:
        row = ser.readline().decode('utf-8').strip()
        parts = row.split(',')
        if len(parts) == 2:
            x_pos = int(parts[0])
            y_pos = int(parts[1])
    ellipse(remap(x_pos, -1024, 1024, 0, width),
            remap(y_pos, -1024, 1024, 0, height), 50, 50)

Explanation:

  • The micro:bit uses the accelerometer to detect how the device is tilted.
  • The data is sent over serial to the py5 program, which converts it into screen coordinates with the function remap().
  • The player can move the object by tilting the micro:bit.

Extensions: add more micro:bit controllers for multiplayer, use the buttons for extra functions, use a motion sensor for special actions in the game.


Related lessons and useful resources


If you want to extend your knowledge of using the micro:bit together with Python Processing, look at the following lessons and resources.


Related lessons on SvetProgramiranja.com



Useful external resources



By combining micro:bit sensors, radio communication and Processing you can make interactive games, simulations and IoT projects, and prepare interesting papers for school and competitions.


Related: the same topic in MakeCode and other lessons

If you worked with the micro:bit in blocks in the 5th or 6th grade, compare the blocks with the MicroPython code: