eagler8 发表于 2019-10-1 06:49:18

【Arduino】108种传感器模块系列实验(131)--24位WS2812环形灯板

37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手试试做实验,不管成功与否,都会记录下来---小小的进步或是搞不定的问题,希望能够抛砖引玉。

【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板

eagler8 发表于 2019-10-1 06:51:28

WS2812
是在寻求一种简单,可扩展和经济实惠的全彩LED的最新进展。红色,绿色和蓝色LED与驱动器芯片一起集成到通过单线控制的微小表面贴装封装中。它们可以单独使用,链接成更长的字符串或组装成更有趣的形状因数。基于WS2812的 LED 驱动器,使用单线控制协议,可分别寻址RGB彩色像素和色带。专用LED驱动器芯片的到来带来了可喜的缓解,减轻了微控制器的繁琐工作,使人们可以专注于应用与创造。



eagler8 发表于 2019-10-1 07:33:19


该Adafruit NeoPixel环包含24个可单独寻址的RGB LED,这些LED以紧密间隔排列,所有这些均可通过微控制器的单个数字输出进行控制。该环的外径为2.6英寸(66毫米),可以将多个环链接在一起,使其非常适合为小型机器人或可穿戴电子项目增加光泽。

功能和规格
24个可单独寻址的RGB LED(基于SK6812-或WS2812B的NeoPixels)
圆形外径2.6英寸(66毫米),内径2.05英寸(52.5毫米)
24位色彩控制(每通道8位PWM);每像素1680万种颜色
单线数字控制接口
工作电压:5 V
每个RGB LED在5 V时消耗大约50 mA的电流,红色,绿色和蓝色在全亮度下(环最大为1.2 A)
多个NeoPixel环可链接在一起

eagler8 发表于 2019-10-1 07:39:34



24位WS2812 5050 RGB LED智能全彩RGB灯环开发板, 产品(直径):86mm ,重量:6g ,电压:DC4-7V范围供电,通信接口:单线通讯

LED驱动芯片WS2812(集成在LED里面)
智能反接保护,电源反接不会损坏IC。
IC控制电路与LED点光源公用一个电源。
控制电路与RGB晶片集成在一个5050封装的元器件中,构成一个完整的外控图元点。
内置信号整形电路,任何一个图元点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
内置上电重定和掉电重定电路。
每个图元点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。
串列级联介面,能通过一根信号线完成资料的接收与解码。
任意两点传传输距离在不超过5米时无需增加任何电路。
当刷新速率30帧/秒时,级联数不小于1024点。
资料发送速度可达800Kbps。
光的颜色高度一致,性价比高。

eagler8 发表于 2019-10-1 07:43:29

5050高亮LED,内置控制芯片,仅需1个IO口即可控制多个LED
芯片内置整形电路,信号畸变不会累计,稳定显示
三基色256级亮度调剂,16万色真彩显示效果,扫描频率不低于400Hz/S
串行连级接口,能通过一根信号线完成数据的接收与解码
高亮LED,光色亮度一致性高
两端有连级接口(DIN DOUT),可以串接

WS2813B是一个集控制电路与发光电路于一体的智慧外控LED光源。其外型与一个5050LED灯珠相同,每个元件即为一个图元点。图元点内部包含了智慧数位介面资料锁存信号整形放大驱动电路,还包含有高精度的内部振荡器和12V高压可编程定电流控制部分,有效保证了图元点光的颜色高度一致。
资料协定采用单线归零码的通讯方式,图元点在上电重定以后,DIN端接受从控制器传输过来的资料,首先送过来的24bit资料被第一个图元点提取后,送到图元点内部的资料锁存器,剩余的资料经过内部整形处理电路整形放大后通过DO埠开始转发输出给下一个级联的图元点,每经过一个图元点的传输,信号减少24bit。图元点采用自动整形转发技术,使得该图元点的级联个数不受信号传送的限制,仅仅受限信号传输速度要求。
LED具有低电压驱动,环保节能,亮度高,散射角度大,一致性好,超低功率,超长寿命等优点。将控制电路集成于LED上面,电路变得更加简单,体积小,安装更加简便。

eagler8 发表于 2019-10-1 08:50:34


连接
LED环具有四个连接点。

标为GND的引脚为接地引脚,应连接至控制LED的微控制器的接地引脚,也应连接至电源的负极。

标有PWR + 5V的引脚是电源输入引脚,应连接到合适的电源。5 V的输入电压用于为环供电,当以全亮度输出白色时,环上的每个LED在5 V时可消耗高达50 mA的电流。这意味着该环最多可吸收约1.2 A电流。

标为“ 数据输入 ”的引脚是一个输入,用于接收控制LED的信号。该引脚应连接至基于SK6812 / WS281x的LED产品的微控制器输出引脚或信号输出引脚。

标有“ 数据输出 ”的引脚是可选的。它可以连接到另一个基于SK6812 / WS281x的LED产品的数据输入引脚,以形成一条链。

我们的0.1英寸排针可以用来连接环的四个针,也可以将导线直接焊接到板上。环并不与任何标头销或连接器发货。

如果在使用NeoPixels时未采取适当的预防措施,则很容易损坏它们。通常,对于基于SK6812 / WS2812B的产品,建议在接地线和电源线之间添加至少10 F的电容器,并在微控制器和信号输入引脚之间添加100Ω至1000Ω的电阻。

eagler8 发表于 2019-10-1 08:59:59

模块电原理图

eagler8 发表于 2019-10-1 09:03:33

ATTiny85与WS2812 LED环配合使用示意图

eagler8 发表于 2019-10-1 10:06:21

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目一:循环点亮24位绿色LED
Module      UNO
VCC   ——   5V
GND——   GND
DI    ——   D6
*/

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN      6 // On Trinket or Gemma, suggest changing this to 1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 24 // Popular NeoPixel ring size

// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 300 // Time (in milliseconds) to pause between pixels

void setup() {
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.

pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}

void loop() {
pixels.clear(); // Set all pixel colors to 'off'

// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for (int i = 0; i < NUMPIXELS; i++) { // For each pixel...

    // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
    // Here we're using a moderately bright green color:
    pixels.setPixelColor(i, pixels.Color(0, 150, 0));

    pixels.show();   // Send the updated pixel colors to the hardware.

    delay(DELAYVAL); // Pause before next pass through loop
}
}

eagler8 发表于 2019-10-1 10:17:28

eagler8 发表于 2019-10-1 10:33:08

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目二:循环快扫24位红绿蓝色LED流水灯
Module      UNO
VCC   ——   5V
GND——   GND
DI    ——   D6
*/

#include <FastLED.h>
#define LED_PIN   6
#define NUM_LEDS    24
CRGB leds;
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
for (int i = 0; i <= 23; i++) {
    leds = CRGB ( 0, 0, 255);
    FastLED.show();
    delay(40);
}
for (int i = 0; i <= 23; i++) {
    leds = CRGB ( 255, 0, 0);
    FastLED.show();
    delay(40);
}
for (int i = 0; i <= 23; i++) {
    leds = CRGB ( 0, 255, 0);
    FastLED.show();
    delay(40);
}
}

eagler8 发表于 2019-10-1 10:44:42


eagler8 发表于 2019-10-1 11:59:54

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目三:使用红色、绿色和蓝色三种参数将任何LED设置为任何颜色
Module      UNO
VCC   ——   5V
GND——   GND
DI    ——   D6
*/

#include <FastLED.h>
#define LED_PIN   6
#define NUM_LEDS    24
CRGB leds;
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

}
void loop() {

leds = CRGB(255, 0, 0);
FastLED.show();
delay(500);
leds = CRGB(0, 255, 0);
FastLED.show();
delay(500);
leds = CRGB(0, 0, 255);
FastLED.show();
delay(500);
leds = CRGB(150, 0, 255);
FastLED.show();
delay(500);
leds = CRGB(255, 200, 20);
FastLED.show();
delay(500);
leds = CRGB(85, 60, 180);
FastLED.show();
delay(500);
leds = CRGB(50, 255, 20);
FastLED.show();
delay(500);
}

eagler8 发表于 2019-10-1 12:07:16

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目四:流水灯变幻彩虹灯
Module      UNO
VCC   ——   5V
GND——   GND
DI    ——   D6
*/

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define BRIGHTNESS 24

Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
strip.setBrightness(BRIGHTNESS);
strip.begin();
strip.show();
}

void loop() {
colorWipe(strip.Color(150, 0, 0), 50); // Red
colorWipe(strip.Color(0, 150, 0), 50); // Green
colorWipe(strip.Color(0, 0, 150), 50); // Blue
colorWipe(strip.Color(150, 150, 150), 50); // BlueWite
rainbowCycle(1);

}

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);
}
}

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);
}
}

uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

eagler8 发表于 2019-10-1 12:20:44

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
项目五:循环流水变幻呼吸灯
Module      UNO
VCC   ——   5V
GND——   GND
DI    ——   D6
*/

// NeoPixel test program showing use of the WHITE channel for RGBW
// pixels only (won't look correct on regular RGB NeoPixel strips).

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN   6

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT24

// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 50

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB   Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB   Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

void setup() {
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.

strip.begin();         // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show();            // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}

void loop() {
// Fill along the length of the strip in various colors...
colorWipe(strip.Color(255,   0,   0)   , 50); // Red
colorWipe(strip.Color(0, 255,   0)   , 50); // Green
colorWipe(strip.Color(0,   0, 255)   , 50); // Blue
colorWipe(strip.Color(0,   0,   0, 255), 50); // True white (not RGB white)

whiteOverRainbow(75, 5);

pulseWhite(5);

rainbowFade2White(3, 3, 1);
}

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //Set pixel's color (in RAM)
    strip.show();                        //Update strip to match
    delay(wait);                           //Pause for a moment
}
}

void whiteOverRainbow(int whiteSpeed, int whiteLength) {

if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;

int      head          = whiteLength - 1;
int      tail          = 0;
int      loops         = 3;
int      loopNum       = 0;
uint32_t lastTime      = millis();
uint32_t firstPixelHue = 0;

for(;;) { // Repeat forever (or until a 'break' or 'return')
    for(int i=0; i<strip.numPixels(); i++) {// For each pixel in strip...
      if(((i >= tail) && (i <= head)) ||      //If between head & tail...
         ((tail > head) && ((i >= tail) || (i <= head)))) {
      strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
      } else {                                             // else set rainbow
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
      }
    }

    strip.show(); // Update strip with new contents
    // There's no delay here, it just runs full-tilt until the timer and
    // counter combination below runs out.

    firstPixelHue += 40; // Advance just a little along the color wheel

    if((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
      if(++head >= strip.numPixels()) {      // Advance head, wrap around
      head = 0;
      if(++loopNum >= loops) return;
      }
      if(++tail >= strip.numPixels()) {      // Advance tail, wrap around
      tail = 0;
      }
      lastTime = millis();                   // Save time of last movement
    }
}
}

void pulseWhite(uint8_t wait) {
for(int j=0; j<256; j++) { // Ramp up from 0 to 255
    // Fill entire strip with white at gamma-corrected brightness level 'j':
    strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
    strip.show();
    delay(wait);
}

for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
    strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
    strip.show();
    delay(wait);
}
}

void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {
int fadeVal=0, fadeMax=100;

// Hue of first pixel runs 'rainbowLoops' complete loops through the color
// wheel. Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to rainbowLoops*65536, using steps of 256 so we
// advance around the wheel at a decent clip.
for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536;
    firstPixelHue += 256) {

    for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...

      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());

      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the three-argument variant, though the
      // second value (saturation) is a constant 255.
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
      255 * fadeVal / fadeMax)));
    }

    strip.show();
    delay(wait);

    if(firstPixelHue < 65536) {                              // First loop,
      if(fadeVal < fadeMax) fadeVal++;                     // fade in
    } else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,
      if(fadeVal > 0) fadeVal--;                           // fade out
    } else {
      fadeVal = fadeMax; // Interim loop, make sure fade is at max
    }
}

for(int k=0; k<whiteLoops; k++) {
    for(int j=0; j<256; j++) { // Ramp up 0 to 255
      // Fill entire strip with white at gamma-corrected brightness level 'j':
      strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
      strip.show();
    }
    delay(1000); // Pause 1 second
    for(int j=255; j>=0; j--) { // Ramp down 255 to 0
      strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
      strip.show();
    }
}

delay(500); // Pause 1/2 second
}
页: [1] 2 3
查看完整版本: 【Arduino】108种传感器模块系列实验(131)--24位WS2812环形灯板