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.
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) |
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.
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
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and touch sensor using screw terminal block breakout board
Touch Sensor - ESP32 MicroPython Code
Reads the value from the touch sensor and print it out
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.
Detects the sensor is touched or released
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.
- Remove your finger from the sensor.
- Check out the message in the Shell at the bottom of Thonny.