搜索代码

Arduino的TTP223电容触摸模块简介

Arduino的TTP223电容触摸模块简介

在本教程中,我们将探讨TTP223电容触摸模块以及如何将其与Arduino集成以创建一个触摸敏感开关。TTP223是一个简单而有效的模块,它允许你通过轻触来开启或关闭设备,如继电器或灯。这项项目将演示如何连接模块并编写一个基本的Arduino程序来读取触摸输入并控制输出设备。本项目的代码和接线将详细解释,以便您可以轻松复制该设置。对于视觉学习者,我建议观看相关的视频教程,以便对设置和编码过程进行澄清(视频中时间为03:15)。

硬件解析

TTP223电容触摸模块是一款紧凑且易于使用的设备,通过电容传感器检测触摸输入。它具有一个输出引脚,当检测到触摸时该引脚会变为高电平,非常适合用于需要通过简单触摸控制电子设备的应用。该模块的供电电压范围为2.0到5.5伏特,使其与大多数Arduino板兼容。 该模块通常具有几个引脚:用于供电的VCC,引地的GND,以及在检测到触摸时发送信号的输出引脚。这个输出引脚可以连接到Arduino的数字输入引脚上,使您能够轻松读取触摸状态。TTP223模块在不希望使用物理开关的项目中特别有用。

数据表详情

制造商Seeed Studio
零件编号TTP223
逻辑/输入输出电压2.0 - 5.5 伏
供电电压2.0 - 5.5 伏
输出电流最大20毫安
峰值电流30毫安
工作温度-40 到 85 °C
包裹TO-220

  • 确保提供适当的电压供应(2.0 - 5.5 V),以避免损坏模块。
  • 输出引脚可以连接到Arduino上的任何数字引脚。
  • 将 GND 引脚连接到 Arduino 地。
  • 将模块远离电噪声源以避免误触发。
  • 必要时使用上拉电阻以获得稳定读数。

接线说明

要将TTP223电容触摸模块连接到Arduino,首先将模块上的VCC引脚连接到Arduino上的5V引脚。接下来,将模块上的GND引脚连接到Arduino上的任一GND引脚。TTP223的输出引脚(发送触摸信号)应连接到Arduino上的数字引脚,例如引脚2。 如果您使用的是带有触摸模块的继电器,将继电器控制引脚连接到另一个数字引脚,例如引脚8。确保继电器的电源按照其规格正确连接。如果您使用多个触摸模块,可以将额外的输出引脚连接到Arduino上的其他数字引脚,并相应地重复接线过程。

代码示例与指南

以下代码片段初始化引脚并设置串行通信。变量touchPin与TTP223模块的输出相连,然而relayPin控制继电器。


int touchPin = 2; // connect output from TTP223 to this
int relayPin = 8; // Connected to relay

void setup() {
  Serial.begin(9600);
  pinMode(touchPin, INPUT); 
  pinMode(relayPin, OUTPUT);
}

该代码初始化触摸模块和继电器引脚,为在循环函数中使用做好准备。Serial.begin(9600)命令设置串行通信的波特率,允许您通过串行监视器监控触摸状态。 在循环中,程序检查触摸输入的状态,并相应地切换继电器。如果检测到触摸,它会在串行监视器上打印“已触摸”并打开继电器。


void loop() {
  val = digitalRead(touchPin); 
  if(val == 1) {
    Serial.println("Touched");
    digitalWrite(relayPin, LOW); // turn relay ON
  }
  delay(100);
}

该摘录展示了触控输入是如何被读取和处理的。digitalRead(touchPin)该函数检查是否检测到触摸。如果检测到,将通过设置激活继电器。relayPin到低。

演示 / 预期内容

当你运行代码并触摸 TTP223 模块时,你应该在串口监视器中看到"Touched"被打印出来,表示触摸被检测到。连接到的继电器relayPin也将激活,让您可以控制连接到它的任何设备。请注意防抖效果;如果您太快触摸传感器,可能会注册多个触摸。 对于更复杂的设置,您可以添加多个 TTP223 模块以控制不同的设备。每个模块可以连接到单独的输入引脚,并在同一循环函数中管理,从而实现对多个输出的多样化控制(视频在 10:45)。

视频时间戳

  • 00:00- TTP223模块介绍
  • 03:15- 接线说明
  • 05:30- 代码演示
  • 10:45- 触摸模块演示
8-The source code for the TTP223 touch module for Arduino
语言: C++
/*
 * 这是用于 TTP223 触摸模块开关的 Arduino 代码。  
 * 通过触摸模块,8 号引脚发送信号以开启继电器或开关。  
 * 
 * 由 Ahmad S. 为 Robojax.com 编写  
 * 日期:2017 年 4 月 1 日,加拿大安大略省 Ajax  
 * 允许分享此代码,前提是此  
 * 说明与代码一起保留。  
 * 免责声明:此代码为“按原样”提供,仅用于教育目的。
 */
int touchPin = 2; // 将TTP223的输出连接到此处
int val = 0;
int relayPin = 8; // 已连接至继电器

void setup() {
 Serial.begin(9600);
 pinMode(touchPin, INPUT);
  pinMode(relayPin, OUTPUT);

}

void loop() {
  digitalWrite(relayPin, HIGH);
  val = digitalRead(touchPin);
  if(val ==1){
  Serial.println("Touched");
  digitalWrite(relayPin, LOW);
  }
  delay(100);
  Serial.println();
}
9-The source code for the TTP223 touch module for Arduino for a two-touch module
语言: C++
/*
 * This is the Arduino code for a TTP223 touch module switch.
 * By touching the module, the pin 8 is sending a signal to turn the relay or switch ON.
 * 
 * Written by Ahmad S. for Robojax.com 
 * Date: April 1, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code, given that this
 * note is kept with the code.
 * Disclaimer: This code is "AS IS" and for educational purposes only.
 * 
 */

 // 1st channel
int touchPin1 = 2;// connect output from TTP223 to this
int val = 0; 
int relayPin = 8;// Connected to relay

 // 2nd channel
int touchPin2 = 3;// connect output from TTP223 to this
int val2 = 0; 
int relayPin2 = 9;// Connected to relay

void setup() {
 Serial.begin(9600);
 pinMode(touchPin, INPUT); // input from touch
 pinMode(relayPin, OUTPUT);// output to relay
 
  pinMode(touchPin2, INPUT); // input from touch (2)
 pinMode(relayPin2, OUTPUT);// output to relay (2)

}

void loop() {
 // start of channel 1 code 
  digitalWrite(relayPin, HIGH);// set the relay to HIGH (if your relay is low-trigger. If not, remove this line)
  val = digitalRead(touchPin); // read channel 1 touch module value
  if(val ==1){
  Serial.println("Channel 1 Touched");
  digitalWrite(relayPin, LOW);// turn relay (of switch) 1 ON
  }
  delay(100);
  Serial.println();
   // end of channel 1 code 
  
 // start of channel 2 code   
  digitalWrite(relayPin2, HIGH);// set the relay to HIGH (if your relay is low-trigger. If not, remove this line)
  val2 = digitalRead(touchPin2); // read channel 2 touch module value
  if(val2 ==1){
  Serial.println("Channel 2 Touched");
  digitalWrite(relayPin2, LOW);// turn relay (of switch) 2 ON
  }
  delay(100);
  Serial.println();
  // end of channel 2 code 
}

资源与参考

尚无可用资源。

文件📁

没有可用的文件。