Driver for WS2812 RGB LEDs (NeoPixels, ...)

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Site Admin
Posts: 505
Joined: Mon Dec 09, 2013 5:02 pm

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby Damien » Wed Dec 10, 2014 11:49 pm

fma wrote:Great! But as I understand from your post on the other thread, the call to send() is still blocking during transfert, so CPU is not availabe for other tasks. Am I right?

Right. First step was to get DMA working, and main reason for that was to get uninterrupted transfers with interrupts enabled. Previously send() was disabling interrupts during a transfer, and this can lead to lost interrupts. Now it's much better with DMA, even if you have to wait for it to finish.

Next step will be to have an option to return before transfer is compete. And some function to check completion, as well as a callback.

Posts: 13
Joined: Wed Apr 22, 2015 1:44 pm

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby MattMatic » Wed Apr 22, 2015 2:17 pm

Really appreciate the WS2812 library :)

Got me going in under 5 minutes.

However, I ran into a couple of minor issues.

First, the "buf_bytes" are not correct. Since SPI shifts the MSB first, the assignment should be (0x88, 0x8e, 0xe8, 0xee).
Having sorted that out my first NeoPixel worked properly.

And, a little extra tweak was to add the RST period of 50µs+ after sending the pixel information. I just added 3 to the self.buf_length ;)
All seems good!

...I will probably take this library a bit further with 'corrections' for linear/log brightness and other animation functions that I need.

Posts: 2
Joined: Tue Jul 07, 2015 9:05 am

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby jszsj » Tue Jul 07, 2015 9:08 am

there is a issue with the drive:
I have a 16 leds chain and a 2 leds chain,I used to test, the first led in both chain does not light, other leds works fine, someone can help me with this?Thanks
(ps: Sorry for my poor Engilsh)

Posts: 1
Joined: Sun Sep 11, 2016 5:09 am

RBGW NeoPixle support

Postby taylorl123 » Sun Sep 11, 2016 5:15 am

I have a strip of 30 rbgw neopixels and it doesn't seem to work properly with this driver. I was just wondering if it was something i did or if the new rbgw LEDs are not supported in this driver.

Posts: 4
Joined: Mon Sep 26, 2016 1:45 am

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby ricblue » Tue Sep 27, 2016 7:43 pm

Have you seen the Adafruit MicroPython + NeoPixel video

The author has created an rgbw version of the MicroPython Neopixel drivers. see here:

Posts: 4
Joined: Thu Sep 29, 2016 9:01 am

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby Electra » Tue Oct 25, 2016 12:58 am

Not sure where else to put this, but maybe someone playing with neopixels will find it handy.
Wanted to change colours randomly without the 'next' colour being close to the previous.
A bit limited in some way, but someone might have fun.

Code: Select all

# Random colour methods for NeoPixel, does use more bytes to store random states
# Based heavly on
# Ported to Python by Electra

import neopixel

class NeoPixel(neopixel.NeoPixel):

  def __init__(self,pin, n):
    neopixel.NeoPixel.__init__(self,pin, n)  # Pass on the __init__
    from uos import urandom
    self.hrnd = [0] * self.n
    for i in range(0,self.n):
      self.hrnd[i] = tmprnd[i]*0.0039

  # HSV values in [0..1]  returns (r, g, b) values from 0 to 255
  def hsv_to_rgb(self,h, s, v):
    h_i = int((h*6))
    f = h*6 - h_i
    p = v * (1 - s)
    q = v * (1 - f*s)
    t = v * (1 - (1 - f) * s)
    if h_i==0: r, g, b = v, t, p
    if h_i==1: r, g, b = q, v, p
    if h_i==2: r, g, b = p, v, t
    if h_i==3: r, g, b = p, q, v
    if h_i==4: r, g, b = t, p, v
    if h_i==5: r, g, b = v, p, q
    return (int(r*256), int(g*256), int(b*256))

  # Move through semi random hue sequence, random saturation
  # Optional parameter selected pixel otherwise all are affected
  def rndhue(self,pix=-1):
    from uos import urandom
    for i in range(0,self.n):
      if pix != -1:
        i = pix 
      # Uncomment next line and indent two after for random skips
#      for t in range(int(urandom(1)[0]/50)+1):   
      self.hrnd[i] += 0.618033988749895  # Golden ratio
      self.hrnd[i] %= 1
      self[i]=self.hsv_to_rgb(self.hrnd[i],(urandom(1)[0]/510)+0.5, 0.50)
      if pix != -1:

import rgbled
import machine
import time
np = rgbled.NeoPixel(machine.Pin(4), 2)
while True: np.rndhue() ; np.write() ; time.sleep(1)

Posts: 7
Joined: Sat Nov 28, 2015 12:04 am

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby deanr » Sun Feb 05, 2017 4:38 pm

I was using an 8 Neopixel strip (Ywrobot Rainbow LED L8 V3) and the posted code on my pyboard (v1.8.7v 2017-01-08 PYPv1.1). It would only light the upper 7 units as if shifted up.
I played with the code and the driver to understand this behavior and found that the first unit when lit (by accident) would not change.
The solution that seems to work is to change the SPI declaration from: self.spi = pyb.SPI(spi_bus, pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1) self.spi = pyb.SPI(spi_bus, pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=0)

... Can't claim to know why.
Thanks for the driver. Missed the NeoPixel driver (ESP8266) in the pyboard micropython...

Posts: 1
Joined: Tue Feb 07, 2017 6:59 am

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby ledcolor » Tue Feb 07, 2017 7:03 am

if your driver can control WS2813 led ,which have 2 data rather than one Data? ... 65&sid=199

User avatar
Posts: 931
Joined: Thu May 28, 2015 5:54 pm

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby deshipu » Tue Feb 07, 2017 7:47 am

The WS2812B "neopixel" is a 5V device, and the 3.3V logic levels don't actually fall within its expected "high" value. However, it only applies to the first pixel in the chain, as the subsequent ones get the logic from the previous pixel, and that will already have a proper voltage. A standard solution to this problem is to use a transistor or logic level shifter. Note that this applies to the original neopixels -- the Chinese clones have a broader range of logic levels they accept. I have no idea why changing the phase works for you.

Posts: 167
Joined: Fri Dec 13, 2013 9:25 am

Re: Driver for WS2812 RGB LEDs (NeoPixels, ...)

Postby torwag » Tue Feb 07, 2017 8:00 am

Just a side note:

Code: Select all

 return (int(r*256), int(g*256), int(b*256))

Should that not be 255 instead of 256? A solid 1.0 returns otherwise an overflow for the neopixel buffer (off instead of full bright)

Return to “Hardware Projects”

Who is online

Users browsing this forum: No registered users and 2 guests