Adarfruit的触摸库-Arduino中文社区 - Powered by Discuz! Archiver

Zoologist 发表于 2016-9-10 08:45

Adarfruit的触摸库

之前我介绍过一个MPR121 触摸传感器模块的库【参考1】,这次再介绍另外一个实现同样功能的库,差别在于这个新的库可以从Arduino IDE中直接下载到:                              国内买到的板子都是根据 Adafruit仿造的,下图就是 Adafruit的原图 引出的 12个触摸脚,还有如下控制pin:1.IRQ 可以做到按下时同时 Arduino2.SDA/SCL I2C通讯用的3.ADDR: 作用是选择I2C的地址,模块本身有下拉电阻,这里可以不接。默认地址是0x5B 4.GND 地5. 3.3V 电源6. VIN 模块本身有一个电压转换芯片,可以接入 5V
运行库自带的例子,接线上只要接3.3V GND SDA SCL 四根线即可,这个库本身是用轮询的方法来处理引脚的触摸信息的。代码如下:
/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor

Designed specifically to work with the MPR121 Breakout in the Adafruit shop
----> https://www.adafruit.com/products/

These sensors use I2C communicate, at least 2 pins are required
to interface

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**********************************************************/

#include <Wire.h>
#include "Adafruit_MPR121.h"

// 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'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;

void setup() {
while (!Serial);      // needed to keep leonardo/micro from starting too fast!

Serial.begin(9600);
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 0x5D
if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
}
Serial.println("MPR121 found!");
}

void loop() {
// Get the currently touched pads
currtouched = 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)) ) {
      Serial.print(i); Serial.println(" touched");
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
    }
}

// reset our state
lasttouched = currtouched;

// comment out this line for detailed data from the sensor!
return;

// debugging info, what
Serial.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 overwhelming
delay(100);
}
运行结果:
需要注意的是模块的地址问题,根据DataSheet ADDR 如果接到GND,地址应该是0x5B ,但是实际上是 0x5A。参考:1.   http://www.lab-z.com/mpr121/MPR121 触摸传感器模块2.   https://www.adafruit.com/products/1982

qingkong 发表于 2016-9-12 15:38

这是个好东西,我正在使用TTP229做东西,可以试试这个芯片。

qingkong 发表于 2016-9-12 15:40

可惜你提供的资料地址都打不开

mostblack 发表于 2016-9-13 11:56

之前买过一个这个芯片的那种电容键盘~

unkindle913 发表于 2021-11-19 16:47

我串口监视器一直显示 not found,把add口接地接3.3V接时钟和地址线都是这句。请问是什么原因

Zoologist 发表于 2021-11-19 19:39

unkindle913 发表于 2021-11-19 16:47
我串口监视器一直显示 not found,把add口接地接3.3V接时钟和地址线都是这句。请问是什么原因 ...

not found 就是无法找到芯片所以输出

检查连线或者你的芯片坏了?

unkindle913 发表于 2021-11-20 11:37

Zoologist 发表于 2021-11-19 19:39
not found 就是无法找到芯片所以输出

检查连线或者你的芯片坏了?

大佬,我买的是黑色的那种突破板,连线是3.3V接uno板上的3.3V,商家说默认地址是0x5A,但是我无论ADD悬空还是接地还是接时钟线或者地址线都显示 not found ;而且试了两个板子都是这个情况,删掉检测那段程序后,直接显示全部触摸。是我还应该外接上拉电阻吗,试了好几天一直没成功

Zoologist 发表于 2021-11-20 19:59

unkindle913 发表于 2021-11-20 11:37
大佬,我买的是黑色的那种突破板,连线是3.3V接uno板上的3.3V,商家说默认地址是0x5A,但是我无论ADD悬空 ...

你先找一个 i2c 扫描程序试试看

https://www.lab-z.com/ai2cscan/

unkindle913 发表于 2021-11-20 22:25

Zoologist 发表于 2021-11-20 19:59
你先找一个 i2c 扫描程序试试看

https://www.lab-z.com/ai2cscan/

谢谢大佬,我先试试

Zoologist 发表于 2021-11-21 09:18

unkindle913 发表于 2021-11-20 22:25
谢谢大佬,我先试试

我现在买东西,如果比较偏移都是一次买两个,主要是怕坏。通常来讲,两个同时坏的可能很小。
页: [1]
查看完整版本: Adarfruit的触摸库