Last active
July 2, 2022 10:53
-
-
Save chris-gong/fbebf494725cc762d731d567700fdafa to your computer and use it in GitHub Desktop.
Code for lighting up a WS2812 LED Strip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 I would suggest signing into your root account, or using sudo
before running any commands. For example, sudo python glow.py
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)