Index>BBC Micro:bit>Adeept Sensor Kit for BBC Micro:bit>Lesson 29 How to control WS2812 RGB ring
No related product information!

Lesson 29 How to control WS2812 RGB ring

2480

   In this lesson, we will carry out an interesting experiment by programming on the Micro:bit so as to control WS2812 RGB ring.

29.1 Components to be prepared

image.png


29.2 The introduction of the WS2812 RGB ring 

29.2.1 The WS2812 RGB ring

WS2812 RGB ring is an intelligent external control LED light source integrating control circuit and light-emitting circuit. Its shape is the same as a 5050 LED bead, and each component is a pixel. The pixel includes the intelligent interface, data latch signal, shaping and amplifying driving circuit and the anti-reverse circuit. It also includes a high-precision internal oscillating circuit and a high-precision constant current control module, which effectively ensures the high consistence of the color of the pixel. And it has a unique breakpoint retransmission function, and exerts two-way signal transmission. The overall color display will not be affected even with single pixel damage.

The data protocol adopts the single NZR communication mode. After the power-on reset, the DIN terminal accepts the data transmitted from the controller. The first 24-bit data is extracted by the first pixel and sent to the data latch in the internal of the pixel. The remaining data is shaped and amplified by the internal shaping processing circuit and then transferred and output to the next cascaded pixel through the DO port. And the signal is reduced by 24 bits every time a pixel is transmitted. The pixel adopts automatic shaping and forwarding technology, so that the number of cascaded pixels is not limited by signal transmission, and only requires signal transmission speed.

After receiving the data and reduced by 24bit, the DIN terminal compares with the DIN data. If there is no signal at the DIN terminal, the DIN terminal receives the signal and loops to the BIN terminal to accept the input signal, ensuring that the damage of one of the ICs does not affect the cascaded transmission and the control IC remains in the BIN port acceptance state until the next power-on to reconfirm after power-off.

Function of the pins

image.png

image.png


LED characteristic parameter

image.png


    LED pin

  The LED circle we use in this experiment is as follow:

image.png 


image.png

29.3 Low level and high level


In circuit, the form of binary (0 and 1) is presented as low level and high level.

Low level is generally equal to ground voltage (0V). High level is generally equal to the operating voltage of components.

The low level of Micro:bit is 0V and high level is 3.3V, as shown below. When IO port on Micro:bit outputs high level, low-power components can be directly driven,like LED.

image.png


29.4 Circuit

    You should connect the components according to the following circuit diagram, and view the pictures with the function “Zoom in”:


image.png


29.5 MakeCode programming

Next, we will use the online MakeCode Editor to complete the experiment in this lesson.


29.5.1 Start programming

(1) Log in to the website

1. You need to enter the URL in the address bar of Google Browser:

https://makecode.microbit.org/

2. After the website is successfully opened, the interface as shown below will appear:

image.png



(2) Import a project

1. In the HOME interface, click the "Import" button to import the external ".hex" file:

image.png


In the pop-up dialog box, select the "Import File", as shown in the following figure:

image.png



Click the "Choose File"

image.png


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_29\BlockCode

Select the file in ".hex" format and click the Open:

image.png


2. Notice whether the file has been loaded into the following window, and then click the "Go ahead!" button, as shown in the following figure:

image.png


3.You can see the following interface when successfully opening the file:

image.png


29.5.2 Run the program

1. After the program is written, connect micro:bit and PC with a Micro USB cable.

2. After micro:bit is connected to the computer, you need to first "Pair device". Click the image.png button on the right of image.png in the lower left corner, and then click the image.png option, as shown in the following figure:

image.png


Then click image.png in the lower right corner

image.png


Then the following dialog box will pop up, select image.png, and then click image.png

image.png


After the device is successfully paired, the image.png button changes to image.png

image.png


3. Start to download the program to Micro:bit, and click the image.png button. Generally, the program will be downloaded directly to the Micro:bit. After the download is completed, your Micro:bit will restart and run the program just downloaded. The WS2812 RGB ring will light up red, green and blue, as shown below:

image.png

[Note]

    1.If no experimental phenomenon has been detected after clicking the button image.png, you need to click the image.png button on the right of the image.png, and then click the image.png , and observe the WS2812 RGB ring again, as shown in the following figure:

image.png

2. If no experimental phenomenon is observed, you should check whether the USB cable of Micro:bit is successfully connected. If not, please download the program again.


  If you have problems, please send us an email: support@adeept.com


29.5.3 Learn the code program

Controling the WS2812 RGB ring will light up red, green, blue.

image.png

Note

If you want to import the “Neopixel” expansion block in a new project, follow the steps below to add it.

(1)Click the button【Extensions】:

image.png 


(2)Enter “Neopixel”and click search.

 

image.png 


(3)Select Select "Neopixel":

image.png 


(4) Added successfully:

image.png


29.6 Python programming

29.6.1 Run the program

1.Connect micro:bit and PC with a Micro USB cable.

2. Open the Mu Editor installed on the computer, and click the button [Load] in the upper left corner to open the source code program of this lesson:

image.png 


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_29\PythonCode

Select the file in ".py" format and click the Open:


image.png 


3.Click the button Flashand download the program to Micro:bit, as shown below:


image.png


4.When the download is successful, the WS2812 RGB ring will light up red, green and blue, as shown below:


image.png


    If you have problems, please send us an email: support@adeept.com


29.6.2 Learn the code program


(1)Import the neopixel package and define the RGB value of the color to be displayed.


10

11

12

13

14

15

16

17

18

import neopixel

 

# define pin8 control 12 Neopixel

npix = neopixel.NeoPixel(pin8,12)

 

# define color

red = (255, 0, 0)

green = (0, 255, 0)

blue = (0, 0, 255)


(2)Control the WS2812 RGB ring to display red, green and blue cyclically.


27

28

29

30

31

32

33

while True:

    LightAll(red)

    sleep(1000)

    LightAll(green)

    sleep(1000)

    LightAll(blue)

    sleep(1000)