According to StarFive, developers have successfully used VisionFive/VisionFive 2 to control colorful light ring, and the specific instructions are as follows:
1. Preparation
- Development board: VisionFive/VisionFive 2 SBC
- Colorful light ring: WS2812B light ring with 24 Beads
- DuPont Line: Many
2. The principal of using the light ring
On the interface of the lamp ring, in addition to VCC (+) and GND (-), there are also DIN for outputting control signals, and DOUT for transmitting signals to subsequent lamp beads.
All WS2812B based light rings, dot matrices, etc. have the same principle of use and are connected one by one:
WS2812B controls a light bead requires 24 bits of data, representing the three color values of GRB:
To control multiple light beads, continuously send multiple sets of 24bit data as mentioned above:
Of course, sending the above data requires packaging and sending according to certain specifications. The specific packaging rules can be found in WS2812B version of Stargate 3.
In this demo, we use the SPI interface to send the control data required for WS2812B, thereby achieving the colorful light ring control of 24 light beads.
3. Connection
Firstly, refer to the following figure and connect the colorful light ring to VisionFive:
Connection:
Note:
The power supply voltage should be determined based on the actual sensor used. The module in this demo uses a 5V power supply voltage.
5. Coding
Then, we write the following program:
# -*- coding: utf-8 -*-
# file: ~/projects/spi/ws2812b_test.py
import time
from lib import neopixel_spidev as np
from lib.pixelbuf import wheel
with np.NeoPixelSpiDev(1, 0, n=24, pixel_order=np.GRB) as pixels:
try:
while True:
print("整圈颜色变化效果:")
pixels.fill((0,0,0))
time.sleep(1)
for i in range(0,255,2):
print(" %d" % i, end="",flush=True)
pixels.fill(wheel(i))
time.sleep(0.01)
print("")
time.sleep(1)
pixels.fill((0,0,0))
time.sleep(1)
print("依次点亮效果:")
for i in range(pixels.n):
print(" %d" % i, end="", flush=True)
if i>0:
pixels[i-1] = (0,0,0)
pixels[i] = wheel(128)
time.sleep(0.1)
print("")
time.sleep(1)
pixels.fill((0,0,0))
time.sleep(1)
print("数量变化效果:")
colors = (
(0,0,0),
(128,0,0),
(0,128,0),
(0,0,128),
(128,128,0),
(128,0,128),
(0,128,128),
(128,128,128)
)
for n in (1,2,3,1,1,2,3,1,3,4,5,3,4,5,5,6,5,4,3,1,5,6,5,4,3,1,1,5,1,1,5,1):
print("n=%d" % n, end="", flush=True)
pixels.fill((0,0,0))
time.sleep(0.1)
for i in range(n*2):
print(" %d" % i, end="", flush=True)
pixels[i] = colors[n]
time.sleep(0.4)
print("")
time.sleep(1)
pixels.fill((0,0,0))
time.sleep(1)
time.sleep(5);
except KeyboardInterrupt:
pass
Sending the control signal of WS2812B requires specific structured data, and we can assemble it ourselves after understanding its principle. In the above code, the py-neopixel-spidev extension library is used, which outputs control signals to WS2812B through the SPI interface of the board.
In the code, a total of three effects were demonstrated:
- The color change of the entire dazzling light ring, from (0,0,0) to (255255255)
- Single light bead illuminated at once
- According to the simplified notation of Two Tigers, a certain number of light beads are lit each time, and the corresponding colors are selected from the colors based on the serial number of the simplified notation
5. Operation result
To run the above program, you also need to install the corresponding StarFive GPIO support library, as follows:
# Install extension library
git clone https://github.com/fschrempf/py-neopixel-spidev.git
cd py-neopixel-spidev
python setup.py build
sudo python setup.py install
After writing, run ws2812b_test.py, and you can see the operation result of the light.
sudo python3 ws2812b_test.py
The following is the output:
The actual effect is as follows:
- The color of the entire light ring changes:
- Single light bead illuminated at once:
- Illuminate a specified number of light beads at once:
6. Summary
In this demo, we learned to control the WS2812B colorful light ring.
WS2812B is used in many applications, such as city night view, Laser lighting display, advertising cabinet, jewelry display cabinet, toys, etc.
On Valentine’s Day or May 20th, you can also create a heart with brilliant effects and give it to your beloved.