ESP32电容式触摸传感器引脚实例
本文展示了如何使用它Arduino IDE的ESP32触摸引脚。ESP触针可以感知任何带电物体的变化。它们通常被用来唤醒睡眠中的物体ESP32
![]()
ESP32 10个触摸通道和引脚对应关系
* 触摸传感器通道 管脚 T0 GPIO4 T1 GPIO0 T2 GPIO2 T3 MTDO T4 MTCK T5 MTDI T6 MTMS T7 GPIO27 T8 32K_XN T9 32K_XP */
实例代码
// set pin numbers const int touchPin = 4; // 使用 T0 获取数据 const int ledPin = 2; //板载led灯 // change with your threshold value const int threshold = 20; // variable for storing the touch pin value int touchValue; void setup(){
Serial.begin(115200); delay(1000); // give me time to bring up serial monitor // initialize the LED pin as an output: pinMode (ledPin, OUTPUT); } void loop(){
// read the state of the pushbutton value: touchValue = touchRead(touchPin); Serial.print(touchValue); // check if the touchValue is below the threshold // if it is, set ledPin to HIGH if(touchValue < threshold){
// turn LED on digitalWrite(ledPin, HIGH); Serial.println("触摸,灯亮")span class="token punctuation">;
}
else{
// turn LED off
digitalWrite(ledPin, LOW);
Serial.println(" - LED off");
}
delay(500);
}