Main Content

Maintain the Orientation of the Image on BBC micro:bit Using MATLAB Functions


The MATLAB® functions for BBC micro:bit enables you to communicate with BBC micro:bit from a host computer running MATLAB. Using MATLAB functions, you can collect data from sensors connected to BBC micro:bit.

In this example, you will learn how to use MATLAB functions to display the Happy Face image on the LED matrix on BBC micro:bit, and use the accelerometer data of the board to keep the orientation of the displayed image constant when the board is rotated along an axis.


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

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
         AvailableSPIBusIDs: 1
     Show all properties, functions, latest values

Step 3 - Display the Image and Use Accelerometer Data to Flip or Rotate Image

In this step, you will learn how to use the microbit object (created in Step 2) and MATLAB functions to display the Happy Face LED image on the BBC micro:bit, and also use the accelerometer data to sense the rotation of the board and then flip or rotate the image.

You can use the writeLED and readAcceleration functions for BBC micro:bit to display image and read the acceleration. Additionally, use the rot90, fliplr, and flipud functions in MATLAB to control the display of image.

writeLED(m, MicrobitImages.HappyFace);
x = MicrobitImages.HappyFace;
for i = 1:1000
    accel = readAcceleration(m);
    if accel(1) > 4.5 % state = 270
        value = rot90(x.Value);
        value = fliplr(value);
    elseif accel(1) < -4.5 % state = 90
        value = rot90(x.Value);
    elseif accel(2) > 4.5 % state = 180
        value = flipud(x.Value);
    else %accel(2) < -4.5 % state = 0
        value = x.Value;
    writeLED(m, value);


The MATLAB code compares the acceleration data with a constant (4.5) for the various states of orientation and performs the following actions:

  • The initial reference (state = 0) is the position where the board is kept vertical with the USB port facing upwards and the LED matrix facing you. The Happy Face LED image is displayed without any flip or rotation.

  • In the next state (state = 90), you rotate the board clockwise by 90 degrees, with the LED matrix still facing you. In this case, the image is rotated back by 90 degrees to maintain the same orientation as seen in state 0.

  • Similarly, in the next two states (state = 180 or 270), the image is flipped or rotated accordingly to keep the orientation constant as in state 0, when you rotate the board with the LED matrix still facing you.

See Also