树莓派4B-Python-控制WS2812

客官°小女子只卖身不卖艺 2022-11-30 01:44 650阅读 0赞

本篇文章是使用树莓派4B点亮WS2812灯环,并实现多种颜色的变换。

树莓派4B-Python-控制WS2812B

  • WS2812
  • 参数介绍与工作原理
  • 与树莓派4B连接
  • 代码
    • 可能会出现的问题:

之前心血来潮,在本人那小电风扇上装了一个可控LED灯WS2812,只为了拥有更好的夜晚灯光视觉效果(好吧我承认是有点无聊)。

风扇

WS812

WS2812

WS2812B是一个集控制电路与发光电路于一体的智能外控LED光源,其外型与一个5050LED灯珠相同,每个元件即为一个像素点。

ws2812

参数介绍与工作原理

这里本人就不重复说明了,详情请参考以下博客/论坛等,希望对你有所帮助。
参考1

参考2

参考3

参考4

与树莓派4B连接

WS2812连接树莓派
红色———5V
蓝色———GND
黄色———GPIO18

代码

首先需要安装相关库:

  1. sudo pip install rpi-ws281x

若没有安装pip的还得先安装pip:(装了则跳过此步骤)

  1. sudo apt-get install python-pip

以下为灯光多种变换的代码,一个函数一种变换:

  1. import time
  2. from rpi_ws281x import PixelStrip, Color
  3. import argparse
  4. LED_COUNT = 16 # LED灯的个数
  5. LED_PIN = 18 # DI端接GPIO18
  6. # 以下可以不用改
  7. LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
  8. LED_DMA = 10 # DMA channel to use for generating signal (try 10)
  9. LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
  10. LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
  11. LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
  12. # 以下为LED模式变换的各个函数
  13. def colorWipe(strip, color, wait_ms=20):
  14. """一次擦除显示像素的颜色."""
  15. for i in range(strip.numPixels()):
  16. strip.setPixelColor(i, color)
  17. strip.show()
  18. time.sleep(wait_ms / 1000.0)
  19. def theaterChase(strip, color, wait_ms=50, iterations=10):
  20. """电影影院灯光风格的追逐动画."""
  21. for j in range(iterations):
  22. for q in range(3):
  23. for i in range(0, strip.numPixels(), 3):
  24. strip.setPixelColor(i + q, color)
  25. strip.show()
  26. time.sleep(wait_ms / 1000.0)
  27. for i in range(0, strip.numPixels(), 3):
  28. strip.setPixelColor(i + q, 0)
  29. def wheel(pos):
  30. """生成横跨0-255个位置的彩虹颜色."""
  31. if pos < 85:
  32. return Color(pos * 3, 255 - pos * 3, 0)
  33. elif pos < 170:
  34. pos -= 85
  35. return Color(255 - pos * 3, 0, pos * 3)
  36. else:
  37. pos -= 170
  38. return Color(0, pos * 3, 255 - pos * 3)
  39. def rainbow(strip, wait_ms=20, iterations=1):
  40. """绘制彩虹,褪色的所有像素一次."""
  41. for j in range(256 * iterations):
  42. for i in range(strip.numPixels()):
  43. strip.setPixelColor(i, wheel((i + j) & 255))
  44. strip.show()
  45. time.sleep(wait_ms / 1000.0)
  46. def rainbowCycle(strip, wait_ms=10, iterations=5):
  47. """画出均匀分布在所有像素上的彩虹."""
  48. for j in range(256 * iterations):
  49. for i in range(strip.numPixels()):
  50. strip.setPixelColor(i, wheel(
  51. (int(i * 256 / strip.numPixels()) + j) & 255))
  52. strip.show()
  53. time.sleep(wait_ms / 1000.0)
  54. def theaterChaseRainbow(strip, wait_ms=50):
  55. """旋转的彩色灯光."""
  56. for j in range(256):
  57. for q in range(3):
  58. for i in range(0, strip.numPixels(), 3):
  59. strip.setPixelColor(i + q, wheel((i + j) % 255))
  60. strip.show()
  61. time.sleep(wait_ms / 1000.0)
  62. for i in range(0, strip.numPixels(), 3):
  63. strip.setPixelColor(i + q, 0)
  64. # Main program logic follows:
  65. if __name__ == '__main__':
  66. # Process arguments
  67. parser = argparse.ArgumentParser()
  68. parser.add_argument('-c', '--clear', action='store_true', help='clear the display on exit')
  69. args = parser.parse_args()
  70. # Create NeoPixel object with appropriate configuration.
  71. strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
  72. # Intialize the library (must be called once before other functions).
  73. strip.begin()
  74. print('Press Ctrl-C to quit.')
  75. if not args.clear:
  76. print('Use "-c" argument to clear LEDs on exit')
  77. try:
  78. while True:
  79. print('Color wipe animations.')
  80. colorWipe(strip, Color(255, 255, 0)) # Red wipe
  81. colorWipe(strip, Color(0, 0, 0), 30)
  82. colorWipe(strip, Color(0, 255, 255)) # Blue wipe
  83. colorWipe(strip, Color(0, 0, 0), 30)
  84. colorWipe(strip, Color(255, 0, 255)) # Green wipe
  85. colorWipe(strip, Color(0, 0, 0), 30)
  86. print('Theater chase animations.')
  87. print('Rainbow animations.')
  88. rainbow(strip)
  89. colorWipe(strip, Color(0, 0, 0), 50)
  90. rainbowCycle(strip)
  91. colorWipe(strip, Color(0, 0, 0), 40)
  92. break
  93. while True:
  94. rainbowCycle(strip)
  95. #print('***********************')
  96. colorWipe(strip, Color(0, 0, 0), 100)
  97. except:
  98. colorWipe(strip, Color(0, 0, 0), 100)

可能会出现的问题:

问题1:提示 “strip.begin()” 该代码出错,显示错误为:

  1. RuntimeError: ws2811_init failed with code -5 (mmap() failed)

原因:直接从IDE上运行了该程序。
解决方法:从命令行上运行程序。方法如下:
①假设程序文件放在桌面上

  1. cd Desktop

②进入桌面后打开程序文件,假设文件名为123.py

  1. sudo python3 123.py

③运行时若想要退出就按Ctrl + C
问题2:
不能使用中文注释?

经过验证,其实是可以的使用中文注释的,并且根本不用删除注释再运行程序,完全不受影响

问题3:
出现ImportError: No module named _rpi_ws281x的错误提示

较大的可能是安装错了,用pip安装可能是Python2的,用pip3安装就能解决

  1. sudo pip3 install rpi-ws281x

##2020.8.29

发表评论

表情:
评论列表 (有 0 条评论,650人围观)

还没有评论,来说两句吧...

相关阅读