Arduino用嘅TTP223電容式觸摸模組介紹
喺呢個教學入面,我哋會探討TTP223電容觸摸模組,同埋點樣將佢同Arduino整合,整一個觸摸感應開關。TTP223係一個簡單又有效嘅模組,可以畀你淨係靠掂一掂就開關繼電器或者燈呢啲裝置。呢個項目會示範點樣連接模組,同埋寫一個基本嘅Arduino程式去讀取觸摸輸入同控制輸出裝置。 呢個項目嘅程式同接線會詳細解釋,等你可以輕鬆重複呢個設定。對於鍾意睇片學習嘅人,我建議睇埋相關嘅視頻教學,等你可以更加清楚個設定同編程過程(喺視頻03:15)。硬件解釋
TTP223電容觸摸模組係一個細細粒又易用嘅裝置,透過電容感應嚟偵測觸摸輸入。佢有一個輸出腳,當偵測到觸摸嘅時候會變高電平,好適合用喺想用簡單觸摸嚟控制電子產品嘅應用。呢個模組嘅供電電壓係2.0至5.5伏特,所以同大部分Arduino板都兼容。 呢個模組通常有幾個腳:VCC用嚟供電,GND接地,仲有輸出腳,當偵測到觸摸嘅時候會發送訊號。呢個輸出腳可以駁去Arduino嘅數位輸入腳,等你可以輕鬆讀取觸摸狀態。TTP223模組尤其啱用喺啲唔想用實體開關嘅項目度。Datasheet細節
| 製造商 | Seeed Studio |
|---|---|
| 零件編號 | TTP223 |
| 邏輯/IO電壓 | 2.0 – 5.5 V |
| 供電電壓 | 2.0 – 5.5 V |
| 輸出電流 | 最大20 mA |
| 峰值電流 | 30 mA |
| 工作溫度 | -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; // 將TTP223嘅輸出駁去呢度
int relayPin = 8; // 駁去繼電器
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(relayPin, OUTPUT);
}
呢段程式初始化觸摸模組同繼電器腳,準備好畀佢哋喺loop函數入面用。Serial.begin(9600)指令設定串列通訊嘅鮑率,等你可以透過Serial Monitor監察觸摸狀態。
喺loop入面,程式檢查觸摸輸入嘅狀態,然後相應噉切換繼電器。如果偵測到觸摸,佢會喺Serial Monitor度印出「Touched」,然後開著繼電器。
void loop() {
val = digitalRead(touchPin);
if(val == 1) {
Serial.println("Touched");
digitalWrite(relayPin, LOW); // 開繼電器
}
delay(100);
}
呢段節錄示範咗點樣讀取同處理觸摸輸入。digitalRead(touchPin)函數檢查有冇偵測到觸摸。如果有,就將relayPin設為LOW嚟啟動繼電器。
示範 / 預期效果
當你執行個程式,然後掂一掂TTP223模組,你應該會喺Serial Monitor度見到「Touched」印出嚟,表示偵測到觸摸。駁咗去relayPin嘅繼電器都會啟動,等你可以控制任何駁咗去佢嘅裝置。要注意防彈跳效果;如果你掂得太快,可能會登記到多次觸摸。
如果想整複雜啲嘅設定,你可以加多幾個TTP223模組嚟控制唔同裝置。每個模組都可以駁去獨立嘅輸入腳,然後喺同一個loop函數入面管理,等你可以靈活控制多個輸出(喺視頻10:45)。
視頻時間戳
- 00:00 - TTP223模組簡介
- 03:15 - 接線指示
- 05:30 - 程式講解
- 10:45 - 觸摸模組示範
8-The source code for the TTP223 touch module for Arduino
語言: C++
/*
* This is the Arduino code for a TTP223 Touch module switch.
* By touching the module, the pin 8 sends 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.
*
*/
int touchPin = 2;// connect output from TTP223 to this
int val = 0;
int relayPin = 8;// Connected to relay
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
}
資源與參考資料
暫時冇資源。
文件📁
其他文件
-
TP223 数据表
robojax-0492_TTP223_Datasheet.pdf0.24 MB