RGB LED

来自Labplus盛思维基百科
Tangliufeng讨论 | 贡献2017年12月25日 (一) 11:08的版本 Arduino示例
跳转至: 导航搜索
黑色传感器最终版12.20-02.png

概述

采用LED专用主控芯片WS2812,支持单总线控制,仅需一根管脚即可控制所有LED,并且模块支持级联控制,可以多个模块同时控制,不占用管脚资源。可实现256级亮度显示,16777216种颜色的全真色彩显示颜色。

技术参数

  • 工作电压:3.3Vor5V
  • 通讯方式:单总线控制
  • 功 率:0.3W/颗
  • 灰度等级:256级
  • 16777216种颜色的全真色彩
  • 支持级联控制
  • 模块尺寸:24x46x7.5mm

引脚定义

VCC 电源
NC 空脚
DI 控制数据信号输入
GND
VCC 电源
NC 空脚
DO 控制数据信号输出
GND

当多个模块需要级联时,把上一个模块的DO连接至下个模块的DI!

使用教程

连接示意图

Arduino示例

Arduino库 Adafruit NeoPixel 点击下载

// This is a demonstration on how to use an input device to trigger changes on your neo pixels.
// You should wire a momentary push button to connect from ground to a digital IO pin.  When you
// press the button it will change to a new pixel animation.  Note that you need to press the
// button once to start the first animation!

#include "Adafruit_NeoPixel.h"



#define PIXEL_PIN    7    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT  10    //级联数量

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);



void setup() {

  strip.setBrightness(50);     //调亮度
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

}

void loop() {
	for(uint16_t num1=0;num1<PIXEL_COUNT;num1++)
	{
		strip.setPixelColor(num1,255,0,0);  //设置红色
	}
	 strip.show();   //显示
	 delay(500);
	 for(uint16_t num1=0;num1<PIXEL_COUNT;num1++)
	{
		strip.setPixelColor(num1,0,255,0);   //设置绿色
	}
	 strip.show();   //显示
	 delay(500);
	  for(uint16_t num1=0;num1<PIXEL_COUNT;num1++)
	{
		strip.setPixelColor(num1,0,0,255);    //设置蓝色
	}
	 strip.show();    //显示
	 delay(500);

	//rainbow(10);
	//rainbowCycle(8);
	//theaterChaseRainbow(100);
}

void startShow(int i) {
  switch(i){
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 50);  // Blue
            break;
    case 4: theaterChase(strip.Color(127, 127, 127), 50); // White
            break;
    case 5: theaterChase(strip.Color(127,   0,   0), 50); // Red
            break;
    case 6: theaterChase(strip.Color(  0,   0, 127), 50); // Blue
            break;
    case 7: rainbow(20);
            break;
    case 8: rainbowCycle(20);
            break;
    case 9: theaterChaseRainbow(50);
            break;
  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

MicroPython示例

from microbit import *
import neopixel
import math
display.off()

np=neopixel.NeoPixel(pin0,4)      #pin0为控制引脚,级联4个RGB


while True:
    for i  in range(4):
        np[i]=  (255,0,0)               #设置4颗灯珠显示颜色,颜色为红
    np.show()                           #刷新显示颜色
    for i  in range(4):
        np[i]=  (0,255,0)                #设置4颗灯珠显示颜色,颜色为绿
    sleep(1000)                
    np.show()                           #刷新显示颜色
    for i  in range(4):        
        np[i]=  (0,0,255)               #设置4颗灯珠显示颜色,颜色为蓝
    sleep(1000)
    np.show()                            #刷新显示颜色
    sleep(1000)

   

图形化示例