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

server/2024/10/22 10:43:28/

简介

  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/server/13382.html

相关文章

【论文笔记】RS-Mamba for Large Remote Sensing Image Dense Prediction(附Code)

论文作者提出了RS-Mamba(RSM)用于高分辨率遥感图像遥感的密集预测任务。RSM设计用于模拟具有线性复杂性的遥感图像的全局特征&#xff0c;使其能够有效地处理大型VHR图像。它采用全向选择性扫描模块&#xff0c;从多个方向对图像进行全局建模&#xff0c;从多个方向捕捉大的空间…

51单片机入门_江协科技_27~28_OB记录的自学笔记_AT24C02数据存储秒表

27. AT24C02(I2C总线) 27.1. 存储器介绍 27.2. 存储器简化模型介绍&#xff0c;存储原理 27.3. AT24C02介绍 •AT24C02是一种可以实现掉电不丢失的存储器&#xff0c;可用于保存单片机运行时想要永久保存的数据信息 •存储介质&#xff1a;E2PROM •通讯接口&#xff1a;I2…

快速理解mysql事务

什么是mysql事务&#xff1f; 事务是一组操作的集合&#xff0c;它是一个不可分割的工作单位&#xff0c;事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求&#xff0c;即这些操作要么同时成功&#xff0c;要么同时失败。 mysql事务特性 原子性&#xff1a;事务…

restful请求风格的增删改查-----修改and删除

一、修改&#xff08;和添加类似&#xff09; 前端&#xff1a; <script type"text/javascript">function update(){//创建user对象var user {id:$("#id").val(),username:$("#username").val(),password:$("#password").val…

新技术前沿-2024-大型语言模型LLM的本地化部署

参考快速入门LLM 参考究竟什么是神经网络 1 深度学习 1.1 神经网络和深度学习 神经网络是一种模拟人脑神经元工作方式的机器学习算法,也是深度学习算法的基本构成块。神经网络由多个相互连接的节点(也称为神经元或人工神经元)组成,这些节点被组织成层次结构。通过训练,…

大型组网使用BFD提高可靠性

学习目标&#xff1a; 1. 当前现网业务存在哪些问题&#xff1f; 2. BFD的工作机制讲解&#xff1b; 3. BFD联动静态路由及OSPF实战&#xff1b; -- BFD - 双向转发检测 -- 通用技术 - 各厂商都支持 - 介质无关 - 协议无关 - 规划地址和配置地址&#xff1a; …

【python】随机模拟——赶火车问题、醉汉回家

问题描述 1.赶火车问题。2.模拟二维随机游动&#xff08;醉汉回家&#xff09; 1.赶火车问题。 一列列车从A站开往B站&#xff0c;某人每天赶往B站上车。他已经了解到火车从A站到B站的运行时间是服从均值为30min&#xff0c;标准差为2min的正态随机变量。火车大约下午13&#…

C++设计模式:适配器模式(十四)

1、定义与动机 定义&#xff1a;将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的哪些类可以一起工作。 动机&#xff1a; 在软件系统中&#xff0c;由于应用环境的变化&#xff0c;常常需要将“一些现存的对象”放在新的环境…