搜尋程式碼

TTP224 4通道電容觸摸 Arduino 基本代碼

TTP224 4通道電容觸摸 Arduino 基本代碼

呢個教學會帶你逐步了解點樣用 TTP224 4通道電容式觸摸感應器配合 Arduino。TTP224 可以幫你整一個簡單嘅觸摸介面,按邊個掣就會著相應嘅 LED。完成呢個項目之後,你就會有一個運作正常嘅系統,掂吓四個掣嘅每一個都會著返相應嘅 LED,同時喺序列監視器度顯示訊息。

TTP224 touch module

喺呢個設定入面,當有掣被撳嘅時候,Arduino 上相應嘅輸出腳位就會啟動,著返 LED。你亦都會喺序列監視器度見到訊息,話俾你知邊個掣被掂到。呢個項目好啱初學者,想學吓電容式觸摸技術同基本 Arduino 編程嘅人。想知多啲細節,記得睇吓 2:30 嘅影片。

硬件解說

呢個項目嘅主要元件係 TTP224 電容式觸摸感應器。佢有四個觸摸感應掣,唔使機械開關都可以做到簡單互動。當有掣被掂到嘅時候,感應器會喺相應嘅輸出腳位輸出高電平訊號。咁樣就好容易連接到其他裝置,好似 LED 或者繼電器咁。

除咗 TTP224 之外,你仲需要一塊 Arduino 板去處理觸摸輸入同控制 LED。Arduino 會讀取感應器嘅輸出腳位,然後相應咁控制 LED。呢個設定亦都可以擴展去做更加複雜嘅應用,好似控制繼電器或者整一個保安鎖系統咁。

Datasheet 詳細資料

製造商TonTouch
零件編號TTP224
邏輯/IO 電壓2.5–5.5 V
輸出電流(每通道)最大 10 mA
峰值電流(每通道)最大 20 mA
響應時間10 ms(典型值)
封裝8-DIP

  • 確保電源供應穩定,喺指定範圍之內。
  • 有需要嘅話用下拉電阻,防止浮空狀態。
  • 將感應器遠離干擾源,好似強電磁場咁。
  • 考慮用防彈跳延遲,防止誤觸發。
  • 測試觸摸靈敏度,調整環境以達到最佳表現。

接線指示

Wring TTP224 Touch 4 Channel with 4 LED
Wring TTP224 Touch 4 Channel with 4 LED

要將 TTP224 接到 Arduino,首先連接電源同接地。將 TTP224 嘅 VCC 腳位連到 Arduino 嘅 5V 腳位,GND 腳位就連到 Arduino 嘅接地腳位。跟住,將 TTP224 嘅輸出腳位連到 Arduino 嘅數位腳位。具體嚟講,將輸出腳位 1 連到 Arduino 嘅腳位 2,輸出腳位 2 連到腳位 3,輸出腳位 3 連到腳位 4,輸出腳位 4 連到腳位 5。

至於 LED,將每粒 LED 嘅陽極(較長嘅腳)分別連到 Arduino 嘅腳位 10、11、12 同 13。將 LED 嘅陰極(較短嘅腳)透過電阻(通常係 220Ω)連到接地,用嚟限制電流。呢個設定會令 Arduino 可以根據 TTP224 嘅觸摸輸入嚟控制每粒 LED。

程式碼範例同解說

Arduino 程式碼會初始化 LED 嘅輸出腳位同觸摸感應器嘅輸入腳位。程式碼用簡單嘅條件判斷去檢查邊個掣被撳,然後著返相應嘅 LED,同時喺序列監視器度印出訊息。

int LD = 200; // 迴圈延遲。唔好改。

void setup() {
    Serial.begin(9600);
    pinMode(10, OUTPUT);// 掣 1 嘅 LED
    pinMode(11, OUTPUT);// 掣 2 嘅 LED
    pinMode(12, OUTPUT);// 掣 3 嘅 LED
    pinMode(13, OUTPUT);// 掣 4 嘅 LED            
    pinMode(2, INPUT);// 掣 1 輸入腳位 2  
    pinMode(3, INPUT);// 掣 2 輸入腳位 3  
}

喺呢段節錄入面,變數 LD 用嚟控制迴圈延遲。setup() 函數初始化序列通訊,並將適當嘅腳位設定為輸入或輸出。

void loop() {
    if(digitalRead(2)){
        Serial.println("掣 1 被掂到 "); 
        digitalWrite(10, HIGH); // 將 LED 著返     
        delay(LD); 
    }else{
        digitalWrite(10, LOW);// 將 LED 熄咗
    }
}

呢段程式碼示範咗 Arduino 點樣檢查連接掣 1 嘅輸入腳位狀態。如果掣被掂到,佢會印出訊息並著返相應嘅 LED。喺完整程式碼入面,其他掣都用相同嘅邏輯重複處理。

示範 / 預期效果

當你執行呢個程式嘅時候,掂吓 TTP224 上面嘅每個掣,都會著返相應嘅 LED,同時喺序列監視器度顯示訊息。確保你嘅接線正確,避免出現問題。如果你同時掂多個掣,可能會見到預期之外嘅行為,所以要小心(喺影片 5:15 度有講)。

影片時間標記

  • 00:00 - TTP224 簡介
  • 02:30 - 接線設定
  • 04:15 - 程式碼解說
  • 05:15 - 項目示範

圖片

TTP224 touch module
TTP224 touch module
Wring TTP224 Touch 4 Channel with 4 LED
Wring TTP224 Touch 4 Channel with 4 LED
19-TTP224 4-Channel Capacitive Touch Arduino Basic Code
語言: C++
/*
 * This is the Arduino code for TTP224 4-Channel Capacitive Touch switch.
 * The output pins 10, 11, 12, and 13 will be ON when keys 1 to 4 are pressed respectively.
 * Pressing button 1 turns pin 10 ON.
 * Pressing button 2 turns pin 11 ON.
 * Pressing button 3 turns pin 12 ON.
 * Pressing button 4 turns pin 13 ON.
 * 
 * Written by Ahmad Shamshiri for Robojax Video
 * Date: December 2, 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 LD = 200; // Loop Delay. Do not change.

void setup() {
    Serial.begin(9600);
    // out pins
    pinMode(10, OUTPUT);// LED for button 1
    pinMode(11, OUTPUT);// LED for button 2
    pinMode(12, OUTPUT);// LED for button 3
    pinMode(13, OUTPUT);// LED for button 4            

    // input pins
    pinMode(2, INPUT);// Button 1 input pin 2  
    pinMode(3, INPUT);// Button 2 input pin 3  
    pinMode(4, INPUT);// Button 3 input pin 4  
    pinMode(5, INPUT);// Button 4 input pin 5              


    Serial.println("Robojax Test");

}

void loop() {

    // button 1 action
    if(digitalRead(2)){
      Serial.println("Button 1 Touched "); 
      digitalWrite(10, HIGH); // Turn the LED ON     
      delay(LD); 
    }else{
      digitalWrite(10, LOW);// Turn OFF the LED
    }

    // button 2 action
    if(digitalRead(3)){
      Serial.println("Button 2 Touched "); 
      digitalWrite(11, HIGH); // Turn the LED ON
      delay(LD); 
    }else{
      digitalWrite(11, LOW);// Turn OFF the LED
    }   

    // button 3 action
    if(digitalRead(4)){
      Serial.println("Button 3 Touched "); 
      digitalWrite(12, HIGH); // Turn the LED ON       
      delay(LD); 
    }else{
      digitalWrite(12, LOW);// Turn OFF the LED
    }     

   // button 4 action
    if(digitalRead(5)){
      Serial.println("Button 4 Touched "); 
      digitalWrite(13, HIGH); // Turn the LED ON      
      delay(LD); 
    }else{
      digitalWrite(13, LOW);// Turn OFF the LED
    }

}// loop

資源與參考資料

暫時冇資源。

文件📁

数据表 (pdf)

其他文件