ESP32 MicroPython Touch Sensor

This tutorial instructs you how to use a touch sensor switch with a ESP32 and MicroPython. You will learn how to connect the touch sensor to ESP32 and write MicroPython code for ESP32 to read the state from the touch switch.

ESP32 MicroPython touch sensor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Touch Sensor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Touch Sensor

The touch sensor, also known as a touch switch or touch button, is commonly used to control devices (e.g., touch-sensitive lamps). It functions the same as a traditional button but is often used in place of buttons on modern devices to create a more streamlined and neat appearance.

Pinout

The touch sensor has three connectors.

  • GND pin: connect to GND (0 volts)
  • VCC pin: connect to VCC (5 volts or 3.3 volts)
  • SIGNAL pin: if not touched, it shows LOW; if touched, it shows HIGH. Connect this pin to the ESP32's input pin.
Touch Sensor Pinout

How It Works

  • When no one is touching the sensor, the signal pin is LOW.
  • When someone touches the sensor, the signal pin is HIGH.

ESP32 - Touch Sensor

The touch sensor's SIGNAL pin is connected to an input pin on the ESP32.

To know if the touch sensor is touched, check if the input pin on the ESP32 is active.

Wiring Diagram

  • How to connect ESP32 and touch sensor using breadboard
The wiring diagram between ESP32 MicroPython Touch Sensor

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and touch sensor

Touch Sensor - ESP32 MicroPython Code

Reads the value from the touch sensor and print it out

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-touch-sensor """ from machine import Pin import utime SENSOR_PIN = 18 # The ESP32 pin GPIO18 connected to the touch sensor # Create a Pin object to handle the input from the touch sensor sensor = Pin(SENSOR_PIN, Pin.IN) while True: # Read the state of the input pin state = sensor.value() # Print the state to the console print(state) # Sleep for a short period to avoid flooding the output utime.sleep(0.5)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the touch sensor according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Touch the sensor with your finger and then take your finger off.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 0 0 1 1 1 1 1 1 1 0 0 0 0 0
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Detects the sensor is touched or released

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-touch-sensor """ from machine import Pin import utime SENSOR_PIN = 18 # The ESP32 pin GPIO18 connected to the touch sensor # Create a Pin object to handle the input from the touch sensor sensor = Pin(SENSOR_PIN, Pin.IN) # Initialize the previous touch state to 0 (assumed LOW) prev_touch_state = 0 while True: # Read the current state of the input pin touch_state = sensor.value() # Check if the sensor state has changed from LOW to HIGH (touched) if prev_touch_state == 0 and touch_state == 1: print("The sensor is touched") # Check if the sensor state has changed from HIGH to LOW (released) elif prev_touch_state == 1 and touch_state == 0: print("The sensor is released") # Save the current state as the previous state for the next loop iteration prev_touch_state = touch_state # Sleep for a short period to debounce the sensor utime.sleep(0.1)

Detailed Instructions

  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 board.
  • Click the green Run button (or press F5) to execute the script.
  • Put your finger on the sensor and keep it there.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The sensor is touched
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡
  • Remove your finger from the sensor.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The sensor is touched The sensor is is released
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!