ESP32电容式触摸传感器引脚实例
这篇文章展示了如何使用Arduino IDE的ESP32触摸引脚。ESP32触针可以感知任何带有电荷的物体的变化。它们通常被用来唤醒沉睡中的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("有触控,灯亮");
}
else{
// turn LED off
digitalWrite(ledPin, LOW);
Serial.println(" - LED off");
}
delay(500);
}