WS2813 RGB LED strip driver
WS2813 RGB LED strip driver
Hello. Is there any library / sample code on how to get WS2813 working with PyBoard? Thanks
-
- Posts: 363
- Joined: Sun Apr 17, 2016 1:55 pm
Re: WS2813 RGB LED strip driver
There is only SPI drive for WS2812/2813 now.
-
- Posts: 168
- Joined: Tue Nov 07, 2017 11:45 pm
Re: WS2813 RGB LED strip driver
have you got an example of SPI being used to light some neopixel?
Re: WS2813 RGB LED strip driver
I tested the code for WS2812 below with a Pycom device and a 24 Pixel ring. The SPI init/call might have to be adapted. It worked to some extend, but the timing is not very precise. There are gaps between the words when sending by SPI, and if these are too long, it is considered as reset. The data be compressed, so its only 9 bytes per WS2812 device.
Code: Select all
from machine import SPI, disable_irq, enable_irq
from time import sleep, sleep_us
def send_spi(data, spi):
no_pixel = len(data)
buffer = bytearray(no_pixel * 3 * 4)
index = 0
for pixel in data:
for byte in pixel:
bits = 0
mask = 0x80
while mask:
bits <<= 4
if byte & mask:
bits |= 0x0e
else:
bits |= 0x08
mask >>= 1
buffer[index] = (bits >> 24) & 0xff
buffer[index + 1] = (bits >> 16) & 0xff
buffer[index + 2] = (bits >> 8) & 0xff
buffer[index + 3] = bits & 0xff
index += 4
sleep_us(60) # ensure initial reset
state = disable_irq()
spi.write(buffer)
enable_irq(state)
data = 6 * [
(85, 0, 0), # green
(0, 85, 0), # red
(0, 0, 85), # blue
(85, 85, 85), # white
]
blank = 24 * [(0,0,0)]
spi = SPI(SPI.MASTER, baudrate=3200000, polarity = 0, firstbit = SPI.MSB, bits=32)
while True:
send_spi(data, spi)
sleep(1)
send_spi(blank, spi)
sleep(1)
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: WS2813 RGB LED strip driver
A simple work around can be to use APA102 LEDs instead as they have a clock line so don't care a bout precise timings. I have driven them in python on both RPi and MicroPython
Re: WS2813 RGB LED strip driver
Modified WS2812 driver, works for my WS2813 led strip from aliexpress
https://github.com/kamikaze/pyboard-exa ... /ws2813.py
https://github.com/kamikaze/pyboard-exa ... /ws2813.py