Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xgqfrms/7f09dc1fcb0afd994ec942183f41c24b to your computer and use it in GitHub Desktop.
Save xgqfrms/7f09dc1fcb0afd994ec942183f41c24b to your computer and use it in GitHub Desktop.
How to fix using Raspberry Pi 3B GPIO Pin to control the WS2812B RGB LEDs Strip error issue! (rpi_ws281x / neopixel)

How to fix using Raspberry Pi 3B GPIO Pin to control the WS2812B RGB LEDs Strip error issue! (rpi_ws281x / neopixel)

adafruit/Adafruit_CircuitPython_NeoPixel#151 (comment)

https://www.cnblogs.com/xgqfrms/p/17387711.html

https://tutorials-raspberrypi.com/connect-control-raspberry-pi-ws2812-rgb-led-strips/

final solution ✅

https://www.youtube.com/watch?v=4fF5joeQ2dY

$ vcgencmd get_throttled
throttled=0x0

disable audio snd_bcm2835

$ sudo vim /boot/config.txt

$ sudo cat /boot/config.txt

# Enable audio (loads snd_bcm2835)
# ✅ fix: https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/issues/151
# dtparam=audio=on
$ sudo reboot

$ sudo ./neopixel-strip-rgb.py
# OR
$ sudo ./pixel-strip-rgb.py
$ cat ./neopixel-strip-rgb.py
#!/usr/bin/env python3
# coding: utf8

import board
import neopixel
from time import sleep

# 60 LEDs
pixels = neopixel.NeoPixel(board.D18, 60)

def red_led_strip():
  for x in range(0, 60):
    pixels[x] = (255, 0, 0)
    sleep(0.1)
def green_led_strip():
  for x in range(0, 60):
    pixels[x] = (0, 255, 0)
    sleep(0.1)
def blue_led_strip():
  for x in range(0, 60):
    pixels[x] = (0, 0, 255)
    sleep(0.1)

def clear_buffer():
  for x in range(0, 60):
    pixels[x] = (0, 0, 0)

try:
  red_led_strip()
  sleep(1.0)
  green_led_strip()
  sleep(1.0)
  blue_led_strip()
  sleep(1.0)
except KeyboardInterrupt:
  print('Ctrl + C exit ✅')
except RuntimeError as error:
  print("error =", error, error.args[0])
  pass
except Exception as error:
  print("exception =", error)
  raise error
finally:
  print("after three seconds, auto clear buffer! 👻")
  sleep(3.0)
  # cleanup
  clear_buffer()
  print('clear 🚀')


"""
$ chmod +x ./neopixel-strip-rgb.
# ✅
$ sudo ./neopixel-strip-rgb.
"""
$ cat pixel-strip-rgb.py
#!/usr/bin/env python3
# coding: utf8

from time import sleep
from rpi_ws281x import PixelStrip, Color

# LED strip configuration:
LED_COUNT = 60        # 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 a signal (try 10)
# LED_BRIGHTNESS = 255   # Set to 0 for darkest and 255 for brightest
LED_BRIGHTNESS = 51   # 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

# Create a NeoPixel object with the appropriate configuration.
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)

# Initialize the library (must be called once before other functions).
strip.begin()
print("Test beginning ✅")

def clear_buffer():
  for i in range(LED_COUNT):
    strip.setPixelColor(i, Color(0, 0, 0))
  strip.show()
  sleep(1.0)

def rgb_test():
  # print("strip.numPixels() =", strip.numPixels())
  while True:
    for i in range(strip.numPixels()):
      # red led ✅
      strip.setPixelColor(i, Color(255, 0, 0))
      strip.show()
      sleep(0.1)
    sleep(1.0)
    for i in range(strip.numPixels()):
      # green led ✅
      strip.setPixelColor(i, Color(0, 255, 0))
      strip.show()
      sleep(0.1)
    sleep(1.0)
    for i in range(strip.numPixels()):
      # blue led ✅
      strip.setPixelColor(i, Color(0, 0, 255))
      strip.show()
      sleep(0.1)
    sleep(1.0)

try:
  rgb_test()
except KeyboardInterrupt:
  print('Ctrl + C exit ✅')
  clear_buffer()
  sleep(2.0)
except RuntimeError as error:
  print("error =", error, error.args[0])
  clear_buffer()
  pass
except Exception as error:
  print("exception =", error)
  clear_buffer()
  raise error
finally:
  print("clear buffer ✅")
  clear_buffer()
  sleep(3.0)
"""
$ sudo ./pixel-strip-rgb.py
"""

image

@xgqfrms
Copy link
Author

xgqfrms commented Apr 26, 2025

image

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