ESP32 基础知识:电容式触摸引脚

ESP32-电容式触摸引脚-教程

ESP32 是一款出色的开发板,可用于创建智能物联网项目,添加触摸功能将使它们变得更加智能。

ESP32 提供 10 个电容式触摸感应 GPIO(2,4,12,13,14,15,27,32,33)只引出了9个。您可以使用这些 GPIO 更新现有的简单按钮项目或创建灯光开关、乐器或自定义交互界面。

让我们学习如何处理这些触摸感应引脚并在项目中使用它们。

ESP32 中的触摸检测由 ULP 协处理器管理。因此这些触摸引脚也可用于将ESP32 从深度睡眠中唤醒

ESP32如何感知触摸?

ESP32 使用人体的电气特性作为输入。当用手指触摸触摸感应引脚时,少量电荷会被吸引到接触点。

这会触发电容变化,从而产生模拟信号。然后,两个逐次逼近 ADC (SAR ADC) 将此模拟信号转换为数字。

ESP32 触摸引脚

ESP32 有 10 个电容式触摸感应 GPIO。当电容负载(例如人体皮肤)靠近 GPIO 时,ESP32 会检测到电容变化。

虽然 ESP32 共有 10 个电容式触摸感应 GPIO 引脚,但其中只有 9 个引出到 30 引脚 ESP32 开发板两侧的排针。

esp32 触摸引脚

读取触摸传感器

读取触摸传感器非常简单。在 Arduino IDE 中,您可以使用该touchRead()函数,该函数接受您要读取的 GPIO 引脚号作为参数。

touchRead(GPIOPin);

硬件连接

理论已经够多了!让我们看一个实际的例子。

让我们将电缆连接到 Touch #0 (GPIO #4)。您可以将任何导电物体(如铝箔、导电布、导电涂料等)附着到此引脚上,并将其变成触摸板。

将电线连接到 esp32 以读取 touch gpio

示例代码

让我们使用库中的示例来看看它是如何工作的。打开 Arduino IDE,导航至File > Examples > ESP32 > Touch,然后打开TouchRead草图。

此示例仅读取触摸引脚 0 并在串行监视器上显示结果。

// ESP32 Touch Test
// Just test touch pin - Touch0 is T0 which is on GPIO 4.

void setup() {
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Test");
}

void loop() {
  Serial.print("Touch: ");
  Serial.println(touchRead(4));  // get touch value on GPIO 4
  delay(1000);
}

上传草图后,以波特率 115200 打开串行监视器,然后按 ESP32 上的 EN 按钮。

现在尝试触摸电线的金属部分,看看它对触摸有何反应。

串行监视器上的 esp32 触摸测试输出

代码说明:

代码非常简单。在setup()中,我们首先初始化与PC的串行通信。

Serial.begin(115200);

在loop()中,我们使用touchRead()函数,并将我们想要读取的引脚作为参数传递。在本例中为 GPIO #4。您还可以传递触摸传感器编号T0

Serial.println(touchRead(4));

ESP32 项目 – 触摸激活 LED

让我们快速创建一个项目来演示如何使用 ESP32 的触摸引脚来控制设备。在此示例中,我们将制作一个简单的触摸激活 LED,当您触摸 GPIO 引脚时,该 LED 就会亮起。

当然,这个项目可以扩展到开门、开关继电器、LED 灯或任何您能想到的东西。

寻找门槛

在继续之前,您应该查看实际从 ESP32 获得的读数。请注意触摸引脚和未触摸引脚时获得的输出。

当您运行上一个程序时,您将在串行监视器中看到接近以下读数:

  • 当您触摸引脚时 (~3)
  • 当你不碰针时 (~71)

根据这些值,我们可以设置一个阈值,这样当读数低于阈值时,我们将切换 LED。在这种情况下,30 可能是一个很好的阈值。

示例代码

下面是一个简单的代码,当您触摸一次引脚时,板载 LED 会打开,当您再次触摸它时,它会关闭。

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

const int threshold = 30;  // 设置阈值

int ledState = LOW;         // the current state of the output pin
int touchState;             // the current reading from the input pin
int lastTouchState = LOW;   // the previous reading from the input pin

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the pin
  int reading = touchRead(touchPin);

  // binarize touch reading for easy operation
  if (reading < threshold) {
    reading = HIGH;
  } else{
    reading = LOW;
  }
  
  // If the pin is touched:
  if (reading != lastTouchState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the touch state has changed:
    if (reading != touchState) {
      touchState = reading;

      // only toggle the LED if the new touch state is HIGH
      if (touchState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastTouchState:
  lastTouchState = reading;
}

将草图上传到您的 ESP32。每次触摸电线时,您都应该看到 LED 切换。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值