Create an Extension for HaloCode in Three Minutes


This section uses the WS2812 extension as an example to describe how to create an extension for HaloCode in three minutes. It focuses on the key points and does not cover every detail. You can download the attached WS2812 extension to find more details.

Introduction to WS2812

Main features

● The control circuit and RGB chip are integrated in a 5050 package component to form a complete external control pixel.

● It is equipped with a built-in signal shaping circuit, shaping signals received by each pixel before outputting waveforms, and thus preventing line waveforms from distortion accumulation.

● It is equipped with built-in power-on and power-off reset circuits.

● The three primary colors of each pixel can achieve 256 levels of luminance, implementing full true color display of 16,777,216 colors with a scan frequency of no lower than 400Hz.

● The serial cascade interface enables the receiving and decoding of data through one signal line.

● Any two-point transmission within five meters does not require any additional circuit.

● At a refresh rate of 30 frames per second, the number of cascades in low-speed mode is no less than 512, and that in high-speed mode is no less than 1024.

● The maximum data transmission rate is 800 Kbps.

● It is cost-effective with highly consistent light color.


It can be applied to LED full-color light strings, full-color LED modules, LED Pixel screens, various electronic products, electronic equipment, and so on.


Preparation for Creating the WS2812 Extension

Goal: Create a HaloCode extension that drives WS2812

Knowledge points:

  1. The hardware core of the HaloCode is ESP32.
  2. The software core of the HaloCode is MicroPython.
  3. WS2812 are LED strips.

Keywords: MicroPython, NeoPixels, WS2812, mext

  1. MicroPython:

The "upload mode" of mBlock 5 is to convert the graphics blocks into Python statements and upload them to HaloCode for execution.

For more information about the syntax and ESP32, see the official MicroPython documentation


  • NeoPixels library: Used to drive WS2812.

  • WS2812 driving example program:

  • WS2812 manual:

  • mext

"mext" is the suffix of the extension file generated by the extension builder. The extension file demonstrated in this tutorial is attached at the end of this article.

To get started, drive WS2812 with Python code in mBlock 5. The Python Code is provided at the end of this article.

Steps for Creating the WS2812 Extension

Step 1: Create a new extension (first minute)

Choose My Plugin > My Extension, click Add extension, select Universal template in the dialog box that appears, and click OK.


The basic information area is displayed.

Step 2: Fill in the basic information


The information in the red rectangle boxes is mandatory.

Enter the basic information and click OK.

The homepage is displayed.

Step 3: Add categories and blocks

Click the editing icon of the extension.


The editing page is displayed.


Six setting tabs are provided. For the WS2812 extension, you need to set the basic information (which is set in step 2), blocks settings, and transcode settings.

Let's set the transcode first.

Set Transcoding language support to Python, and click Save.


Click the Blocks settings tab and click Add category.

The Extension category area is displayed.


Set the ID and category name, and click OK.

One minute is enough, isn't it?

Let's go to the next step.

Step 4: Design the Statement Block (second minute)

Click on the building block in the "building block list" to start configuring the building block details.

Click the icon at the bottom of the Blocks list area to add a block, and setting it in the Blocks settings area on the right.



To learn parameter setting, visit https://www.mBlock

For block types, refer to the block design standards at https://www.mBlock

Refer to the official blocks when designing blocks, so that other users can easily understand how to use the your blocks.

You can add all the blocks you need, and then set the code for them.

This is the second minute.

Let's go to the next step.

Step 5: Fill in transcode (third minute)

Click the Upload transcode tab in the Blocks settings area.


Let's start with the following block.


Click the Upload transcode tab.


You do not need to set the import, lib, and loop items.

Double-click the code area to edit it.

Enter the following code and click OK.



The following figure shows the mapping between Upload transcode and Python in mBlock 5. The reference variable syntax is {lednumber}.


The loop item is not set in this example.

This is the third munite, and the extension has been created.

Now, let's test the extension we have just created.

Verify the WS2812 Extension

Step 1: Download the extension and drag and drop it to mBlock 5 on your PC.

1. Click Download in the upper right corner.


The package box is displayed.

2. Wait for the packaging to complete, and click Download now.


3. Open mBlock 5, find the halo_ws2812.mext file, and drag and drop it to mBlock 5.


The installation is successful if the message indicating that WS2812 is successfully updated.


If you have dragged the same extension to mBlock 5 multiple times, wait until the message indicating successful update is displayed and close mBlock 5. Open mBlock 5 again to allow it to load the latest .mext file.


Step 2: Verify the blocks

Drag and drop blocks to the Scripts area.

Before drag the blocks, you need to:

(1) Set the mode to Upload, and drag an event block to the Scripts area first.

(2) Click the </> Button at the right sidebar to display the code interface.


Check the code generated by the blocks carefully.



You can copy the code and paste it to the Python editing interface. Upload the program to HaloCode, and you can see the error information returned by HaloCode.



Extension preview screenshot


Another Way to Learn Extension Block DesignIn addition to the step-by-step learning by following the tutorial, you can also find an extension, import it to the extension builder, and refer to it.

What you need is to import it to the extension builder.



After importing an extension, you can see in detail how it is designed.

For details about how to import an extension, see How to import extension into IDE (PC).

Download extension package

Download Extension Package

Python Code ready to Use

#generated by mBlock 5 for HaloCode
#codes make you happy

import time, machine, event, halo

from esp import neopixel_write

RED = (255, 0, 0)

YELLOW = (255, 255, 0)

GREEN = (0, 255, 0)

AQUA = (0, 255, 255)

BLUE = (0, 0, 255)

PURPLE = (148, 0, 211)

INDIGO = (75, 0, 130)

ORANGE = (255, 127, 0)



class NeoPixel:

ORDER = (1, 0, 2, 3)

def __init__(self, pin, n, bpp=3, timing=1): = pin

    self.n = n

    self.bpp = bpp

    self.buf = bytearray(n * bpp)

    self.timing = timing

def __setitem__(self, index, val):

    offset = index * self.bpp

    for i in range(self.bpp):

        self.buf[offset + self.ORDER[i]] = val[i]

def __getitem__(self, index):

    offset = index * self.bpp

    return tuple(self.buf[offset + self.ORDER[i]]

                 for i in range(self.bpp))

def fill(self, color):

    for i in range(self.n):

        self[i] = color

def fillone(self, ni, color):

    self[ni] = color

def write(self):

    neopixel_write(, self.buf, self.timing)

global neop

neop = NeoPixel(machine.Pin(23), 10)


def on_start():

for color in RAINBOWS:




Reference example

Create Extensions for micro:bit

results matching ""

    No results matching ""