【雕爷学编程】Arduino动手做(138)---64位WS2812点阵屏模块
37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
知识点:WS2812B
是一个集控制电路与发光电路于一体的智能外控LED光源。其外型与一个5050LED灯珠相同,每个元件即为一个像素点。像素点内部包含了智能数字接口数据锁存信号整形放大驱动电路,还包含有高精度的内部振荡器和12V高压可编程定电流控制部分,有效保证了像素点光的颜色高度一致。
数据协议采用单线归零码的通讯方式,像素点在上电复位以后,DIN端接受从控制器传输过来的数据,首先送过来的24bit数据被第一个像素点提取后,送到像素点内部的数据锁存器,剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点,每经过一个像素点的传输,信号减少24bit。像素点采用自动整形转发技术,使得该像素点的级联个数不受信号传送的限制,仅仅受限信号传输速度要求。
LED具有低电压驱动,环保节能,亮度高,散射角度大,一致性好,超低功率,超长寿命等优点。将控制电路集成于LED上面,电路变得更加简单,体积小,安装更加简便。
WS2812B主要特点
智能反接保护,电源反接不会损坏IC。
IC控制电路与LED点光源公用一个电源。
控制电路与RGB芯片集成在一个5050封装的元器件中,构成一个完整的外控像素点。
内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
内置上电复位和掉电复位电路。
每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。
串行级联接口,能通过一根信号线完成数据的接收与解码。
任意两点传传输距离在不超过5米时无需增加任何电路。
当刷新速率30帧/秒时,级联数不小于1024点。
数据发送速度可达800Kbps。
光的颜色高度一致,性价比高。
主要应用领域
LED全彩发光字灯串,LED全彩模组, LED全彩软灯条硬灯条,LED护栏管。
LED点光源,LED像素屏,LED异形屏,各种电子产品,电器设备跑马灯。
WS2812全彩矩阵LED模块(64位灯珠)
尺寸:6.5*6.5cm
芯片:WS2812B(内置于LED)
LED:5050封装RGB全彩高亮
电压:5V
端口:数字
平台:Arduino 单片机
控制方式:内置控制芯片,只需一个IO口即可控制
模块参考电原理图
实验涉及到的几个WS2812B相关库
安装FastLED库,工具—管理库—搜索FastLED—安装
安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
安装Adafruit_NeoPixel库,
下载https://learn.adafruit.com/adafr ... ibrary-installation
Arduino实验开源代码
/* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 安装NeoPixel库,工具—管理库—搜索NeoPixel—安装 安装Adafruit_NeoPixel库, 下载https://learn.adafruit.com/adafr ... ibrary-installation 程序之一:简单蓝色流水灯 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> //16兆赫Adafruit饰品所需 #endif // Arduino上的哪个插脚与NeoPixels相连? #define PIN 6 // Arduino上有多少个LED? #define NUMPIXELS 64 Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); #define DELAYVAL 30 //像素之间暂停的时间(毫秒) void setup() { #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) clock_prescale_set(clock_div_1); #endif // 特定代码结束 pixels.begin(); // 初始化neopxel strip对象(必需) } void loop() { pixels.clear(); //将所有像素颜色设置为“关闭” //一串中的第一个新混合物是0,第二个是1,一直往上 //像素数减1 for (int i = 0; i < NUMPIXELS; i++) { // 对于每个像素...... //Color()接受RGB值,从0,0,0到255,255,255 //这里我们使用的是中等明亮的蓝色: pixels.setPixelColor(i, pixels.Color(0, 0, 150)); pixels.show(); // 将更新的像素颜色发送到硬件 delay(DELAYVAL); // 在下一个通过循环之前暂停 } }
Arduino实验场景图
Arduino实验开源代码
粉色单灯流水灯
/* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 安装NeoPixel库,工具—管理库—搜索NeoPixel—安装 安装Adafruit_NeoPixel库, 下载https://learn.adafruit.com/adafr ... ibrary-installation 程序之二:粉色单灯流水灯 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include <Adafruit_NeoPixel.h> #define PIN 6 #define MAX_LED 64 #define ADD true #define SUB false int val = 0; boolean stat = ADD; Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, PIN, NEO_RGB + NEO_KHZ800 ); void setup() { strip.begin(); //初始化Adafruit_NeoPixel; strip.show(); //显示所有LED为关状态; } void loop() { uint8_t i,a=0; uint32_t color = strip.Color(0, 150, 150); //选择所显示的颜色 while(a<65) { for(i=0;i<64;i++) { if(i==a) strip.setPixelColor(i, color); //第几个LED点亮; else strip.setPixelColor(i, 0); //使其他LED全灭; } strip.show(); //是LED显示所选的颜色; delay(50); //延时50ms; a++; } }
Arduino实验开源代码
四色交替流水灯
/* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 安装FastLED库,工具—管理库—搜索FastLED—安装 程序之三:四色交替流水灯 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include <FastLED.h> #define LED_PIN 6 #define NUM_LEDS 64 CRGB leds[NUM_LEDS]; void setup() { FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); } void loop() { for (int i = 0; i <= 63; i++) { leds = CRGB ( 0, 0, 200); FastLED.show(); delay(33); } for (int i = 63; i >= 0; i--) { leds = CRGB ( 89, 0, 0); FastLED.show(); delay(33); } for (int i = 0; i <= 63; i++) { leds = CRGB ( 0, 89, 0); FastLED.show(); delay(33); } for (int i = 63; i >= 0; i--) { leds = CRGB ( 89, 0, 200); FastLED.show(); delay(33); } }
Arduino实验场景图
Arduino实验开源代码
RGB传输测试满屏变幻彩灯
/* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 安装NeoPixel库,工具—管理库—搜索NeoPixel—安装 安装Adafruit_NeoPixel库, 下载https://learn.adafruit.com/adafr ... ibrary-installation 程序之五:RGB传输测试满屏变幻彩灯 实验接线 Module UNO VCC —— 3.3V 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? // 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_COUNT 60 // 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_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 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 }
程序之五:RGB传输测试满屏变幻彩灯 视频(52秒)
链接:https://v.youku.com/v_show/id_XNDU2ODQ2MDI0NA==.html
实验开源仿真编程(Linkboy V4.62)
Arduino实验开源代码
黑客帝国绿色流水灯
/* 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验一百三十八:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏 安装FastLED库,工具—管理库—搜索FastLED—安装 安装Adafruit_NeoPixel库, 下载https://learn.adafruit.com/adafr ... ibrary-installation 程序之四:黑客帝国绿色流水灯 实验接线 Module UNO VCC —— 3.3V GND —— GND DI —— D6 */ #include <Adafruit_NeoPixel.h> #define PIN 6 #define MAX_LED 64 #define ADD true #define SUB false int val = 0; boolean stat = ADD; Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, PIN, NEO_RGB + NEO_KHZ800 ); void setup() { strip.begin(); strip.show(); } void loop() { uint8_t i, a = 0; uint32_t color = strip.Color(190, 50, 0); while (a < 65) { for (i = 0; i < 64; i++) { if (i == a) strip.setPixelColor(i, color); else strip.setPixelColor(i, 0); } strip.show(); delay(25); a++; } }
Arduino实验场景图
黑客帝国绿色流水灯