WS2813 RGB LED strip driver

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
kamikaze
Posts: 149
Joined: Tue Aug 16, 2016 10:10 am
Location: СССР
Contact:

WS2813 RGB LED strip driver

Post by kamikaze » Wed Dec 05, 2018 12:18 am

Hello. Is there any library / sample code on how to get WS2813 working with PyBoard? Thanks

shaoziyang
Posts: 252
Joined: Sun Apr 17, 2016 1:55 pm

Re: WS2813 RGB LED strip driver

Post by shaoziyang » Wed Dec 05, 2018 2:40 am

There is only SPI drive for WS2812/2813 now.

rhubarbdog
Posts: 161
Joined: Tue Nov 07, 2017 11:45 pm

Re: WS2813 RGB LED strip driver

Post by rhubarbdog » Fri Dec 14, 2018 4:38 am

have you got an example of SPI being used to light some neopixel?

User avatar
Roberthh
Posts: 1370
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: WS2813 RGB LED strip driver

Post by Roberthh » Fri Dec 14, 2018 6:16 am

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)

OutoftheBOTS_
Posts: 556
Joined: Mon Nov 20, 2017 10:18 am

Re: WS2813 RGB LED strip driver

Post by OutoftheBOTS_ » Fri Dec 14, 2018 8:02 am

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

User avatar
kamikaze
Posts: 149
Joined: Tue Aug 16, 2016 10:10 am
Location: СССР
Contact:

Re: WS2813 RGB LED strip driver

Post by kamikaze » Thu Dec 20, 2018 11:47 pm

Modified WS2812 driver, works for my WS2813 led strip from aliexpress

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

Post Reply