WS2812 Addressable LEDs: Raspberry Pi Quickstart Guide

Thanks for coming back and posting your solution once you found it, you may save someone in the future many hours of lost sleep!

6 Likes

Just giving back to the community, Trent. :slightly_smiling_face:

6 Likes

Just Curious: Has anyone been able to get two WS2812 strips connected in series working on SPI?
I am using a 5V 60A power supply, SPI (Configuration posted here), changed the number of LED pixels to 600. I get a SPI transfer error.

pi@raspberrypi4:~ $ python /home/pi/rpi_ws281x/python/examples/modstrandtest.py -c
Press Ctrl-C to quit.
Theater chase animations.
Can’t send spi messageTraceback (most recent call last):
File “/home/pi/rpi_ws281x/python/examples/modstrandtest.py”, line 136, in
theaterChase(strip, Color(127, 127, 127)) # White theater chase
File “/home/pi/rpi_ws281x/python/examples/modstrandtest.py”, line 38, in theaterChase
strip.show()
File “/usr/local/lib/python2.7/dist-packages/rpi_ws281x/rpi_ws281x.py”, line 138, in show
raise RuntimeError(‘ws2811_render failed with code {0} ({1})’.format(resp, str_resp))
RuntimeError: ws2811_render failed with code -14 (SPI transfer error)

Went back and read up on the WS281x pyPI page and made the modification - in case it is the cause:

SPI

When using SPI the ledstring is the only device which can be connected to the SPI bus. Both digital (I2S/PCM) and analog (PWM) audio can be used.

Many distributions have a maximum SPI transfer of 4096 bytes. This can be changed in /boot/cmdline.txt by appending

spidev.bufsiz=32768

Still I get the same traceback error. I have modified the strandtest to run only from the Theaterwipe onwards.

I can run two strips in parallel (300 pixels setting in the python file), but not in series. While in series, I also tried feeding the data in series and powered the second strip individually, still the same issue with either 300 pixels or 600 pixels setting. The first strips works as intended, the second strip does nothing.

Is there a way to run two strips in series using SPI (or PWM)?

2 Likes

The WS2812 LEDs don’t support SPI, they use a proprietary bit-banging communications procedure. It’s described in the datasheet here: WS2812B Datasheet
I’d always recommend using a WS2812 library like Adafruit’s Neopixel library or FastLED.
Was there a reason you wanted to use SPI?

Sorry I missed this part from earlier, you will need to use a Pin that supports PWM output for the WS2812s to be driven. The PWM pins are 12,13,18, and 19.
The SPI pins do use Pin 18 as well,for SPI1, you could look at switching to SPI0 to free up pin 18 for PWM. Otherwise keep using SPI1 and move the PWM pin to 12 or 13.

image

2 Likes

Thanks, Trent. I am using the SPI because I want to keep the digital audio “ON” on my Pi4.

I am using the RPi_WS281x library from PyPi (Sudo pip install RPi_ws281x). I also installed the files from this tutorial to run the strip(s).

GPIO 10 works well for one strip or two strips in parallel. The DIN input is from the same pin from the level shifter for both, when connected in parallel. I can try the GPIO 18 on my Pi Zero, where I don’t intend to use the audio.

I tried the Adafruit Neopixel library (Python 3) and since the files from this site are compiled for Python 2.x, I was not able to run the scripts.

2 Likes

Thanks again, Trent.
Using PWM Pin 18 on Pi Zero and it works for two strips in series. Among other things, installed Raspbian Bullseye on the Zero. The python script here does not work with Python 2 anymore. The script changes the default of the command “python” to python2, but have to use python3 as root to run this. Also, had to use pip3 install of the Rpi_ws281x python repository instead of pip to get it running. Hope this would help anyone upgrading from Buster to Bullseye.

G

3 Likes

I am new to using WS2812 LEDs and followed the instructions in the video. I have found a couple of questions on the error I am getting. I have tried several suggestions but none have worked. The error I am seeing is:

pi@rpi4:~/rpi_ws281x/python/examples $ sudo python strandtest.py
Traceback (most recent call last):
  File "strandtest.py", line 9, in <module>
    from rpi_ws281x import *
ImportError: No module named rpi_ws281x

I also checked the version of python I am using which is Python 2.7.16 as I noted a post that stated this demo will not work with python 3.

Is there other code that I might try rather than strandtest.py?

Thanks

2 Likes

Welcome to the forum Don.

See if it works with this command

sudo python3 strandtest.py

Or sudo python2 strandtest.py

3 Likes

G,
Thank you! The python3 command worked. The python2 did not. Seems that I have a lot to learn about python and python in rpi.
Is there a resource that you could point me to that would explain to me why one works and one does not? Is this just a matter of libraries?

Thanks again,
Don

3 Likes

You’re welcome, Don.
You must’ve Bullseye OS. The default is python3. Just check the post I had made before your first post on this thread.

I am not a wizard of python, learn everything the good, old, hard way. :slight_smile:

3 Likes

Hello, quick question how is the pattern supposed to look like if you don’t have a 5V power supply ? Can the Raspberry Pi provide power to the led strip ? I can’t make it work somehow so I’m wondering if the power is the cause

1 Like

Hi Vivien,

Welcome to the forum!!

The PI has an onboard Polyfuse that will increase its resistance to a level where it isnt allowing much current.
You’ll be able to power a small strip off the 5V GPIO (about 5 at full brightness or 15 at a reasonable brightness).
Also note that the LED’s will draw current even when they’re not lit!

There’s a middle ground where the WS2812’s are receiving a lower than expected voltage and there is a noticeable redshift @Liam120347 has a good photo here: 12 day project: ESP32 - LED controller - #6 by Liam120347

Let us know how you go!
Liam

2 Likes

Hello All,

Im trying to utilise this however, when running

curl -L http://coreelec.io/33 | bash

it returns errors at the end

bash: line 22: cd: python: no such file or directory
python: cant open file 'setup.py': [Errno 2] No such file or directory

Manually adding the python directories from the Github also doesn’t work as SCONS isnt building something properly and there is no python directory

What’s going on? All packages are up to date. And its a fresh install of RPI

Hi @tsykes - welcome to the forum!
We’ve been able to replicate this error and are working to fix it! Thanks for raising this issue.

Looks like the setup process has changed over time and the script needs updating. We’ll let you know here when it’s back up and running.

2 Likes

Hi again @tsykes - i’m curious to see if the example code from this tutorial will work after installing the package through Thonny (pip). If so, we can update the written content to refer to this method instead!

In thonny, open Tools > Manage Packages

image


Search for rpi_ws281x, find the correct result and install it.

The example code ought to work now!

2 Likes

Hi Michael,
I am getting the same error. It seems the GIT source has been changed to remove Python. I did find a later GIT source GitHub - jgarff/rpi_ws281x: Userspace Raspberry Pi PWM library for WS281X LEDs, however, that gives C compilation errors.
Please advise when the install script has been updated. In the meantime I will keep trying to install the libraries.

1 Like

Hi Adrian,

Welcome to the forum, very sorry about the delay in getting back to you!!

Did Michaels post above when using Thonny help?

Liam

1 Like

Hi Liam,
No that wasn’t much help. I did eventually solve the issue by copious googling. I found an update install script at GitHub - richardghirst/rpi_ws281x: Userspace Raspberry Pi PWM library for WS281X LEDs and https://github.com/jgarff/rpi_ws281x/tree/master/python among other things I don’t remember.

What I could do with help is a breadboard wiring diagram for SPDT micro-switch. Either the switch does not work reliably or depending how I wire the Pico, the Pico shutdowns. Thank you

Adrian

2 Likes

Hi Adrian,

What were you looking for the switches poles to do? It sounds like you might be shorting the regulator or leaving it OC (shorting is real bad for it)

1 Like