Page 1 of 1

WS2813 RGB LED strip driver

Posted: Wed Dec 05, 2018 12:18 am
by kamikaze
Hello. Is there any library / sample code on how to get WS2813 working with PyBoard? Thanks

Re: WS2813 RGB LED strip driver

Posted: Wed Dec 05, 2018 2:40 am
by shaoziyang
There is only SPI drive for WS2812/2813 now.

Re: WS2813 RGB LED strip driver

Posted: Fri Dec 14, 2018 4:38 am
by rhubarbdog
have you got an example of SPI being used to light some neopixel?

Re: WS2813 RGB LED strip driver

Posted: Fri Dec 14, 2018 6:16 am
by Roberthh
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)

Re: WS2813 RGB LED strip driver

Posted: Fri Dec 14, 2018 8:02 am
by OutoftheBOTS_
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

Posted: Thu Dec 20, 2018 11:47 pm
by kamikaze
Modified WS2812 driver, works for my WS2813 led strip from aliexpress

https://github.com/kamikaze/pyboard-exa ... /ws2813.py