LIVE MINI ESP32开发板教程系列(四)NeoPixel + ws2812b实现炫彩显示

  • 实验材料
  • WS2812b简介
  • LIVE MINI ESP32开发板引脚图
  • 硬件连接
    • 测试时连线图
    • 独立使用时的连线图
  • 库安装
    • 方式一:直接用`管理库`进行安装
    • 方式二:安装`ZIP`包
  • 源代码
    • 单纯彩虹效果显示源代码
    • 带按键可进行显示效果切换的源代码
      • 原理图
      • 源代码
  • 实物图

实验材料

LIVE MINI ESP32 *1
WS2812b灯带 (其实不限款式~)
四脚按键 *1 (可选)
杜邦线若干
5V电源(本文用的是一个充电宝拆的~,因为笔者的灯带是5v的,如果你的是12V款,换成12V电源即可)

WS2812b简介

WS2812B是一款常用的RGB灯珠。市面上占有率很高。一米30灯珠的灯带某宝可能也就7、8元左右。其内部是自带芯片的。驱动器来比以前单纯的led灯珠要方便很多。

LIVE MINI ESP32开发板引脚图

硬件连接

  • WS2812b灯带是有方向的,以8位的灯带为例,可以发现灯带两端都是有外接的导线的。但是仔细观察会发现标注略有不同,一侧为GND、VCC、DIN、GND,而另一侧为GND、VCC、DOUT、GND。其中DINDOUT标识了灯带的使用方向。多条灯带串联使用的时候务必注意连接的方向。每一级的out都要和in端相连。
  • 调试和独立使用的时候供电会有所不同,所以硬件连接稍有区别。请根据自己的实际情况选择相应的硬件连接方式。

测试时连线图

测试时一般都需要将ESP32芯片通过安卓线接到电脑的USB口,此时芯片可以通过安卓供电。

独立使用时的连线图

独立使用的时候注意要把5V的电源接入ESP32芯片的VCC

库安装

方式一:直接用管理库进行安装

  • 在菜单栏选择项目->加载库->管理库
  • 在搜索栏键入neopixel回车后稍等即可见到Adafruit NeoPixel,点击install进行安装。

方式二:安装ZIP

有的时候方式一的安装会受到网络状态影响,进而导致安装失败。所以可以下载ZIP包。这样安装成功率高些,但是比方法一稍微复杂丢丢。
Adafruit NeoPixel的github下载地址

  • 在菜单栏选择项目->加载库->添加.ZIP库...

源代码

该源代码是在Neopixel的buttoncycler示例的基础上进行更改的。分为两个测试例程:第一个为没有按键的单纯彩虹色显示效果测试;第二个为通过按键进行显示效果的切换。

单纯彩虹效果显示源代码

// Simple demonstration on using an input device to trigger changes on your
// NeoPixels. Wire a momentary push button to connect from ground to a
// digital IO pin. When the button is pressed it will change to a new pixel
// animation. Initial state has all pixels off -- press the button once to
// start the first animation. As written, the button does not interrupt an
// animation in-progress, it works only when idle.#include <Adafruit_NeoPixel.h>
#ifdef __AVR__#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif// Digital IO pin connected to the button. This will be driven with a
// pull-up resistor so the switch pulls the pin to ground momentarily.
// On a high -> low transition the button press logic will execute.
//#define BUTTON_PIN   2 //这个简单彩虹显示的例子里没有用到按键所以注释掉#define PIXEL_PIN    16  // 连接灯带Din的引脚时LIVE MINI ESP32的GPIO16
#define PIXEL_COUNT 8  // 测试时使用的是8颗灯珠的ws2812b灯带盘。你可以根                 //据你手头的灯带上的灯珠数量进行修改:比如16颗就                //改成16。32颗就改成32// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + 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() {pinMode(BUTTON_PIN, INPUT_PULLUP);strip.begin(); // Initialize NeoPixel strip object (REQUIRED)strip.show();  // Initialize all pixels to 'off' rainbow(10);//彩虹显示效果// theaterChaseRainbow(10);//可以切换成其他效果
}void loop() {}// 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 matchdelay(wait);                           //  Pause for a moment}
}// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase(uint32_t color, int wait) {for(int a=0; a<10; a++) {  // Repeat 10 times...for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...strip.clear();         //   Set all pixels in RAM to 0 (off)// 'c' counts up from 'b' to end of strip in steps of 3...for(int c=b; c<strip.numPixels(); c += 3) {strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'}strip.show(); // Update strip with new contentsdelay(wait);  // Pause for a moment}}
}// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {// Hue of first pixel runs 3 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 3*65536. Adding 256 to firstPixelHue each time// means we'll make 3*65536/256 = 768 passes through this outer loop:for(long firstPixelHue = 0; firstPixelHue < 3*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):int 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 single-argument hue variant. The result// is passed through strip.gamma32() to provide 'truer' colors// before assigning to each pixel:strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));}strip.show(); // Update strip with new contentsdelay(wait);  // Pause for a moment}
}// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {int firstPixelHue = 0;     // First pixel starts at red (hue 0)for(int a=0; a<30; a++) {  // Repeat 30 times...for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...strip.clear();         //   Set all pixels in RAM to 0 (off)// 'c' counts up from 'b' to end of strip in increments of 3...for(int c=b; c<strip.numPixels(); c += 3) {// hue of pixel 'c' is offset by an amount to make one full// revolution of the color wheel (range 65536) along the length// of the strip (strip.numPixels() steps):int      hue   = firstPixelHue + c * 65536L / strip.numPixels();uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGBstrip.setPixelColor(c, color); // Set pixel 'c' to value 'color'}strip.show();                // Update strip with new contentsdelay(wait);                 // Pause for a momentfirstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames}}
}

带按键可进行显示效果切换的源代码

这个源代码添加了一个按键。按键一端接在GPIO17,一端接地。

原理图

源代码

// Simple demonstration on using an input device to trigger changes on your
// NeoPixels. Wire a momentary push button to connect from ground to a
// digital IO pin. When the button is pressed it will change to a new pixel
// animation. Initial state has all pixels off -- press the button once to
// start the first animation. As written, the button does not interrupt an
// animation in-progress, it works only when idle.#include <Adafruit_NeoPixel.h>
#ifdef __AVR__#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif// Digital IO pin connected to the button. This will be driven with a
// pull-up resistor so the switch pulls the pin to ground momentarily.
// On a high -> low transition the button press logic will execute.
#define BUTTON_PIN   17//按键接在17引脚#define PIXEL_PIN    16  //16引脚接ws2816b的DIN#define PIXEL_COUNT 8  // 灯珠的数量// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + 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)boolean oldState = HIGH;
int     mode     = 0;    // Currently-active animation mode, 0-9void setup() {pinMode(BUTTON_PIN, INPUT_PULLUP);strip.begin(); // Initialize NeoPixel strip object (REQUIRED)strip.show();  // Initialize all pixels to 'off'
}void loop() {// Get current button state.boolean newState = digitalRead(BUTTON_PIN);// Check if state changed from high to low (button press).if((newState == LOW) && (oldState == HIGH)) {// Short delay to debounce button.delay(20);// Check if button is still low after debounce.newState = digitalRead(BUTTON_PIN);if(newState == LOW) {      // Yes, still lowif(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8switch(mode) {           // Start the new animation...case 0:colorWipe(strip.Color(  0,   0,   0), 50);    // Black/offbreak;case 1:colorWipe(strip.Color(255,   0,   0), 50);    // Redbreak;case 2:colorWipe(strip.Color(  0, 255,   0), 50);    // Greenbreak;case 3:colorWipe(strip.Color(  0,   0, 255), 50);    // Bluebreak;case 4:theaterChase(strip.Color(127, 127, 127), 50); // Whitebreak;case 5:theaterChase(strip.Color(127,   0,   0), 50); // Redbreak;case 6:theaterChase(strip.Color(  0,   0, 127), 50); // Bluebreak;case 7:rainbow(10);break;case 8:theaterChaseRainbow(50);break;}}}// Set the last-read button state to the old state.oldState = newState;
}// 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 matchdelay(wait);                           //  Pause for a moment}
}// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase(uint32_t color, int wait) {for(int a=0; a<10; a++) {  // Repeat 10 times...for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...strip.clear();         //   Set all pixels in RAM to 0 (off)// 'c' counts up from 'b' to end of strip in steps of 3...for(int c=b; c<strip.numPixels(); c += 3) {strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'}strip.show(); // Update strip with new contentsdelay(wait);  // Pause for a moment}}
}// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {// Hue of first pixel runs 3 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 3*65536. Adding 256 to firstPixelHue each time// means we'll make 3*65536/256 = 768 passes through this outer loop:for(long firstPixelHue = 0; firstPixelHue < 3*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):int 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 single-argument hue variant. The result// is passed through strip.gamma32() to provide 'truer' colors// before assigning to each pixel:strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));}strip.show(); // Update strip with new contentsdelay(wait);  // Pause for a moment}
}// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {int firstPixelHue = 0;     // First pixel starts at red (hue 0)for(int a=0; a<30; a++) {  // Repeat 30 times...for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...strip.clear();         //   Set all pixels in RAM to 0 (off)// 'c' counts up from 'b' to end of strip in increments of 3...for(int c=b; c<strip.numPixels(); c += 3) {// hue of pixel 'c' is offset by an amount to make one full// revolution of the color wheel (range 65536) along the length// of the strip (strip.numPixels() steps):int      hue   = firstPixelHue + c * 65536L / strip.numPixels();uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGBstrip.setPixelColor(c, color); // Set pixel 'c' to value 'color'}strip.show();                // Update strip with new contentsdelay(wait);                 // Pause for a momentfirstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames}}
}

实物图

我因为想接下来做个小机器音箱,所以选了两个8灯珠的圆环WS2812B并联在一起了。

LIVE MINI ESP32开发板教程系列(四)NeoPixel + ws2812b实现炫彩显示相关推荐

  1. LIVE MINI ESP32开发板教程系列(三)drv2605L模块+手机常用振动器实现117种震动效果

    LIVE MINI ESP32开发板教程系列(三)drv2605L模块+手机常用振动器实现117种震动效果 LIVE MINI ESP32引脚图 手机振动器介绍 DRV2605L模块 硬件连线图 DR ...

  2. 物联网开发笔记(75)- 使用Micropython开发ESP32开发板之控制tm1637时钟数码管显示

    一.目的 这一节我们学习如何使用我们的ESP32开发板来控制4位tm1637时钟数码管显示. 二.环境 ESP32 + 4位tm1637时钟数码管(wokwi仿真也可) + Thonny IDE + ...

  3. 哥哥教你学嵌入式 之 智芯科技 开发板 Z20K11x系列 教程(一)

    哥哥教你学嵌入式 之 智芯科技 开发板 Z20K11x系列 教程(一) 文章日志 1.写于2022/11/25(网上这块板子的教程几乎没有,呜呜呜,只得自己写了) 文章目录 1.认识开发板 2.串口相 ...

  4. 超便利!教你用ESP32开发板DIY掌上网页服务器!

    本文作者:默.默是铁熊的创客好友,经常与铁熊分享创意项目. 前段时间有个老师对我说:每到开学季,学校就要印刷学生的录取名单并进行张贴,为此学校每年都要耗费大量的人力物力.学校里面教学活动很多,传统的通 ...

  5. 【极创】arduino入门之ESP8266和ESP32开发板的arduino环境配置

    一.前言 在这个万物智联时代,廉价的ESP系列芯片与开发板成为众多开发者首选,其内包含强大的无线通讯功能,可以满足开发者们绝大部分的开发需求. 二.ESP8266与ESP32开发板的arduino环境 ...

  6. ESP32开发板开源啦 ESP32-IOT-KIT全开源物联网开发板

    鸽了已久的 ESP32开发板计划 终于赶在年前与大家见面了,本来上个月就能一睹芳容的,无奈年末好多事儿堆在一起,又碰巧手机出了问题,以前的照片全部丢失.为不影响开源效果,这期间一直在完善资料,Gith ...

  7. nodemcu引脚_一、ESP32开发板NodeMCU-32S简介

    目录 NodeMCU-32S 引脚功能 板上资源 总结功能特点 NodeMCU-32S NodeMCU-32S是基于 ESP32-32S 模组设计的一款较为经典的ESP32开发板,个人认为较为适合初学 ...

  8. 物联网开发笔记(31)- 使用Micropython开发ESP32开发板之手机扫二维码远程控制开关灯(1)

    一.目的 我们分3节讲述远程控制.这一节在我们的240x240的oled屏幕上显示二维码,然后用手机扫二维码,从开发板的TCP服务器上返回字符串. 二.环境 ESP32 + 240x240的oled彩 ...

  9. 厦门理工嵌入式开发LPC1768开发板教程

    LPC1768开发板教程 注意:本文档基于LPC1768.h头文件和EZ1768.h头文件(在文档结尾附出) 文章目录 LPC1768开发板教程 1.GPIO 1.1 设置GPIO 1.2 设置输入输 ...

  10. 物联网开发笔记(48)- 使用Micropython开发ESP32开发板之控制OLED ssd1306屏幕

    一.目的 这一节我们学习如何使用我们的ESP32开发板来控制OLED ssd1306屏幕,此处使用的是I2C协议,大家可自行百度学习一下I2C. 二.环境 ESP32 + OLED ssd1306屏幕 ...

最新文章

  1. java 静态类 安全_Java静态static工具类线程安全问题研究
  2. liunx mysql模块_linux下安装MySQLdb模块_MySQL
  3. 【深度学习】词的向量化表示
  4. PaddleOCR——申请显存不足【Allocate too much memory for the GPU memory pool, assigned 8000 MB】解决方案
  5. SSH学习-Hibernate对象生命周期管理
  6. 带你理清 Java 混乱的日志体系 - log4j、logback、log4j2、jcl、SLFJ 究竟是啥关系?
  7. mysql技术innodb存储引擎读后感_《Mysql技术内幕-InnoDB存储引擎》读书笔记 (一)...
  8. mysql中SQL查询优化方法总结
  9. express 4 简单实现自动注册路由功能
  10. 动态调用object php,PHP动态调用,大家都来谈吧
  11. 转行人工智能,不得不温故的数学基础知识
  12. docker 安全性_未来的Docker安全性
  13. 使用Metal打造令人惊叹的游戏效果
  14. 【Visual Studio 2019 C# TCP通信调试助手】
  15. 数字人民币来了!它与支付宝、微信有什么区别吗?
  16. 设置使用 CUDA/显卡 的编号
  17. c语言课程设计 选课系统,学生选课系统c语言课程设计.doc
  18. Eclipse在官网下载页面打不开
  19. SQL的SUBSTR()函数
  20. php中substr函数用法,关于substr函数的详细介绍

热门文章

  1. 《The Django Book 2.0》中文版笔记
  2. python教程百度云盘-Python 模拟登陆百度云盘实战教程
  3. 测试后台管理系统思路和方法
  4. AdminLTE的介绍与使用(详细流程)-----前端框架
  5. 计算机仿真技术交通灯设计,交通灯的设计心得体会总结
  6. c2000 电阻采样_常用超低阻值采样电阻阻值一览表
  7. 微信开发者文档学习笔记(一)
  8. 基于Python的语音识别控制系统
  9. 自定义Xshell高亮
  10. 微信小程序可滑动周日历组件