3.15 WS2812 RGB LED

3.15.1 Overview

WS2812 RGB LED is an external control LED integrating control circuit and light emitting circuit. It adopts single-line return-to-zero code communication, and supports 256 gray levels to display full-colors. The integrated chip inside each pixels efficiently stabilizes color output. So it is widely used in lighting, display and decoration.

3.15.2 Schematic Diagram

t120

From the Schematic Diagram, ws2812 connects and transmits data over a single wire, which is the communication method named single-bus return-to-zero code (single NZR). The data enters in serial through the DIN port, and each pixel receives and processes 24 bits data (R, G, B color channels with 8 bits each).

For detailed information of transmission mode, please refer the specification of ws2812.

3.15.3 Code Blocks

Blocks in :

  1. t121 is a block included in coding box library. It controls 12 RGB LEDs in the coding box, and the color of each can be controlled.

image-20250423143447807

  1. t122 is a block included in coding box library. It controls displayed colors of all RGB LEDs in the coding box.

  2. t123 is a block included in coding box library. It turns off all RGB LEDs in the coding box.

The code blocks in NeoPixel also control the display of RGB LED, and there are more ways for it. For details, please visit Libraries | MicroBlocks Wiki.

3.15.4 Test Code

You can manually build blocks, or directly open the code file we provide: 3-15-WS2812.ubp. If you have any questions about how to open code files or upload code, please back to 1.9 Upload Code.

Build code blocks:

  1. In , drag and to the script area, and stack them together.

t34

  1. In , drag t121 and place in . Set the first LED to show red, the second LED to show green, the third LED to show blue, the fourth LED to show yellow, the fifth LED to show cyan, the sixth light to show purple, and keep the remaining six ones unchanged.

  2. In , drag t4 and put it under t121.

  3. In , drag t123 and put it under t4 and add one more t4 at last.

Complete code:

t124

3.15.5 Test Result

Connect the coding box to the MicroBlocks via USB or Bluetooth, and click t59 to upload the code to the coding box. The first six RGB LEDs respectively light up in red, green, blue, yellow, cyan and purple, while the other six all light up in green. Each RGB LED turns on for 0.5s then off for another 0.5s.