一、目的
这一节我们学习如何使用我们的ESP32开发板来学习触控TouchPad操作。
二、环境
ESP32 + Thonny + 几根杜邦线 + Win10
接线方法:
三、代码
from machine import Pin,TouchPadimport time# 可使用的触控pin: 0,2,4,12,13,14,15,27,32,33 led=Pin(2,Pin.OUT)tou=TouchPad(Pin(15))def Touch():temp = tou.read()print("%d"%(temp))if(temp > 10 and temp < 100):led.value(1)else:led.value(0)time.sleep(0.5)def main():while True:Touch()if __name__=="__main__":main()
四、演示效果
运行程序后,板载LED灯(使用的是GPIO2)状态为灭,当你用手或者钥匙去触摸GPIO15上的导线的时候,LED就会亮起来。
五、参考资料
官方资料:Quick reference for the ESP32 — MicroPython latest documentation/en/latest/esp32/quickref.html#capacitive-touch