Main Content

Getting Started with Simulink Coder Support Package for BBC micro:bit

This example shows you how to use Simulink® Coder™ Support Package for BBC micro:bit to run Simulink® model on a BBC micro:bit.

Introduction

Simulink Coder Support Package for BBC micro:bit enables you to create and run Simulink models on a BBC micro:bit. The support package includes a library of Simulink blocks for configuring and accessing BBC micro:bit peripherals and communication interfaces.

In this example you will learn how to configure a simple Simulink model to generate code for BBC micro:bit and run the generated code on the board.

Prerequisites

Required Hardware

To run this example you will need the following hardware:

  • BBC micro:bit

  • USB type A to Mini-B cable

Task 1 - Connect a BBC micro:bit to Host Machine

In this task, you will connect BBC micro:bit to host machine.

1. Plug in a Mini USB cable from a USB port on the host machine to the USB connector on the BBC micro:bit. The BBC micro:bit comes with the mass-storage device (MSD) Flash Programmer OpenSDA Application preinstalled.

2. Before using the BBC micro:bit, we recommend installing mbed Serial Driver Download and Install mbed Windows serial port driver

It will appear as a removable storage drive with a volume label of BBC micro:bit.

Task 2 - Review BBC micro:bit Support Package Block Library

1. Enter Simulink Browser at the MATLAB® prompt. This opens the Simulink Library Browser.

2. In the Simulink Library Browser, navigate to Simulink Coder Support Package for BBC micro:bit.

3. Go to the LED Library, double-click the 5x5 LED Matrix block. Review the block mask, which contains a description of the block and block inputs the associated 5x5 LED Matrix.

Task 3 - Create a Model for BBC micro:bit

In this task, you will create a simple Simulink model that sets the image on the 5x5 LED Matrix.

1. In MATLAB, select HOME > New > Simulink Model. Under Simulink, select Blank Model > Create Model.

2. Drag the 5x5 LED Matrix block from BBC micro:bit library to the model.

3. Drag the Constant block from the Simulink Sources library to your model. Set the value of Constant block to any 5x5 Matrix to any pattern. For example, for letter "X"pattern, set value to [ 1 0 0 0 1;0 1 0 1 0;0 0 1 0 0;0 1 0 1 0;1 0 0 0 1]

4. Connect the Constant block to the input of 5x5 LED Matrix block.

Task 4 - Configure and Run the Model on BBC micro:bit

In this task, you will configure and run your model on the BBC micro:bit.

1. Connect the BBC micro:bit to your computer with a USB cable.

2. In your Simulink model, go to the Modeling tab, and click Model Settings.

3. When the Configuration Parameters page opens up, navigate to Hardware Implementation pane.

  • Set the Hardware board parameter to BBC micro:bit.

  • On the Target Hardware Resources pane, Set the Build action to Build, load and run to automatically download the generated binary file on to the connected BBC micro:bit.

4. Navigate to Solver pane and set the Solver to discrete (no continuous states).

5. Click OK.

6. In your Simulink model, go to the Hardware tab and click Build, Deploy & Start. The model will now be deployed to the BBC micro:bit.

7. Observe that the input pattern is displayed in the 5x5 LED Matrix.

8. Save your model.

A bbcmicrobit_gettingstarted pre-configured model is included for your convenience.

Other Things to Try with RGB LED Block

For example:

  • Create and run a model that displays different Smiley patterns.

  • Create and run a model that can blink the pattern (use other Simulink blocks also).

Summary

This example introduced the workflow for creating an algorithm from a Simulink model and then running it on BBC micro:bit. In this example you learned that:

  • Simulink Coder Support Package for BBC micro:bit provides blocks for configuring, reading from and writing to BBC micro:bit peripherals.