In this article, I am going to cover how we can make two of our micro:bit radio signals communicate with each other using the radio signal of BBC micro: bit.What is Micro:Bit Radio?
Micro:Bit radio signals are used to communicate with one another or with many micro:bits. Generally, we can say that it is used for communication purposes.This provides the ability to send general purpose data packets from one micro:bit to another and to extend a message bus to span multiple micro: bits. So if we raise an event on one micro:bit, then another micro:bit can receive it. This component provides a very easy to use, flexible, broadcast radio channel. Anything we can send from one micro:bit, can be received by another micro:bit nearby. So let us see how we can do this.Tools You Need
- Micro:Bit (2 pcs)
- AA Battery (4 pcs)
- USB (1 Pcs)
We're going to use two micro:bits and make them communicate with each other via radio signals.We just need to power up both the micro:bits and upload the code for communication so there are no connection issues. In the code, we're going to use radio module for communication purposes. So go to makecode.microbit.org and create a new project. Go to Logic Block and choose If then Block then put it in the forever block.
Now go to Input Block and choose "button A pressed" and place it inside the if block.
Now go to Radio block and then choose radio send number and place it inside the block.
Now go to the basic block and choose pause then place it after the "radio send" block like this.
Let us see what is the purpose of the above code.
The above code will send number 1 via radio signal to another micro:bit when we press button A on the micro:bit. We need to create the group or channel for communication so that we can send the packet using that channel. And the micro:bit using that channel will receive the packet. So the above code is used for sending the radio signal 1 with the delay of 200 ms. This is the sending part now let us see what to do when the radio signal is received. Go to Radio and then choose "on radio received" block where receivedNumber is a variable which is used to store the number received by the micro:bit via radio signal.
Now go to the logic block and choose if-then block and place it inside the on radio received.Then go to the variable block and grab the variable receivedNumber then place it inside then the true block of if.
In any two-way electronics communications, we need to sed and receive acknowledgment so we're sending radio number 2 to another micro:bit so that we will know if our radio is sent and received successfully. I am just going to display a happy face or heart and add one more line to play the melody.
Now what if we receive radio 2? Then again I am going to play the melody. So if we send radio signal as 1 then another micro:bit will receive and it send an acknowledgement by sending radio signal 2. When number 2 is received then we will perform some actions.
Now download and copy this program to both the micro:bit. And you're good to go. Get the full code here: CODE
See Demo Here