ESP32电容式触摸传感器引脚

ESP32电容式触摸传感器引脚


这篇文章展示了如何使用Arduino IDE的ESP32触摸引脚。ESP32触针可以感知任何带有电荷的物体的变化。它们通常被用来唤醒沉睡中的ESP32

ESP32电容式触摸传感器引脚_第1张图片

实例代码

// set pin numbers
const int touchPin = 4; 
const int ledPin = 2;

// 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(" - LED on");
  }
  else{
     
    // turn LED off
    digitalWrite(ledPin, LOW);
    Serial.println(" - LED off");
  }
  delay(500);
}

你可能感兴趣的