Radio Communication - Wireless Messaging
Python ·What is Radio Communication? 📻
Think about walkie-talkies!
🎙️ One person talks → waves travel through the air → another person hears it
micro:bits can do the same thing!
📱 One micro:bit sends a message → 📡 Radio waves → 📱 Other micro:bits receive it
Just like texting, but without phones or WiFi!
Radio Groups 📻
What’s a radio group?
It’s like a channel or chat room number!
- Group 1: Sarah and Maya talking
- Group 2: Emma and Zoe talking
- Group 3: Alex and Jordan talking
Only micro:bits on the SAME group number can talk to each other!
Think: Different group chats on your phone 💬
Project: Wireless Messenger 📱
Let’s build a simple messaging system between micro:bits!
Step 1: Set Up Radio Group
First, we need to choose which “channel” to use.
radio.set_group(1)
This goes at the start of your program (not in a function).
The number can be 0-255 (we’ll use 1)
⚠️ Everyone in your group must use the SAME number!
Step 2: Send a Message (Button A)
When we press button A, let’s send a message!
def on_button_pressed_a():
radio.send_string("Hello!")
input.on_button_pressed(Button.A, on_button_pressed_a)
This sends “Hello!” to everyone on group 1! 📤
Step 3: Receive Messages 📥
Now we need to LISTEN for incoming messages!
def on_received_string(receivedString):
# What should we do with the message?
radio.on_received_string(on_received_string)
This function runs automatically when a message arrives!
Step 4: Display Received Messages
Let’s show the message on the LED screen!
def on_received_string(receivedString):
basic.show_string(receivedString)
radio.on_received_string(on_received_string)
The receivedString variable holds the message that was sent!
Complete Code! 🎉
radio.set_group(1)
def on_button_pressed_a():
radio.send_string("Hello!")
def on_received_string(receivedString):
basic.show_string(receivedString)
input.on_button_pressed(Button.A, on_button_pressed_a)
radio.on_received_string(on_received_string)
That’s it! Now let’s test it! 🚀
Step 5: Test in Simulator 🖥️
Let’s try it out!
- Click the simulator’s button A
- A second micro:bit will appear! 📱📱
- Press A again
- Watch the message appear on the second micro:bit!
The simulator shows how two micro:bits would communicate!
Time to Chat! 💬
Partner up and try it!
- Find a partner with a micro:bit
- Make sure you’re both on group 1
- One person press button A
- Watch the message appear on the other micro:bit!
- Try sending messages back and forth!
It’s like texting, but with micro:bits! 📱✨
Advanced Radio Features
Sending Numbers
You can send numbers instead of strings!
def on_button_pressed_a():
radio.send_number(42)
def on_received_number(receivedNumber):
basic.show_number(receivedNumber)
input.on_button_pressed(Button.A, on_button_pressed_a)
radio.on_received_number(on_received_number)
Sending Multiple Types
You can send different types of data:
radio.set_group(1)
def on_button_pressed_a():
radio.send_string("Hello!")
def on_button_pressed_b():
radio.send_number(100)
def on_received_string(receivedString):
basic.show_string(receivedString)
def on_received_number(receivedNumber):
basic.show_number(receivedNumber)
input.on_button_pressed(Button.A, on_button_pressed_a)
input.on_button_pressed(Button.B, on_button_pressed_b)
radio.on_received_string(on_received_string)
radio.on_received_number(on_received_number)
Changing Radio Groups
You can change groups during your program:
radio.set_group(1) # Start on group 1
def on_button_pressed_a():
radio.set_group(2) # Switch to group 2
basic.show_string("Group 2")
def on_button_pressed_b():
radio.set_group(1) # Switch back to group 1
basic.show_string("Group 1")
input.on_button_pressed(Button.A, on_button_pressed_a)
input.on_button_pressed(Button.B, on_button_pressed_b)
Challenges
Challenge 1: Custom Messages
Can you send different messages with different buttons?
- Button A: “Hello!”
- Button B: “How are you?”
- Button A+B: “Goodbye!”
Solution
radio.set_group(1)
def on_button_pressed_a():
radio.send_string("Hello!")
def on_button_pressed_b():
radio.send_string("How are you?")
def on_button_pressed_ab():
radio.send_string("Goodbye!")
def on_received_string(receivedString):
basic.show_string(receivedString)
input.on_button_pressed(Button.A, on_button_pressed_a)
input.on_button_pressed(Button.B, on_button_pressed_b)
input.on_button_pressed(Button.AB, on_button_pressed_ab)
radio.on_received_string(on_received_string)Challenge 2: Secret Code System
Create a secret code system! Send numbers that represent different messages:
- 1 = “Hello”
- 2 = “Goodbye”
- 3 = “Help!”
Solution
radio.set_group(1)
def on_button_pressed_a():
radio.send_number(1) # Hello
def on_button_pressed_b():
radio.send_number(2) # Goodbye
def on_button_pressed_ab():
radio.send_number(3) # Help!
def on_received_number(receivedNumber):
if receivedNumber == 1:
basic.show_string("Hello")
elif receivedNumber == 2:
basic.show_string("Goodbye")
elif receivedNumber == 3:
basic.show_string("Help!")
input.on_button_pressed(Button.A, on_button_pressed_a)
input.on_button_pressed(Button.B, on_button_pressed_b)
input.on_button_pressed(Button.AB, on_button_pressed_ab)
radio.on_received_number(on_received_number)Challenge 3: Multi-Player Game
Can you create a simple game using radio?
Ideas:
- Rock, Paper, Scissors (send your choice, receive opponent’s)
- Number guessing game
- Reaction time game
- Score sharing
Challenge 4: Remote Control
Make one micro:bit control another!
- Button A on controller: Move LED left
- Button B on controller: Move LED right
- Shake controller: Reset position
Radio Communication Tips 💡
- Same Group: Make sure all micro:bits you want to communicate are on the same group number
- Range: Radio works best within a few meters
- Interference: Too many micro:bits on the same group can cause interference
- Testing: Use the simulator first to test your code!
- Battery: Radio uses more battery, so keep that in mind
Common Issues and Solutions
Problem: Messages not received
Solutions:
- Check that both micro:bits are on the same group
- Make sure both have the radio code running
- Try moving closer together
- Check that the receiving function is set up correctly
Problem: Wrong messages received
Solutions:
- Make sure you’re using the right receive function (
on_received_stringvson_received_number) - Check that you’re on the right group (other groups might be sending messages too)
What We Learned Today
✓ What radio communication is and how it works
✓ How to set up radio groups
✓ How to send messages between micro:bits
✓ How to receive and display messages
✓ How to send different types of data (strings and numbers)
✓ How to build wireless messaging systems
Key Takeaways
- Radio groups are like channels - only micro:bits on the same group can communicate
- Use
radio.set_group(number)to set your group (0-255) - Use
radio.send_string()to send text messages - Use
radio.send_number()to send numbers - Use
radio.on_received_string()to receive text - Use
radio.on_received_number()to receive numbers - Radio communication lets micro:bits talk to each other wirelessly!
Next Steps
Try creating your own radio projects! Can you make:
- A remote control system?
- A multiplayer game?
- A secret messaging system?
- A collaborative art project?
The possibilities are endless! Radio communication opens up a whole new world of projects where multiple micro:bits can work together!
Project Ideas
1. Secret Messenger
Create a system where you can send secret coded messages to friends!
2. Multi-Player Rock Paper Scissors
Two micro:bits play Rock, Paper, Scissors and announce the winner!
3. Remote LED Controller
One micro:bit controls the LEDs on another micro:bit!
4. Collaborative Counter
Multiple micro:bits work together to count something!
5. Radio Tag Game
A game where micro:bits send signals to each other - like a digital version of tag!
Have fun experimenting with radio communication! 📻✨