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:
Import pybimport math from ws2812 import WS2812 Ring = WS2812 (Spi_bus=1, led_count=8, intensity=0.1) def Data_generator (led_count): data = [(0, 0, 0) for I in Range (led_count)] step = 0 while True: red = Int ((1 + math.sin (step * 0.1324)) * 127) green = Int (1 + math.sin (step * 0.1 654)) * 127) blue = Int ((1 + math.sin (step * 0.1)) * 127) data[step% led_count] = (red, green, Blue) C20/>yield Data Step + = 1 for data in Data_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:
ImportGCImportPybclassWS2812:"""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 I Ntensity (float up to 1)"""Self.led_count=Led_count self.intensity=intensity#Prepare SPI Data buffer (4 bytes for each color)Self.buf_length = Self.led_count * 3 * 4Self.buf=ByteArray (self.buf_length)#SPI InitSelf.spi = Pyb. SPI (Spi_bus, Pyb. Spi. MASTER, baudrate=3200000, polarity=0, phase=1) #Turn LEDs offself.show ([])defShow (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 ()defsend_buf (self):"""Send buffer over SPI. """self.spi.send (SELF.BUF) gc.collect ()defUpdate_buf (self, data, start=0):"""Fill 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= 0x03Index= Start * 12 forRed, green, blueinchdata:red= Int (Red *intensity) Green= Int (Green *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 >> 2 &Mask] Buf[index+7] = buf_bytes[red &Mask] Buf[index+8] = Buf_bytes[blue >> 6 &Mask] Buf[index+9] = Buf_bytes[blue >> 4 &Mask] Buf[index+10] = Buf_bytes[blue >> 2 &Mask] Buf[index+11] = buf_bytes[blue &Mask] Index+ = 12returnIndex//12deffill_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 LEDsBUF =Self.buf off=Self.buf_bytes[0] forIndexinchRange (END * 12, 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