🛠 Communication entre deux micro:bit avec le module radio
Objectives
Make two micro:bit V2 boards communicate wirelessly using the MicroPython radio module, which leverages the 2.4 GHz BLE radio of the nRF52833 (SoftDevice S113). You will send encrypted messages between multiple boards using groups.
Hardware
- 2 micro:bit V2 boards (or more)
- 1 micro-USB cable to flash each board
- No router or pairing required: everything is built-in
Complete Code
# Émetteur (carte 1)
from microbit import *
import radio
radio.on
radio.config(group=23, channel=40, power=6)
while True:
if button_a.was_pressed:
radio.send("SOS")
display.show(Image.HAPPY)
if button_b.was_pressed:
radio.send("COUCOU")
display.show(Image.SILLY)
sleep(50)
# Récepteur (carte 2)
from microbit import *
import radio
radio.on
radio.config(group=23, channel=40, power=6)
while True:
msg = radio.receive
if msg == "SOS":
display.show(Image.SAD)
sleep(1000)
elif msg == "COUCOU":
display.show(Image.HEART)
sleep(1000)
display.clear
sleep(50)
group filters recipients, channel (0-83) sets the 2.4 GHz channel, and power (0-7) sets the transmission power. Messages are small frames; the module is designed for games and multi-board networks (see the official "Fireflies" tutorial).
Expected Verification
Pressing A on board 1: board 2 displays a sad face; pressing B: a heart. Two boards flashed with the same code react to each other from several meters away.
Common Errors
- Forgetting
radio.on:senddoes nothing silently. - Different groups or channels between sender and receiver: no reception.
- Messages that are too long or binary data not supported: stick to short strings.
- Wi-Fi interference on the chosen channel: change
channelif frames are lost.
Other tutorials for this board
Code examples for this board
coeur.pyMicroPythonboutons.pyMicroPythonpodometre.pyMicroPythonlumiere.pyMicroPythonmusique.pyMicroPythonradio_ping.pyMicroPython