Example Codefor micro:bit

Last revision 2026/01/31

This guide offers a detailed exploration of micro:bit programming using MakeCode, covering hardware connections, I2C communication, and display functions for a rich coding experience.

Hardware Preparation

Software Preparation

Wiring Diagram

When using the MakeCode programming platform, this product only supports I2C communication.

DFR0997-I2C wiring diagram

Sample Code

1. Background Color or Image Display

MakeCode programming

Set the background color of the display. Or display a custom image from the USB drive (fixed resolution 320×240). Enter the image name, for example, "fruit.png".

DFR0997-Makecode 1

Display effect:

DFR0997-Makecode result 1

2. Text Display

  1. Each display type has a corresponding number. For example, text, icon, widget, graphics, etc., each type can be assigned a number up to 255. Each number corresponds to the text, so each text can be deleted by specifying the number.
  2. Only English display is supported in MakeCode.

MakeCode programming

Display text, time, and date.

DFR0997-Makecode 2

Display effect:

DFR0997-Makecode result 2

3. Icon Display

Function Description: Display icons stored in the U-disk storage. The icons can be updated in terms of position and size.

The U-disk contains 200+ icons, including some animated icons and images with a resolution of 320x240, as shown below.

DFR0997-Icon

Here are some examples of exquisite icons:

sensor icon

DFR0997-sensor icon

agriculture icon

DFR0997-Agriculture icon

animal icon

DFR0997-Animal icon

botany icon

DFR0997-Botany icon

environment icon

DR0997-Environment icon

transport icon

DFR0997-Transport icon

There are also other categories of icons, such as those for expressions, food, music, digital graphics, people, safety, seasons, sports, and weather, which will not be displayed one by one. You can connect the module's Type-C interface to a computer, check the USB drive to make a selection. If you don't find a suitable one, you can download your own pictures to the USB drive for use.

  1. After plugging in the USB drive and selecting a picture, you need to power the display on again to ensure it displays properly.
  2. When calling an icon or picture, it is necessary to ensure that the names of the folder, icon, or picture in the code are consistent with those in the USB drive.
  3. Requirements for custom picture formats in the USB drive:
  • PNG format;
  • Background pictures should be adapted to a resolution of 320×240;
  • Icons should be adapted to a resolution of 64×64;
  • Animated pictures should be adapted to a resolution of 64×64.

MakeCode programming:

DFR0997-MakeCode 3

Display effect:

DFR0998-MakeCode result 3

4. Basic Graphics Display

MakeCode programming:

Display basic graphics such as lines, rectangles, circles, triangles, etc. on the display. The position, size, border color, fill color, and other parameters can be modified.

DFR0997-MakeCode 4

Display effect:

DFR0997-Makecode result 4

5. Widget Display

  • Bar Function: You need to create a progress bar widget first, and then set the value to be displayed on the progress bar. This widget can be modified in terms of position, size, and color, and can also be deleted.

    MakeCode programming:

    DFR0997-MakeCode 5

    Display effect:

    DFR0997-MakeCode result 5

  • Slider Function:

    The slider widget allows users to create the widget first, and then set the value to be displayed on the slider.

    This widget can be modified in terms of position, size, and color, and can also be deleted.

    MakeCode programming:

    DFR0997-MakeCode 6

    Display effect:

    DFR0997-MakeCode result 6

  • Compass Function: You need to create the compass widget first, and then set the value to be displayed on the compass pointer. This widget can be modified in terms of position, size, and can also be deleted.

    MakeCode programming:

    DFR0997-MakeCode 7

    Display effect:

    DFR0997-MakeCode result 7

  • Gauge Function: You need to create the gauge widget first, and then set the value to be displayed on the gauge. This widget can be modified in terms of position, size, starting and ending scale, base color, and pointer color, and can also be deleted.

    MakeCode programming:

    DFR0997-MakeCode 8

    Display effect:

    DFR0997-MakeCode result 8

  • Linear Gauge Function: You need to create the linear gauge widget first, and then set the value to be displayed on the linear gauge. This widget can be modified in terms of position, size, starting and ending scale, base color, and pointer color, and can also be deleted.

    MakeCode programming:

    DFR0997-MakeCode 9

    Display effect:

    DFR0997-MakeCode result 9

  • Chart Function:

    Step 1: You need to create the chart widget first, set the content of the horizontal and vertical axes, chart background color, and chart style (there are 3 types: 1-line shadow chart, 2-bar chart, 3-line shadow chart).

    Step 2: Create the color for the chart data series.

    Step 3: Add data to the chart data series, including data name, data group name, and the number of data points to be displayed.

    (The number of data points displayed should be set according to the screen size, as the space for displaying the horizontal axis content is limited)

    The colors and styles of the chart widget can be modified. The colors of the chart data series can also be modified. The chart can also be deleted.

    MakeCode programming:

    DFR0997-MakeCode 10

    Display effect:

    DFR0997-MakeCode result 10

Was this article helpful?

TOP