This example shows you how to use Simulink® models for wireless communication and control between two BBC micro:bit boards.
The Simulink® Coder Support Package for BBC micro:bit provides Radio blocks that help you to use wireless radio communication over a dedicated channel between two or more BBC micro:bit boards.
To view the Radio blocks:
1. Enter slLibraryBrowser at the MATLAB® prompt.
2. In the Simulink Library Browser, navigate to Simulink Coder Support Package for BBC Micro:bit and double-click Radio blocks.
You can see the Radio Receive block and Radio Transmit block.
The Radio Transmit block is used to send data from a BBC micro:bit. If one BBC micro:bit is sending data, any number of BBC micro:bit boards in its vicinity can receive the data. In this case, the sender is like one person speaking, each receiver is like one of many people listening.
If more than one board is sending data at the same time in the same channel, the sent data is corrupted and the receiver receives improper data. In this case, the senders are like two people talking at the same time. The receiver is like a listener trying to follow more than one speaker at the same time.
The problem with corrupted data from multiple senders can be avoided by using different channels.
The Radio blocks in BBC micro:bit can operate at different channels, numbered from 1 to 100. Each channel works at a different frequency. If a few BBC micro:bit boards are sending and receiving data using one channel, the BBC micro:bit boards operating at other channels cannot receive the data even if they are physically close to the sending (or transmitting) BBC micro:bit.
A channel can be visualized as a closed room. The speaker and listeners are in the closed room. The speaker in one room cannot be heard from the other rooms. In one room, any number of listeners can be present. All of them can hear the speaker in that room.
In this example, you will learn about two Simulink models that are designed for wireless communication and control using the Radio blocks:
The Radio Transmit Simulink model, when deployed on the first BBC micro:bit (Tx), tracks the tilt of the board along the x-axis using the accelerometer sensor. The board then transmits this data over a radio channel.
The Radio Receive Simulink model, when deployed on the second BBC micro:bit (Rx), receives the data from the transmitting BBC micro:bit. The receiving board then displays a left or right arrow on the LED matrix to indicate the tilt direction of the Tx BBC micro:bit.
If you are new to Simulink, complete the Interactive Simulink Tutorial.
If you are new to Simulink Coder, visit the Simulink Coder product page for an overview and tutorials.
We also recommend completing Getting Started with Simulink Coder Support Package for BBC micro:bit.
To run this example you will need the following hardware:
Two BBC micro:bit boards
Battery pack (the one given with the BBC micro:bit board)
USB type A to Micro-B cable
1. Connect the Tx BBC micro:bit to the host computer. For details about the USB connection between BBC micro:bit and host computer, refer to Getting Started with Simulink Coder Support Package for BBC micro:bit.
2. Open the Radio Transmit model:
3. On the toolbar of the Simulink model window, click the Deploy to hardware icon.
4. After the code deployment is successful, unplug this Tx BBC micro:bit from USB, and connect the battery pack to it. This board continues to function as the transmitter.
5. Connect the Rx BBC micro:bit to the host computer.
6. Open the Radio Receive model:
7. In the Simulink window, click the Deploy to hardware icon.
8. Tilt the Tx BBC micro:bit along the x-axis.
You can observe that the arrow on the Rx BBC micro:bit changes direction based on the direction of the tilt of the Tx BBC micro:bit.
Radio Transmit Model
In the Radio Transmit model, the tilt of the BBC micro:bit is obtained by reading the Accelerometer block's X output. The X output is directly connected to the Radio Transmit block and the channel number is set to a particular value. The Radio Transmit block transmits the accelerometer data to any BBC micro:bit that is receiving data in the same channel.
Radio Receive Model:
In the Radio Receive model, the Radio Receive block receives the data from the transmiting BBC micro:bit. Ensure that the Radio Receive block is configured for the same channel as the Radio Transmit block. Additionally, the Radio Receive block is also configured with Data length as
1 (for interpreting data of one axis only) and the Data type as
double. The 5x5 LED Matrix block is used to display the arrow (Point Left block and Point Right block) based on the output from a Switch block.
Implement the same logic for y-axis. Tip: Use Y axis data from accelerometer sensor on Tx BBC micro:bit and use the Point Up and Point Down blocks in Rx BBC micro:bit.
Create a system with few BBC micro:bits, in which only one BBC micro:bit switches ON at once, based on radio communication
Tip: Use the Fire Fly Queen Model on one BBC micro:bit, which assigns unique IDs to multiple BBC micro:bits, and later broadcasts a random ID. Use the Fire Fly Slave Model on each of the remaining BBC micro:bits that flashes an LED when it either receives an ID or responds to the broadcast from the Queen BBC micro:bit.
Copyright 2017 The MathWorks, Inc.