Microbit Python Computer Science
Advanced
40 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC or Microbit

Radio Messaging Network

In this lesson, you'll create a simple chat system with multiple Micro:bits using radio communication. Build a network to send and receive predefined messages, using buttons to select and send them while learning string manipulation and functions.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction to the Radio Messaging Network

    In this lesson, you'll build a simple chat system using multiple Micro:bits that communicate via the radio module. You'll create a network where devices can send and receive predefined messages. You'll use buttons to select messages from a list, manipulate strings to add sender names, and implement functions to handle receiving and displaying messages with conditional logic for automatic responses. This project reinforces string manipulation, lists, control structures, and functions for multi-device interaction.

    By the end of this lesson, your Micro:bit will be part of a radio messaging network. Open the Micro:bit Python editor at python.microbit.org and start a new project. For testing with multiple devices, you'll need at least two Micro:bits.

    Test your code in the simulator. The radio module allows wireless communication between Micro:bits within range.

    2 - Import Modules and Set Up Radio

    Start by importing the necessary modules. We'll need the microbit module for hardware access and the radio module for communication.

    Add the following complete code to your editor:

    from microbit import *
    import radio
    
    radio.on()
    radio.config(group=1)
    Run the code. Nothing visible will happen yet, but the radio is now active. This sets up the radio to send and receive messages. Configuring it to radio group 1 means that it will only broadcast and receive messages on this group. For example, if your friend has configured radio group 2, then you will not be able to send and receive message from each other.
    Avoid copy-pasting the code. Type each line to understand the logic and structure. This will help with exams and real coding tasks.

    3 - Define Messages List and Sender Name

    We'll create a list of predefined messages and a string for the sender's name. Lists are great for storing multiple items, and we'll use string manipulation later to prepend the sender's name to messages.

    Update your code to the following complete version:

    from microbit import *
    import radio
    
    radio.on()
    radio.config(group=1)
    
    sender_name = "Alice"  # Change this to your name
    messages = ["Hi", "How are you?", "Good morning", "Goodbye", "Help!"]
    Run the code. Still no visible output, but the list and name are defined. Try printing one message in the simulator if you want to test.
    The list 'messages' holds strings. We'll select from this list using buttons.

    4 - Select a Message

    Now, let's use the A button to select a message from the list. We'll use it to cycle through the messages in the list.

    Update your code to this complete version:

    from microbit import *
    import radio
    
    radio.on()
    radio.config(group=1)
    
    sender_name = "Alice"  # Change this to your name
    messages = ["Hi", "How are you?", "Good morning", "Goodbye", "Help!"]
    selected_index = -1
    
    while True:
        if button_a.was_pressed():
            selected_index = selected_index + 1 # next message
            if selected_index >= len(messages):  # If we go past the last message
                selected_index = 0  # Go back to the first message
            display.scroll(messages[selected_index], delay=100)
        
        sleep(100)
    Run the code. Press A to cycle through messages (they scroll on the display). 
    The while True loop runs continuously, checking for button A presses, incrementing the message index (and wrapping around if needed), and scrolling the selected message on the display.
    Why do you think we set selected_index to -1 initially?

    5 - Send a Message

    Now, let's use the B button to send the selected message. We'll also manipulate the string to add the sender's name.

    Update your code to this complete version:

    from microbit import *
    import radio
    
    radio.on()
    radio.config(group=1)
    
    sender_name = "Alice"  # Change this to your name
    messages = ["Hi", "How are you?", "Good morning", "Goodbye", "Help!"]
    selected_index = -1
    
    while True:
        if button_a.was_pressed():
            selected_index = selected_index + 1 # next message
            if selected_index >= len(messages):  # If we go past the last message
                selected_index = 0  # Go back to the first message
            display.scroll(messages[selected_index], delay=100)
        if button_b.was_pressed():
            message_to_send = sender_name + ": " + messages[selected_index]
            radio.send(message_to_send)
            display.show(Image.YES)
            sleep(500)
            display.clear()
        sleep(100)
    Run the code. Press A to cycle through messages (they scroll on the display). Press B to send the selected message; a tick appears briefly. If you have another Micro:bit running similar code, it can receive it.
    The if button_b.was_pressed() block checks if button B was pressed. If so, it constructs the message by concatenating the sender's name with the selected message, sends it using radio.send(), displays a confirmation image, pauses briefly, and clears the display.
    Note: You can see the sent and recived messages in the Radio segment below the virtual microbit.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more