First, the purpose of the experiment
Learn how WS2812B works
The driving method of learning ws2812b
Second, the experimental equipment
Tpyboard v102 1 Pieces
ws2812b Rgb-ring-8 A
Micro USB Data Cable 1 article
DuPont Line several
Iii. introduction of WS2812B
WS2812B is a set of control circuit and light-emitting circuit in one of the intelligent external control led light source. The outer type is the same as a 5050LED lamp bead, and each element is a single pixel point. The pixel interior contains an intelligent digital interface data latch signal shaping amplifier drive circuit, also contains a high-precision internal oscillator and programmable current control section, effectively guarantee the pixel light color height consistent.
The data protocol adopts single-line zero code communication mode, pixel point after the power-on reset, the DIN end accepts data transmitted from the Controller, first sent over the 24bit data is extracted from the first pixel point, sent to the data latch inside the pixel, The remaining data is amplified after the internal shaping processing circuit is enlarged by the do port to forward the output to the next cascaded Pixel point, each pass through a pixel point of transmission, the signal is reduced by 24bit. The pixel point adopts the automatic shaping and forwarding technology, so that the number of cascaded pixels is not limited by the signal transmission, only the speed of the limited signal is required.
Physical map
It's a 8-lamp bead.
Pin description for ws2812b:
Hardware connection
Connect the Tpyboard v102 to the ws2812b as follows:
The program source code is as follows:
ImportPybImportMath fromws2812ImportWS2812 Ring= WS2812 (Spi_bus=1, led_count=8, intensity=0.1) defData_generator (led_count): Data= [(0, 0, 0) forIinchrange (led_count)] Step=0 whiletrue:red= Int ((1 + math.sin (step * 0.1324)) * 127) Green= Int ((1 + math.sin (step * 0.1654)) * 127) Blue= Int ((1 + math.sin (step * 0.1)) * 127) Data[step% Led_count] =(red, green, blue)yieldData Step+ = 1 forDatainchData_generator (Ring.led_count): ring.show (data) Pyb.delay (100)
There is also a need to introduce a ws2812.py file. The contents are as follows:
Import Gcimport Pyb class WS2812: "" "Driver for WS2812 RGB LEDs. May is used for controlling single LED or chain of LEDs. Example of Use:chain = WS2812 (Spi_bus=1, led_count=4) data = [(255, 0, 0), # red (0, 255, 0), # Green (0, 0, 255), # Blue (in.), # White] Chain.sho W (data) version:1.0 "" "Buf_bytes = (0x11, 0x13, 0x31, 0x33) def __init__ (self, Spi_bus=1, led_count=1, Intensity=1): "" "Params: * spi_bus = SPI bus ID (1 or 2) * Led_count = Count of LEDs * Intensity = light intensity (float up to 1) "" "Self.led_count = led_count self.intensity = Inten sity # Prepare SPI data Buffer (4 bytes for each color) self.buf_length = Self.led_count * 3 * 4 Self.buf = ByteArray (self.buf_length) # SPI init self.spi = Pyb. SPI (Spi_bus, Pyb. Spi. MASTER, Baudrate=3200000, Polarity=0, phase=1) # turn LEDs off Self.show ([]) def show (Self, data): "" " Show RGB data on LEDs. Expected data = [(R, G, B), ...] where R, G and B is intensities of colors in range from 0 to 255. One RGB tuple for each LED. Count of tuples May is less than count of connected LEDs. "" "Self.fill_buf (data) self.send_buf () def send_buf (self):" "," "" Send buffer over SPI. "" "Self.spi.send (SELF.BUF) gc.collect () def update_buf (self, data, start=0):" "" Fi ll a part of the buffer with RGB data. Order of colors in buffer are changed from RGB to GRB because WS2812 LED have GRB order of colors. Each color was represented by 4 bytes in buffer (1 bytes for each 2 bits). Returns the index of the first unfilled LED note:if you find this function ugly, it's because speed Optimisa tions beated Purity of Code. "" "buf = self.buf buf_bytes = self.buf_bytes intensity = Self.intensity Mask = 0x03 index = start * for red, green, blue in data:red = Int (red * intensity) green = INT (GRE En * intensity) blue = Int (blue * intensity) buf[index] = Buf_bytes[green >> 6 & Mask] BUF[INDEX+1] = Buf_bytes[green >> 4 & mask] buf[index+2] = Buf_bytes[green >> 2 & Mask] buf[index+3] = buf_bytes[green & mask] buf[index+4] = buf_bytes[red >> 6 & mask] buf[index+5] = buf_bytes[red >> 4 & mask] buf[index+6] = buf_bytes[red & Gt;> 2 & mask] buf[index+7] = buf_bytes[red & mask] buf[index+8] = buf_bytes[blue & Gt;> 6 & mask] buf[index+9] = Buf_bytes[blue >> 4 & mask] buf[index+10] = Buf_byt Es[blue>> 2 & mask] buf[index+11] = buf_bytes[blue & Mask] Index + = return in Dex//Def fill_buf (self, data): "" "Fill buffer with RGB data. All LEDs after the data is turned off. "" "End = Self.update_buf (data) # Turn off the rest of the LEDs buf = self.buf off = Self.bu F_bytes[0] for index in range (end *, self.buf_length): buf[index] = off index + = 1
This reference is a project on GitHub. Project Address:
https://github.com/JanBednarik/micropython-ws2812
Let's show you the effect (the last light bead is broken, you can ignore it by yourself ... )
Https://v.qq.com/x/page/d05297wxo1b.html
Micropython example of Tpyboard v102 colorful lantern ws2812b