Arduino UNO驱动MPR121接近电容式触摸传感器控制WS2812彩灯

ops/2024/9/23 10:24:58/

简介

  MPR121芯片功能强大可用作触摸,电容检测,驱动LED等等.在低速扫描下可以将功 耗降低到8μA,可以处理多达12个独立的触摸板。支持I2C,几乎可以用任何微控 制器连接。可以使用ADDR引脚选择4个地址中的一个,一个I2C2线总线上共有48 个电容触摸板。使用该芯片比使用模拟输入进行电容式感应要容易得多,并且可以 配置灵敏度。

参数特性

  1. 工作电压:2.5V-3.6VDC
  2. 采样频率:采样间隔时间为16ms时,电源电流为29μA,停止模式电流3μA
  3. 输出接口:12个电容传感输入
  4. 输入接口:8个输入为LED驱动器和GPIO多功能
  5. 完整的触摸检测:每个传感输入的自动配置自动校准
  6. 触摸/释放阈值和去抖动以进行触摸检测
  7. I2C接口,带中断输出
  8. 工作温度范围:-40℃至+85℃
  9. 尺寸:30.5*20.6mm

引脚定义

名称描述
IRQ开路集电极中断输出引脚,低电平激活
SCLI 2C时钟
SDAI2C数据
ADDRI 2C地址选择输入引脚。将ADDR引脚连接到VSS、VDD、SDA或SCL线,得到I2C地址分别为0x5A、0x5B、0x5C和0x5D
VREG内部调节器节点
VSS
REXT外部电阻器-将一个75 kΩ1%的电阻器连接到VSS,以设置内部参考电流
ELE0 - 11电极0 - 11
VDD电源输入

典型应用实例及电极图案

硬件准备

Arduino UNO板、MPR121电容触摸模块、WS2812模块。

引脚接线

MPR121 / WS2812Arduino UNO
MPR121-SCLA5
MPR121-SDAA4
WS2812-IOIO8
MPR121-3.3V3.3V
WS2812-5V5V
GNDGND

示例代码

#include <Wire.h>
#include "Adafruit_MPR121.h"
#include "FastLED.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif#define NUM_LEDS 9 
#define LED_DT 8 
#define LED_TYPE WS2812 
#define COLOR_ORDER RGBCRGB leds[NUM_LEDS]; 
CHSV myHSVcolor(45, 255, 200);// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint8_t parseNum = 0;
uint8_t paletteNum = 0;
uint8_t startIndex = 0;
uint8_t chromatism = 50;
uint16_t lasttouched = 0;
uint16_t currtouched = 0;void setup() {LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);FastLED.setBrightness(128);Serial.begin(9600);while (!Serial) { // needed to keep leonardo/micro from starting too fast!delay(10);}Serial.println("Adafruit MPR121 Capacitive Touch sensor test");// Default address is 0x5A, if tied to 3.3V its 0x5B// If tied to SDA its 0x5C and if SCL then 0x5Dif (!cap.begin(0x5A)) {Serial.println("MPR121 not found, check wiring?");while (1);}Serial.println("MPR121 found!");
}void loop() {// Get the currently touched padscurrtouched = cap.touched();for (uint8_t i = 0; i < 12; i++) {// it if *is* touched and *wasnt* touched before, alert!if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {switch (i) {case 0:stateLed_ON(parseNum++);if (parseNum > 10) {parseNum = 0;}break;case 1: stateLed_OFF(); break;case 2:dynamicLED(paletteNum, startIndex);paletteNum++;if (paletteNum > 1) {paletteNum = 0;}break;}}}// reset our statelasttouched = currtouched;// comment out this line for detailed data from the sensor!return;// debugging info, whatSerial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);Serial.print("Filt: ");for (uint8_t i = 0; i < 12; i++) {Serial.print(cap.filteredData(i)); Serial.print("\t");}Serial.println();Serial.print("Base: ");for (uint8_t i = 0; i < 12; i++) {Serial.print(cap.baselineData(i)); Serial.print("\t");}Serial.println();// put a delay so it isn't overwhelmingdelay(100);
}void stateLed_ON(uint8_t parseNum) {   //点亮单色调颜色switch (parseNum) {case 0: fill_solid(leds, NUM_LEDS, CRGB::Crimson);              FastLED.show(); break;case 1: fill_solid(leds, NUM_LEDS, CRGB::Aqua);                 FastLED.show(); break;case 2: fill_solid(leds, NUM_LEDS, CRGB::Amethyst);             FastLED.show(); break;case 3: fill_solid(leds, NUM_LEDS, CRGB::Blue);                 FastLED.show(); break;case 4: fill_solid(leds, NUM_LEDS, CRGB::Chartreuse);           FastLED.show(); break;case 5: fill_solid(leds, NUM_LEDS, CRGB::DarkOrange);           FastLED.show(); break;case 6: fill_solid(leds, NUM_LEDS, CRGB::DeepPink);             FastLED.show(); break;case 7: fill_solid(leds, NUM_LEDS, CRGB::GhostWhite);           FastLED.show(); break;case 8: fill_solid(leds, NUM_LEDS, CRGB::Gold);                 FastLED.show(); break;case 9: fill_solid(leds, NUM_LEDS, CRGB::GreenYellow);          FastLED.show(); break;case 10: fill_solid(leds, NUM_LEDS, CRGB::MediumSpringGreen);   FastLED.show(); break;}FastLED.show();
}void stateLed_OFF() {fill_solid(leds, NUM_LEDS, CRGB::Black);FastLED.show();
}void dynamicLED(uint8_t paletteNum, uint8_t colorIndex) {switch (paletteNum) {case 0: gradientflowingLED(); break;case 1: FillLEDsFromPaletteColors(colorIndex); break;}
}void gradientflowingLED() {for (int i = 0; i < NUM_LEDS; i++) {fill_solid(leds + i, 1, myHSVcolor);FastLED.show();myHSVcolor.h += 10;delay(50);fill_solid(leds + i, 1, CRGB::Black);FastLED.show();delay(50);}for (int i = NUM_LEDS; i > 0; i--) {fill_solid(leds + i, 1, myHSVcolor);FastLED.show();myHSVcolor.h += 10;delay(50);fill_solid(leds + i, 1, CRGB::Black);FastLED.show();delay(50);}
}void FillLEDsFromPaletteColors(uint8_t colorIndex)
{for (int i = 0; i < chromatism; i++) {colorIndex++;for ( int j = 0; j < NUM_LEDS; ++j) {leds[j] = ColorFromPalette(RainbowColors_p, colorIndex, 255, LINEARBLEND);colorIndex += 3;FastLED.show();FastLED.delay(10);}}fill_solid(leds, NUM_LEDS, CRGB::Black);FastLED.show();
}

示例演示


http://www.ppmy.cn/ops/13601.html

相关文章

JavaWeb前端/后端开发规范——接口文档概述及YApi平台的使用

前言&#xff1a; 整理下笔记&#xff0c;打好基础&#xff0c;daydayup!!! 接口文档 什么是接口文档&#xff1f; 目前主流的开发模式为前后端分离式开发&#xff0c;为了方便前后端的对接&#xff0c;就需要使用接口文件进行统一规范。 接口文档记载什么信息&#xff1f; 1&…

【大语言模型LLM】-如何使用大语言模型提高工作效率?

关于作者 行业&#xff1a;人工智能训练师/LLM 学者/LLM微调乙方PM发展&#xff1a;微调大模型训练/大模型增强检索RAG分享国内大模型前沿工作记录&#xff0c;共同成长&#xff0c;欢迎关注交流… 大语言模型LLM基础-系列文章 大语言模型LLM】-大语言模型如何编写Prompt?持…

Mediator 中介者

意图 使用一个中介者对象来封装一系列的对象交互。中介者使各个对象不需要显式地互相引用&#xff0c;从而使其耦合松散&#xff0c;而且可以独立的改变他们之间的交互。 结构 Mediator&#xff08;中介者&#xff09;定义一个接口用于各同事&#xff08;Colleague&#xff0…

云服务器部署Springboot项目

前端项目打包 修改ip地址 在控制台输入npm run build:prod 会产生dist文件 将dist文件中的内容移动至/usr/local/nginx/html目录下 后端项目打包 修改ip地址 执行clean操作 执行install操作 将生成的target文件中的jar包移动至/usr/local/src目录下 启动 注意⚠️&#xff…

dedebiz文章内页自动给正文图片加了style样式怎么去掉

dede文章内页自动给正文图片加了style样式怎么去掉 打开&#xff1a;/system/archive/archives.class.php 查找&#xff1a;box-shadow&#xff0c;找到如下&#xff1a; margin:20px 0;box-shadow:0 1px 2px rgba(0,0,0,.1)改成下面这样&#xff1a; box-shadow:0 0px 0px rgb…

python爬虫之爬取文本内容(2)

一、基本案例 #注意&#xff1a;需要将requests包换成2.27.1 #中文编码gbk也可能是utf-8 import requests #from bs4 import BeautifulSoupif __name__ __main__:url https://www.biqg.cc/book/6909/1.html#目标访问网站url#伪装头信息的引入header {"User-Agent"…

【注释和反射】获取class类实例的方法

目录 一、获取一个类的Class对象的几种方法 代码 二、哪些类型可以有Class对象&#xff1f; 代码 一、获取一个类的Class对象的几种方法 Class对象是访问类元数据的入口&#xff0c;通过它可以获取类的名称、方法、字段、构造器、注解等信息&#xff0c;还可以创建类的实例…

Elasticsearch克隆索引

我所使用的Elasticsearch的版本是基于7.17.7。 需求是将某个ES的索引进行克隆。例如我要将索引test_0419_1克隆一份新的索引test_0419_2。步骤如下&#xff1a; 首先将源索引进行修改PUT /test_0419_1/_block/write&#xff0c;即禁止对这个索引进行写数据操作。然后执行克隆…