Labs / Bipes + MicroPython / 8 Actuators (Outputs / Actuators)

8.7 Want to create colorful pixel art? Light up a cool LED matrix! β€”β€”WS2812 Matrix Module

Author: Li Zisheng Β· Source: FreakStudio open documentation Β· License: CC BY 4.0

Contents

Young makers, today let's unlock a "magic screen that can conjure up rainbows" 🌈!

It is composed of tiny WS2812 RGB LED beads, each of which can independently emit red, green and blue light. When combined together, they can display rainbows, smileys, text and even animations, just like a small electronic canvas, making it extremely suitable for creative interactive projects ✨!

1. Module Introduction

Ordinary LEDs can only emit one color, while the WS2812 matrix module is a smart full-color LED matrix: a micro driver chip is embedded in each LED bead, and with only one data line, the development board can independently control the color and brightness of each individual LED bead, enabling the entire matrix to display rich patterns and dynamic effects.

Compared with ordinary LED modules, it has the following characteristics:

In graphical programming, these blocks are commonly used for WS2812 matrices:

In practical projects, it is often used for the following purposes:

Quick Science: What is a WS2812 LED bead?

WS2812 is a type of "RGB LED with built-in driver". Each LED bead contains a tiny chip that can store the color commands sent by the development board. Using only a single data line, the development board can transmit color data to each LED bead in sequence, allowing each of them to display a distinct color β€” this is the secret behind the matrix's ability to show complex patterns!

2. Wiring Instructions

To make the matrix "light up", the first step is still β€”β€” connect the wires correctly!

WS2812 6 7 64 DIO0

We connect the WS2812 matrix module to the digital interface DIO0 of the development board, and the corresponding relationship is as follows:

Development Board Pins

Module Pins

Description

Pin 6

DIN

Communication signal output interface on MCU

Pin 7

NC

-

πŸ“Œ Note

3. Example: Chasing LED light effect (turning on red LEDs one by one)

Experiment Objective: Make the 64 LED beads in the matrix light up in red sequentially from the 0th to the 63rd one, meanwhile print the serial number of the currently lit bead on the terminal to create the dynamic effect of a "running light".

Everyday Thought: The string lights used during festivals produce a similar chasing light effect, so let's use a WS2812 matrix to create an even cooler "full-color chasing LED light"!

The graphical program is as follows:

WS2812 27/GPIO21 64 0 1 63 0.1

Building Block Assembly Steps:

  1. Initialize the matrix
    • Drag in Init NeoPixel block;
    • Set the Pin to Pin 27 / GPIO21, and set the Number of LEDs to 64 (consistent with the number of matrix LED beads).
  2. Cycle-controlled lamp beads
    • Drag in the count with i from 0 to 64 by 1 block;
    • Let the variable i range from 0 to 63, corresponding to the serial number of each lamp bead one by one.
  3. Print the serial number of the LED bead
    • Add the print i block inside the loop to display the number of the currently lit LED in the terminal.
  4. Set the LED bead color
    • Drag in the Control NeoPixel block;
    • Set LED to i (the serial number of the lamp bead in the current loop), and set Color to red;
  5. Refresh Display
    • Add the Write NeoPixel block to send color data to the matrix and turn on the LEDs;
  6. Control animation speed
    • Drag in the delay block and set it to 0.1 seconds β†’ to make the chaser LED movement smoother.

Experimental Effect:

5307984e87b74b21128d522d35748c02.mp4 641 KB

Graphical blocks will be automatically converted into MicroPython code, and the running results will be displayed:

WS2812 21 64 0 255 0.1 22

Directly importable building blocks:

LED Board.xml 3 KB

Tips

  1. If the LED bead does not light up:
    • Check whether the pins are selected correctly and the wiring is secure;
    • Confirm that the number of initialized LED beads is consistent with the actual number of the matrix (e. g., 64 beads);
    • Check whether the module power supply is normal to avoid the lamp beads not lighting up due to insufficient voltage.
  2. What should I do if there is a color display error?
    • Check Control NeoPixel for the color settings; the RGB value ranges from 0 to 255.
    • Ensure that the Write NeoPixel block is executed after setting the color, otherwise the color will not refresh.
  3. Advanced Gameplay:
    • Change the color to green or blue, and check the chaser light effect;
    • Implement a "rainbow gradient flowing water light" with nested loops to make the color of each lamp bead change sequentially;
    • Combined with sensors, such as a touch key trigger, it can display a smiling face pattern on the matrix.

✨ Interactive Mini Quiz

  1. Think about it: Why does the WS2812 matrix only need one data line to control all the LED beads? How many lines would be required if a regular LED matrix is used?
  2. Try it: Change the delay time from 0.1 second to 0.5 second, and see whether the speed of the flowing LED lights will become slower or faster? Then change it to 0.05 second to experience different animation rhythms.

Summary

  1. The WS2812 matrix module is an intelligent full-color LED display module, with each LED bead capable of independent color control;
  2. It communicates via a single bus, features simple wiring, and supports complex patterns and animations;
  3. The basic usage is to realize a flowing-light effect through "loop + single LED control", while the advanced usages include displaying patterns, rainbow gradient, etc. ;
  4. It is an excellent module for learning full-color LED control and creative display, which can make your project full of visual impact.