使用树莓派控制led灯板 ws2812

使用树莓派控制led灯板ws2812

推荐一个库 Adafruit_NeoPixel

这里是地址:https://github.com/adafruit/Adafruit_NeoPixel

这个原本是在Arduino上控制led的库
github上的介绍:

Arduino library for controlling single-wire-based LED pixels and strip such as the Adafruit 60 LED/meter Digital LED strip, the Adafruit FLORA RGB Smart Pixel, the Adafruit Breadboard-friendly RGB Smart Pixel, the Adafruit NeoPixel Stick, and the Adafruit NeoPixel Shield.

After downloading, rename folder to ‘Adafruit_NeoPixel’ and install in Arduino Libraries folder. Restart Arduino IDE, then open File->Sketchbook->Library->Adafruit_NeoPixel->strandtest sketch.

Compatibility notes: Port A is not supported on any AVR processors at this time

Supported chipsets
We have included code for the following chips - sometimes these break for exciting reasons that we can’t control in which case please open an issue!

AVR ATmega and ATtiny (any 8-bit) - 8 MHz, 12 MHz and 16 MHz
Teensy 3.x and LC
Arduino Due
Arduino 101
ATSAMD21 (Arduino Zero/M0 and other SAMD21 boards) @ 48 MHz
ATSAMD51 @ 120 MHz
Adafruit STM32 Feather @ 120 MHz
ESP8266 any speed
ESP32 any speed
Nordic nRF52 (Adafruit Feather nRF52), nRF51 (micro:bit)
Check forks for other architectures not listed here!

用在树莓派(python)上面也是一样的:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-skmaMHBm-1603779810385)(https://cdn-learn.adafruit.com/assets/assets/000/063/650/medium800/leds_NeoPixel_Single_LED_Red.jpg?1539811956)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-peSFGw36-1603779810398)(https://cdn-learn.adafruit.com/assets/assets/000/063/651/medium800/leds_NeoPixel_All_LEDs_Green.jpg?1539811967)]

下面是我做的成品地址:https://www.bilibili.com/video/av47393296

欢迎来讨论

下面是代码哦:

import time
import board
import neopixel

pixel_pin = board.D18  #定义针脚
num_pixels = 30 #定义LED个数
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 == neopixel.RGB or ORDER == 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:
    
    pixels.fill((255, 0, 0))
    pixels.show()
    time.sleep(1)
    pixels.fill((0, 255, 0))
    pixels.show()
    time.sleep(1)
    pixels.fill((0, 0, 255))
    pixels.show()
    time.sleep(1)
    rainbow_cycle(0.001)   
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
控制ws2812灯带,需要用到树莓派上的GPIO口,以及相关的软件库。以下是一个简单的操作流程: 1. 连接ws2812灯带到树莓派上,可以使用杜邦线将灯带的DI引脚连接到树莓派的GPIO口(例如GPIO18)上。 2. 安装控制ws2812灯带的Python库,例如rpi_ws281x。可以使用pip install rpi_ws281x命令进行安装。 3. 编写控制ws2812灯带的Python脚本,使用rpi_ws281x库提供的函数进行控制。例如: ```python import time from rpi_ws281x import * # LED strip configuration: LED_COUNT = 8 # Number of LED pixels. LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!). 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 # Define the colors of the LED pixels. colors = [ Color(255, 0, 0), # Red Color(0, 255, 0), # Green Color(0, 0, 255), # Blue ] # Create a LED strip object. strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL) strip.begin() # Loop through the colors and display them on the LED strip. while True: for color in colors: for i in range(strip.numPixels()): strip.setPixelColor(i, color) strip.show() time.sleep(1) ``` 这个脚本使用了rpi_ws281x库提供的函数,通过循环控制LED灯带的颜色变化,可以通过修改colors列表中的颜色值来改变LED灯带的显示效果。 4. 运行Python脚本,控制LED灯带的显示效果。可以使用python命令运行脚本,例如: ```shell python led_control.py ``` 这样就可以实现通过树莓派控制ws2812灯带的效果了。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值