The MATLAB® functions for BBC micro:bit enables you to communicate with BBC micro:bit from a host computer running MATLAB. The MATLAB command line interface can be used to access BBC micro:bit's I/O peripherals and communication interfaces. Using MATLAB functions, you can collect data from sensors connected to BBC micro:bit and actuate devices attached to BBC micro:bit.
In this example, you will learn how to create a
to connect to a BBC micro:bit from within MATLAB. You examine the properties of this
object and learn how to use it to glow or blink an LED on the BBC micro:bit.
If you are new to MATLAB, it is helpful to read Get Started with MATLAB.
To run this example, you need the following hardware:
USB type A to Micro-B cable
To connect BBC micro:bit to host computer, refer to Task 1 (Connect an BBC micro:bit to host machine) explained in Getting Started with Simulink Coder Support Package for BBC micro:bit.
In this step, you will create a microbit object in MATLAB, which establishes the communication between the host computer and BBC micro:bit. Create a microbit object,m, from the MATLAB Command Window.
m = microbit
microbit with properties: Port: "COM16" AvailableDigitalPins: ["P0-P16"] AvailablePWMPins: ["P0-P10", "P12-P16"] AvailableAnalogPins: ["P0-P4", "P10"] AvailableI2CBusIDs: 1 AvailableSPIBusIDs: 1 Show all properties, functions, latest values
In this step, you will learn how to use the microbit object (created in Step 2) and MATLAB functions to glow or blink an LED on BBC micro:bit.
BBC micro:bit has a 5x5 LED Matrix. Each LED can be accessed from MATLAB by
defining either its x and y coordinates or its pixel number. You can specify these
writeLED function to glow an LED, and also include the
function in a
for loop to blink the LED.
% Glow the top left LED by defining its pixel number. writeLED(m, 1); % Glow the bottom right LED by defining its pixel number. writeLED(m, 25); % Clear the LED matrix. clearLED(m); % Blink the LED at the position [3,3] ten times. for i = 1:10 writeLED(m, [3,3]); pause(0.5); clearLED(m); pause(0.5); end
If the connection between host computer and BBC micro:bit is no longer needed, clear the microbit object.