Pin:
ESP32 有 10 个电容式触摸 GPIO。这些 GPIO 可以感知任何带有电荷的东西的变化,比如手指触摸 GPIO 时引起的变化,当ESP32处于深度睡眠状态时,触控引脚还可以用作唤醒源。
TOUCH1 为 GPOI0,30 脚 GPIO 电路板中没有引出。
读取触摸传感器:
touchRead(GPIO);
- 触摸触摸感应 GPIO 时,传感器读取的值会下降。
- 可以设置一个阈值,以便在检测到触摸时执行某些变化。
Code:
触摸按键被触摸,点亮 LED,否则熄灭 LED
#include <Arduino.h>
const int touchPin = 4;
const int ledPin = 2;
const int threshold = 60; // 阈值
void setup()
{
Serial.begin(115200);
delay(1000);
pinMode (ledPin, OUTPUT);
}
void loop()
{
int touchValue = touchRead(touchPin); // 读取 GPIO4 触摸
// int touchValue = touchRead(T0); // 读取 GPIO4 触摸
Serial.print(touchValue);
if(touchValue < threshold)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
delay(500);
}