Main Content

Glow or Blink LED on BBC micro:bit Using MATLAB Functions

Introduction

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 microbit object 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.

Prerequisites

If you are new to MATLAB, it is helpful to read Get Started with MATLAB.

Required Hardware

To run this example, you need the following hardware:

  • BBC micro:bit

  • USB type A to Micro-B cable

Step 1 - Connect a BBC micro:bit to Host Computer

To connect BBC micro:bit to the host computer, refer to Task 1 (Connect an BBC micro:bit to host machine) explained in .

Step 2 - Create a microbit Object to Communicate with 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
     Show all properties, functions, latest values

Step 3 - Glow or Blink an LED

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 in the 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
    

Step 4 - Disconnect the microbit Object

If the connection between host computer and BBC micro:bit is no longer needed, clear the microbit object.

clear m;

See Also

|