Guide by Tim; Control Multiple Fully-Addressable WS2812B RGB LED Strips with a Raspberry Pi Single Board Computer

i have 19 pixels, so 19 pixels are in the config specified. where can i see what X is when error occurs?

i do it with 5 in the config, change line 20 to 1 pixel and the same occurs…

You can see the value of x by including a debug print statement in your code at that point. Or you can look at the code and see what process is being used to set the value of x. Is it in a list processing loop?

What is line 20? Has it got something to do with setting the value of x? If you post the code you are using it may make it easier to see where the problem is.

here is the code:

#include all neccessary packages to get LEDs to work with Raspberry Pi
import time
import board
import neopixel

#Initialise a strips variable, provide the GPIO Data Pin
#utilised and the amount of LED Nodes on strip and brightness (0 to 1 value)
pixels1 = neopixel.NeoPixel(board.D18, 19, brightness=1)

#Also create an arbitary count variable
x=0

#Focusing on a particular strip, use the command Fill to make it all a single colour
#based on decimal code R, G, B. Number can be anything from 255 - 0. Use a RGB Colour
#Code Chart Website to quickly identify a desired fill colour.
pixels1.fill((0, 220, 0))

#Below demonstrates how to individual address a colour to a LED Node, in this case
#LED Node 10 and colour Blue was selected
pixels1[10] = (0, 20, 255)

#Sleep for three seconds, You should now have all LEDs showing light with the first node
#Showing a different colour
time.sleep(4)

#Little Light slider script, it will produce a nice loading bar effect all the way up
#and then all the way back
#This was created using a While Loop taking advantage of that arbitary variable to determine
#which LED Node we will taget/index with a different colour

#Below will loop until variabe x has value 35
while x<35:
    
    pixels1[x] = (255, 0, 0)
    pixels1[x-5] = (255, 0, 100)
    pixels1[x-10] = (0, 0, 255)
    #Add 1 to the counter
    x=x+1
    #Add a small time pause which will translate to 'smoothly' changing colour
    time.sleep(0.05)

#below section is the same process as above loop just in reverse
while x>-15:
    pixels1[x] = (255, 0, 0)
    pixels1[x+5] = (255, 0, 100)
    pixels1[x+10] = (0, 255, 0)
    x=x-1
    time.sleep(0.05)

#Add a brief time delay to appreciate what has happened    
time.sleep(4)

#Complete the script by returning all the LED to off
pixels1.fill((0, 0, 0))


I would be extremely grateful if you could tell me where exactly I should write what and how to see the variable

Your print statement should go immediately before the line that has produced the error (line 34). The error is occurring because the value of x is outside the allowed range for the pixel string you have specified in the pixel array constructor. That, in turn, will be because your while loop is going from 0 to 35. A similar problem exists in the second while loop.

Note that the code is relying on the library ignoring negative indices. If it does ignore the negative values you will not get the effect you expect. If it does not ignore negative indices then the other lines in the loop will also error because when x is 0 (the starting value) then [x-5] and [x-10] will be invalid values for the pixel number.

Okay, I just commented out both loops. errors are gone. Now not all LEDs would have to be turned on by pixels1.fill and the 10th LED by pixels1[10] ?

they dont :frowning:

I wouldn’t recommend just deleting chunks of code. What was the result when you added that print statement?

If you are rewriting the code to do something different you should show what changes you made and describe what you expected to happen and what actually happened.

The strange thing is that it is the original OneStripNeopixel.py file from the core page and in the tutorial on YouTube it works with 21 LEDs and an unchanged file.

look here

1 Like

You have changed the file.
Original:

pixels1 = neopixel.NeoPixel(board.D18, 55, brightness=1)

Your code:

pixels1 = neopixel.NeoPixel(board.D18, 19, brightness=1)

It is always likely that a change in one part of the file will require other changes as well.

Either set the code back to what it originally was, or use the debug print statement that I recommended so that you can identify the cause of the problem and adjust the loop to eliminate it. The two changes will produce slightly different results, so the one you choose will depend on what you are trying to achieve.

1 Like

Using the latest version of Raspberry Pi OS on a Pi 4, I ran into an issue installing each of the packages:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    For more information visit http://rptl.io/venv

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

This was resolved by adding the flag --break-system-packages to each of the install commands.

So, the three install commands I used were:

sudo pip3 install rpi_ws281x --break-system-packages
sudo pip3 install adafruit-circuitpython-neopixel --break-system-packages
sudo python3 -m pip install --force-reinstall adafruit-blinka --break-system-packages

This seems a possibly less-than-ideal solution. I did try getting this to run in a venv but ran into an issue running the script as root. I’m sure someone smarter than me can figure that out :slight_smile:


I also tried this on a Pi 5, and got an error indicating the hardware isn’t supported:

Traceback (most recent call last):
  File "/home/michael/Documents/led/strandtest.py", line 91, in <module>
    strip.begin()
  File "/usr/local/lib/python3.11/dist-packages/rpi_ws281x/rpi_ws281x.py", line 143, in begin
    raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(resp, str_resp))
RuntimeError: ws2811_init failed with code -3 (Hardware revision is not supported)
Segmentation fault

I assume this isn’t an error with the script, but rather with the packages. Anyway, just posting here in case someone else tries this on a Pi 5.

1 Like

The error you got from pip is a new behaviour from the latest OS.

Python package management is often a confusing experience and users can often break their builds so the RPi foundation has opted to semi-force the use of Python virtual environments.

As for the following error, I agree it may be a package configuration issue, though some light research hasn’t turned anything up yet

1 Like

What translations (e.g., gpiod…) are required to get ws2812b Python control code (e.g., Neopixel…) that works on Raspberry Pi 2b to work on the new Raspberry Pi 5? I have seen references (link to example: How to Control the Raspberry Pi 5 GPIO with Python 3 | Tom's Hardware) that the RPi 5 uses the RP1 chip that memory maps I/O requiring changes to I/O code. I don’t want to reinvent the wheel if there is a straightforward changes that has already been developed.

Thanks in advance for your help

Hi @Stan264266 - welcome to the forums :slight_smile:

At the moment we really don’t know! At this stage i would expect the relevant drivers are going to need patching to cope with the new hardware definitions. After some light research I can’t find any mention of Raspberry Pi 5 on any drivers eg. NeoPixels

Michael,

Thanks for your response. Can you point me to documentation for the libraries that are imported to support the control of the ws2812B LED strips. Python code snippet below:

from rpi_ws281x import *

import neopixel

import board

Objectively I can use the documentation to determine what modifications are needed.

Thanks,

Stan

here’s what i found

https://docs.circuitpython.org/projects/neopixel/en/latest/

1 Like

Michael,

Thanks for the additional info.

I will step through the references to see if I can determine what needs to be modified to interface to the RPi 5.

The “rpi_ws281x” and “board” libraries seem the most likely culprits. Are there other libraries that you suspect will need modifications as well?

Thanks again,

Stan

I would begin by opening a dialogue with the maintainers of those repos, or checking if open issues exist regarding RPi 5
they may already be working on it as we speak!

2 Likes

Michael,

Thanks for the follow up.

I’ve had a chance to do a little debugging but definitely need to do more to present a targeted, relevant request for support.

The “pixels” array function does appear to be populating correctly when I set the number of LEDs in the strip under test.

Also, I found the info below at Tom’s Hardware that I setup and verified using GPIO 17 to control a single standard LED.

How to Control the Raspberry Pi 5 GPIO with Python 3 | Tom’s Hardware (tomshardware.com)

Stan

2 Likes

Good morning!

I am having issues with downloading the necessaty libraries. Could someone help?

When I run:
sudo pip3 install adafruit-circuitpython-neopixel
I am getting the following error message:
Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-0sma2agr/Adadfruit-Blinka/

I’ve tried looking up some solutions but I have no been successful. Any help would be appreciated!

1 Like

Hi Bryan,

Welcome to the forum! Can you please share what Pi you are using and what version of the OS you have. I know there’s some issues with pip on Bookworm so you may need to install Bullseye.

1 Like

Thanks Jack!

I am using a Raspberry Pi 3 Model B and it looks like the version is 2017-11-29? Looks like I need to update/upgrade something… I really appreciate the help! Let me know what I need to do.

2 Likes