Skip to content

Instantly share code, notes, and snippets.

@chris-gong
Last active July 2, 2022 10:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chris-gong/fbebf494725cc762d731d567700fdafa to your computer and use it in GitHub Desktop.
Save chris-gong/fbebf494725cc762d731d567700fdafa to your computer and use it in GitHub Desktop.
Code for lighting up a WS2812 LED Strip
from rpi_ws281x import *
# LED strip configuration:
LED_COUNT = 300 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ,LED_DMA,LED_INVERT,LED_BRIGHTNESS,LED_CHANNEL)
strip.begin()
for x in range(0,LED_COUNT):
strip.setPixelColor(x,Color(255,0,0))
strip.show()
@daggiemaggie
Copy link

Hi,

I am new to raspberry pi and coding with python. I was trying your code on my raspberry pi 4 (model B) but it gives errors like this:
Can’t open /dev/mem: Permission denied
Traceback (most recent call last):
File “/home/pi/Documents/glow.py”, line 12, in
Strip.begin()
File “/usr/local/lib/python3.7/dist-packages/rpi_ws281x/rpi_ws281x.py”, line 130, in begin
raise RuntimeError(‘ws2811_init failed with code {0} ({1})’.format(resp, str_resp))
RuntimeError: ws2811_init failed with code -5 (mmap() failed)

The assistant in python also says that 'Adafruit_NeoPixel' and 'Color' are not defined.

Is there something I am doing wrong?

Thanks in advance :)

@chris-gong
Copy link
Author

chris-gong commented May 19, 2020

@daggiemaggie I would suggest signing into your root account, or using sudo before running any commands. For example, sudo python glow.py.

@Vinaypraveen
Copy link

Hi,

I am new to raspberry pi and coding with python. I was trying your code on my raspberry pi 4 (model B) but it gives errors like this:
Can’t open /dev/mem: Permission denied
Traceback (most recent call last):
File “/home/pi/Documents/glow.py”, line 12, in
Strip.begin()
File “/usr/local/lib/python3.7/dist-packages/rpi_ws281x/rpi_ws281x.py”, line 130, in begin
raise RuntimeError(‘ws2811_init failed with code {0} ({1})’.format(resp, str_resp))
RuntimeError: ws2811_init failed with code -5 (mmap() failed)

The assistant in python also says that 'Adafruit_NeoPixel' and 'Color' are not defined.

Is there something I am doing wrong?

Thanks in advance :)

use LED_PIN = 10 instead of LED_PIN = 18
and go to interfaces under raspberry pi configuration and enable all the GPIO related interfaces (eg: SPI)
then without root also your program will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment