xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

How to fix WS2812B RGB LEDs strip not work bug All In One

How to fix WS2812B RGB LEDs strip not work bug All In One

RGB / RGBW

how to clear ws2812b LED buffer data

如何清除 ws2812b LED buffer 数据

finally: clear buffer ✅

#!/usr/bin/env python3
# coding: utf8

from time import sleep
import board
import adafruit_dht

dhtDevice = adafruit_dht.DHT11(board.D17)
# dhtDevice = adafruit_dht.DHT11(board.D18)
# dhtDevice = adafruit_dht.DHT11(board.D18, use_pulseio=False)

print('开始读取温湿度 🌡 💦')

# once
def once():
  try:
    temperature_c = dhtDevice.temperature
    temperature_f = temperature_c * (9 / 5) + 32
    humidity = dhtDevice.humidity
    print("🌡 华氏温度 Temperature: {:.1f} °F ".format(temperature_f))
    print("🌡 摄氏温度 Temperature: {:.1f} °C".format(temperature_c))
    print("💦 湿度 Humidity: {}% ".format(humidity))
  except KeyboardInterrupt:
    print('Ctrl + C 退出 ✅')
  except RuntimeError as error:
    print("error =", error, error.args[0])
    pass
  except Exception as error:
    # dhtDevice.exit()
    raise error
  finally:
    sleep(2.0)
    dhtDevice.exit()
    # cleanup
    print('clear 🚀')

once()

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

https://stackoverflow.com/questions/74167188/get-rid-of-lost-access-to-message-queue-in-a-simple-python-script/76264450#76264450

Adafruit_CircuitPython_NeoPixel

$ pip3 install adafruit-circuitpython-neopixel

$ sudo pip3 install adafruit-circuitpython-neopixel

https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel#installing-from-pypi

import board
import neopixel

bugs ❌

Adafruit

https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/issues/151

pi@raspberrypi:~/Desktop $ vim ./neo-strip.py 
pi@raspberrypi:~/Desktop $ cat neo-strip.py 
#!/usr/bin/env python3
# coding: utf8

import board
import neopixel
from time import sleep


PIN = board.D18
# LEDs = 60
LEDs = 30
ORDER = neopixel.RGBW
pixels = neopixel.NeoPixel(PIN, LEDs, brightness=0.2, auto_write=False, pixel_order=ORDER)

# 60 LEDs
# pixels = neopixel.NeoPixel(board.D18, 60)
while True:
  for x in range(0, 60):
    # RGBW ❓
    # RGB
    pixels[x] = (255, 0, 0, 0)
    sleep(0.1)

"""
$ chmod +x ./led-strip.py
# ❌ Can't open /dev/mem: Permission denied
# $ ./led-strip.py

# ✅
$ sudo ./led-strip.py
"""
pi@raspberrypi:~/Desktop $ sudo ./neo-strip.py
Traceback (most recent call last):
  File "/home/pi/Desktop/./neo-strip.py", line 21, in <module>
    pixels[x] = (255, 0, 0, 0)
  File "/usr/local/lib/python3.9/dist-packages/adafruit_pixelbuf.py", line 310, in __setitem__
    self._set_item(index, r, g, b, w)
  File "/usr/local/lib/python3.9/dist-packages/adafruit_pixelbuf.py", line 274, in _set_item
    raise IndexError
IndexError
pi@raspberrypi:~/Desktop $

??? $ cat ./neo-strip.py

#!/usr/bin/env python3
# coding: utf8

import board
import neopixel
from time import sleep

PIN = board.D18
# 0.3W/LED (03mA ~ 60mA)
# LEDs = 60
LEDs = 30
# mode: GRB 
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(PIN, LEDs, brightness=0.2, auto_write=True, pixel_order=ORDER)

while True:
  for x in range(0, LEDs):
    # GRB => Green
    pixels.fill((255,0, 0))
    pixels.show()
    sleep(0.5)
    # GRB => Red
    pixels[x] = (0, 255, 0)
    sleep(0.1)

"""
$ chmod +x ./led-strip.py
# ❌ Can't open /dev/mem: Permission denied
# $ ./led-strip.py

# ✅
$ sudo ./led-strip.py
"""

image

solution

  1. 电压不足,外接电源 ?

通道模式: GRB
LED 功率: 0.3W/每颗

Green Red Blue / Green Red Blue White

https://docs.circuitpython.org/projects/neopixel/en/latest/api.html#neopixel.GRB

??? 查询 neopixel API

点亮 LED 前,清空 buffer 数据?

clear

# clear buffer ???
pixels.fill((0,0, 0))

https://adafruit.github.io/Adafruit_NeoPixel/html/class_adafruit___neo_pixel.html#ae259682b202be0acd258d6879f4c7121

ws2812 - NodeMCU Documentation

https://nodemcu.readthedocs.io/en/1.5.4.1-final/modules/ws2812/

https://randomnerdtutorials.com/guide-for-ws2812b-addressable-rgb-led-strip-with-arduino/

rpi_ws281x

#!/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 = 50   # Set to 0 for darkest and 255 for brightest
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

# 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()
except RuntimeError as error:
  print("error =", error, error.args[0])
  clear_buffer()
  pass
except Exception as error:
  print("exception =", error)
  clear_buffer()
  raise error

https://www.cnblogs.com/xgqfrms/tag/rpi_ws281x/

image

#!/usr/bin/env python3
# coding: utf8

print("Test ✅")

from time import sleep
from rpi_ws281x import PixelStrip, Color

# LED strip configuration:
LED_COUNT = 30        # 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 = 20   # Set to 0 for darkest and 255 for brightest
LED_INVERT = True     # 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 NeoPixel object with appropriate configuration.
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)

# Intialize the library (must be called once before other functions).
strip.begin()

# clear
for i in range(strip.numPixels()):
    strip.setPixelColor(i, Color(0, 0, 0))
strip.show()

sleep(1)

# Switch all pixels on
for i in range(strip.numPixels()):
    strip.setPixelColor(i, Color(255, 255, 255))
strip.show()

print("Test finished 🎉")
exit()

https://github.com/rpi-ws281x

https://github.com/rpi-ws281x/rpi-ws281x-python

https://github.com/rpi-ws281x/rpi-ws281x-python/tree/master/examples

https://github.com/rpi-ws281x/rpi-ws281x-python/blob/master/examples/neopixelclock.py#L48

rpi_ws281x API docs

pixelpi

https://pixelpi.readthedocs.io/en/latest/_modules/pixelpi/strip.html

clear

https://pixelpi.readthedocs.io/en/latest/library.html#pixelpi.Strip.clearLEDs

https://python.hotexamples.com/examples/rpi_ws281x/PixelStrip/begin/python-pixelstrip-begin-method-examples.html

REPL

https://www.runoob.com/try/runcode.php?filename=HelloWorld&type=python3

#!/usr/bin/python3

for i in range(0, 10):
  print("i =", i)

"""
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
"""
print("\n")
for i in range(0, 10, 2):
  print("step i =", i)

"""
step i = 0
step i = 2
step i = 4
step i = 6
step i = 8
"""

"""

range(stop)
range(start, stop[, step])

start: 计数从 start 开始。默认是从 0 开始。例如 range(5) 等价于 range(0, 5)
stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是 [0, 1, 2, 3, 4] 没有 5
step:步长,默认为 1。例如:range(0, 5) 等价于 range(0, 5, 1)

https://www.runoob.com/python3/python3-func-range.html

"""

https://www.runoob.com/python3/python3-func-range.html

demos


# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Simple test for NeoPixels on Raspberry Pi
import time
import board
import neopixel


# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
# NeoPixels must be connected to D10, D12, D18 or D21 to work.
pixel_pin = board.D18

# The number of NeoPixels
num_pixels = 30

# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.GRB

pixels = neopixel.NeoPixel(
    pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)


def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        r = g = b = 0
    elif pos < 85:
        r = int(pos * 3)
        g = int(255 - pos * 3)
        b = 0
    elif pos < 170:
        pos -= 85
        r = int(255 - pos * 3)
        g = 0
        b = int(pos * 3)
    else:
        pos -= 170
        r = 0
        g = int(pos * 3)
        b = int(255 - pos * 3)
    return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)


def rainbow_cycle(wait):
    for j in range(255):
        for i in range(num_pixels):
            pixel_index = (i * 256 // num_pixels) + j
            pixels[i] = wheel(pixel_index & 255)
        pixels.show()
        time.sleep(wait)


while True:
    # Comment this line out if you have RGBW/GRBW NeoPixels
    pixels.fill((255, 0, 0))
    # Uncomment this line if you have RGBW/GRBW NeoPixels
    # pixels.fill((255, 0, 0, 0))
    pixels.show()
    time.sleep(1)

    # Comment this line out if you have RGBW/GRBW NeoPixels
    pixels.fill((0, 255, 0))
    # Uncomment this line if you have RGBW/GRBW NeoPixels
    # pixels.fill((0, 255, 0, 0))
    pixels.show()
    time.sleep(1)

    # Comment this line out if you have RGBW/GRBW NeoPixels
    pixels.fill((0, 0, 255))
    # Uncomment this line if you have RGBW/GRBW NeoPixels
    # pixels.fill((0, 0, 255, 0))
    pixels.show()
    time.sleep(1)

    rainbow_cycle(0.001)  # rainbow cycle with 1ms delay per step

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

#!/usr/bin/env python3
# coding: utf8

from time import sleep
import board
import adafruit_dht

dhtDevice = adafruit_dht.DHT11(board.D17)
# dhtDevice = adafruit_dht.DHT11(board.D18)
# dhtDevice = adafruit_dht.DHT11(board.D18, use_pulseio=False)

print('开始读取温湿度 🌡 💦')

# once
def once():
  try:
    temperature_c = dhtDevice.temperature
    temperature_f = temperature_c * (9 / 5) + 32
    humidity = dhtDevice.humidity
    # print("Temperature: {:.1f} °F / {:.1f} °C".format(temperature_f, temperature_c))
    print("🌡 华氏温度 Temperature: {:.1f} °F ".format(temperature_f))
    print("🌡 摄氏温度 Temperature: {:.1f} °C".format(temperature_c))
    print("💦 湿度 Humidity: {}% ".format(humidity))
  except KeyboardInterrupt:
    print('Ctrl + C 退出 ✅')
  except RuntimeError as error:
    print("error =", error, error.args[0])
    pass
  except Exception as error:
    # dhtDevice.exit()
    raise error
  finally:
    sleep(2.0)
    dhtDevice.exit()
    # cleanup
    print('clear 🚀')

# infinite loop
def infinite():
  while True:
    try:
      temperature_c = dhtDevice.temperature
      temperature_f = temperature_c * (9 / 5) + 32
      humidity = dhtDevice.humidity
      # print("Temperature: {:.1f} °F / {:.1f} °C".format(temperature_f, temperature_c))
      print("🌡 华氏温度 Temperature: {:.1f} °F ".format(temperature_f))
      print("🌡 摄氏温度 Temperature: {:.1f} °C".format(temperature_c))
      print("💦 湿度 Humidity: {}% ".format(humidity))
    except KeyboardInterrupt:
      print('Ctrl + C 退出 ✅')
    except RuntimeError as error:
      print("error =", error, error.args[0])
      pass
    except Exception as error:
      # dhtDevice.exit()
      raise error
    finally:
      sleep(2.0)
      dhtDevice.exit()
      # cleanup
      print('clear 🚀')

once()
# infinite()

"""
https://www.cnblogs.com/xgqfrms/p/17406481.html

https://stackoverflow.com/questions/74167188/get-rid-of-lost-access-to-message-queue-in-a-simple-python-script/76264450#76264450

"""

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

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

API docs

Adafruit CircuitPython NeoPixel

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

https://pypi.org/project/adafruit-circuitpython-neopixel/

https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel

https://github.com/adafruit/Adafruit_NeoPixel

WS2812B

https://cdn-shop.adafruit.com/datasheets/WS2812.pdf

https://www.adafruit.com/category/168

CircuitPython

https://github.com/adafruit/circuitpython

https://circuitpython.org/

https://circuitpython.org/downloads?manufacturers=Raspberry+Pi

image

https://circuitpython.org/board/raspberry_pi_pico/

Raspberry Pi 3 Model B

Micro USB power supply (2.1 A)

Upgraded switched Micro USB power source up to 2.5A

5V/2.5A 电源

https://www.raspberrypi.com/products/raspberry-pi-3-model-b/

https://www.raspberrypi.com/products/

refs

bugs

https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/issues/151

https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/issues/153

https://github.com/rpi-ws281x/rpi-ws281x-python/issues/95



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-05-10 13:22  xgqfrms  阅读(75)  评论(31编辑  收藏  举报