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

简介

  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.hqwc.cn/news/635838.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1.为什么选择Vue框架

参考&#xff1a;百战程序员 为什么选择Vue框架 Vue是什么&#xff1f; 渐进式 JavaScript 框架&#xff0c;易学易用&#xff0c;性能出色&#xff0c;适用场景丰富的 Web 前端框架 为什么要学习Vue Vue是目前前端最火的框架之一Vue是目前企业技术栈中要求的知识点Vue可以…

2024上海国际半导体制造设备材料与核心部件展览会

2024上海国际半导体制造设备材料与核心部件展览会 2024 Shanghai International Semiconductor Manufacturing Equipment Materials and Core Components Exhibition 时间&#xff1a;2024年11月18日-20日 地点&#xff1a;上海新国际博览中心 详询主办方陆先生 I38&#…

C++三大特性之一:继承

文章目录 前言一、继承方式二、继承类型继承中构造和析构的顺序继承中的内存分配多继承语法(非重点)继承中同名静态成员的处理继承一般在哪里用到进阶&#xff1a;菱形继承和虚拟继承 总结 前言 C三大特性&#xff1a;继承、多态和封装。继承是面向对象编程的一个核心概念&…

IMUGNSS 误差状态卡尔曼滤波器(ESKF)的离散时间的ESKF 运动方程与运动过程

IMU&GNSS 误差状态卡尔曼滤波器&#xff08;ESKF&#xff09;的离散时间的ESKF 运动方程与运动过程 离散时间的ESKF 运动方程ESKF的运动过程 离散时间的ESKF 运动方程 名义状态变量的离散时间运动方程可以写为&#xff1a;&#xff08;不用考虑噪声&#xff0c;噪声在误差…

华为 2024 届实习招聘——硬件-电源机试题(四套)

华为 2024 届实习招聘——硬件-电源机试题&#xff08;四套&#xff09; 部分题目分享&#xff0c;完整版带答案(有答案&#xff0c;答案非官方&#xff0c;未仔细校正&#xff0c;仅供参考&#xff09;&#xff08;共四套&#xff09; 获取&#xff08;WX:didadidadidida313&…

LeetCode - 283.移动零

题目链接&#xff1a; LeetCode - 283.移动零 题目分析&#xff1a; ​​​​​ 题解代码&#xff1a; #include<iostream> #include<vector> using namespace std;class Solution { public:void moveZeroes(vector<int>& nums) {for (int cur 0, des…

【001_音频开发-基础篇-专业术语】

001_音频开发-基础篇-专业术语 文章目录 001_音频开发-基础篇-专业术语创作背景术语表常见音源HDMI相关声音系统立体声2.1 声音系统5.1 环绕声系统5.1.2 环绕声系统7.1 环绕声系统7.1.4 环绕声系统9.1.4 环绕声系统 音质等级定义QQ音乐网易云音乐 创作背景 学历代表过去、能力…

ChatGPT研究论文提示词集合1-【主题选择与问题研究、文献综述】

点击下方▼▼▼▼链接直达AIPaperPass &#xff01; AIPaperPass - AI论文写作指导平台 目录 1.主题选择与问题定义 2.文献综述 3.书籍介绍 AIPaperPass智能论文写作平台 近期小编按照学术论文的流程&#xff0c;精心准备一套学术研究各个流程的提示词集合。总共14个步骤…

C++相关概念和易错语法(7)(初始化列表、隐式类型转换、友元)

1.初始化列表 初始化列表是集成在构造函数里面的&#xff0c;对象在创建的时候一定会调用构造函数&#xff08;就算不显式定义&#xff0c;也会自动生成并调用&#xff09;。初始化列表就是这些对象的成员变量在创建的时候初始化的地方。 下面是使用的例子&#xff0c;可以先…

【CVPR2023】《A2J-Transformer:用于从单个RGB图像估计3D交互手部姿态的锚点到关节变换网络

这篇论文的标题是《A2J-Transformer: Anchor-to-Joint Transformer Network for 3D Interacting Hand Pose Estimation from a Single RGB Image》&#xff0c;作者是Changlong Jiang, Yang Xiao, Cunlin Wu, Mingyang Zhang, Jinghong Zheng, Zhiguo Cao, 和 Joey Tianyi Zhou…

7. DAX 时间函数-- DATE 日期--TOTALMTD、TOTALQTD、TOTALYTD

函数名目的语法返回值TOTALMTD计算当前上下文中该月份至今的表达式的值 。TOTALMTD ( <表达式>, <日期列>, [<筛选器>] )标量 表示表达式的标量值&#xff0c;在“日期”中给定日期&#xff0c;计算当前月份至今的日期 。TOTALQTD计算当前上下文中该季度至今…

k8s 部署 kube-prometheus监控

一、Prometheus监控部署 1、下载部署文件 # 使用此链接下载后解压即可 wget https://github.com/prometheus-operator/kube-prometheus/archive/refs/heads/release-0.13.zip2、根据k8s集群版本获取不同的kube-prometheus版本部署 https://github.com/prometheus-operator/k…