ESP32 - Touch Sensor

Touch sensor (also called touch button or touch switch) is widely used to control devices (e,g. touchable lamp). It has the same functionality as a button. It is used instead of the button on many new devices because it makes the product look neat.

In this tutorial, we will learn how to use the touch sensor with ESP32.

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Touch Sensor
3×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Touch Sensor

Pinout

Touch sensor has 3 pins:

  • GND pin needs to be connected to GND (0V)
  • VCC pin needs to be connected to VCC (5V or 3.3v)
  • SIGNAL pin is an output pin: LOW when it is NOT touched, HIGH when it is touched. This pin needs to be connected to ESP32's input pin.
  • Touch Sensor Pinout

How It Works

  • When the sensor is NOT touched, the sensor's SIGNAL pin is LOW
  • When the sensor is touched, the sensor's SIGNAL pin is HIGH

ESP32 - Touch Sensor

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

By reading the state of ESP32's pin (configured as an input pin), we can detect whether the touch sensor is touched or not.

Wiring Diagram

ESP32 Touch Sensor Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program For Touch Sensor

  • Initializes the ESP32 pin to the digital input mode by using pinMode() function. For example, pin GIOP18
pinMode(18, INPUT_PULLUP);
  • Reads the state of the ESP32 pin by using digitalRead() function.
int inputState = digitalRead(18);

※ NOTE THAT:

There are two wide-used use cases:

  • The first: If the input state is HIGH, do something. If the input state is LOW, do another thing in reverse.
  • The second: If the input state is changed from LOW to HIGH (or HIGH to LOW), do something.

Depending on the application, we choose one of them. For example, in case of using a touch sensor to control an LED:

  • If we want the LED to be ON when the sensor is touched and OFF when the sensor is NOT touched, we SHOULD use the first use case.
  • If we want the LED to be toggle between ON and OFF each time we touch the sensor, we SHOULD use the second use case.
  • How to detect the state change from LOW to HIGH
// constants won't change. They're used here to set pin numbers: const int SENSOR_PIN = 18; // GIOP18 pin that connects to the sensor's SIGNAL pin // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: currentState = digitalRead(SENSOR_PIN); if(lastState == LOW && currentState == HIGH) Serial.println("The sensor is touched"); // save the the last state lastState = currentState; }

Touch Sensor - ESP32 Code

We will run four example codes:

  • Reads the value from the touch sensor and print to the Serial Monitor.
  • Controls LED according to the sensor's state.
  • Detects the sensor is touched or released.
  • Toggles LED when the sensor is touched (This is the most common-used).

Reads the value from the touch sensor and print to the Serial Monitor

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-touch-sensor */ // constants won't change. They're used here to set pin numbers: const int SENSOR_PIN = 18; // GIOP18 pin that connects to the sensor's SIGNAL pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: int state = digitalRead(SENSOR_PIN); // print state to Serial Monitor Serial.println(state); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Touch your finger to the sensor and release.
  • See the result on Serial Monitor. It looks like the below:.
  • COM6
    Send
    0 0 0 1 1 1 1 1 1 0 0
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Controls LED according to the sensor's state

If the sensor is touched, turn LED on. If the sensor is not touched, turn LED off.

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-touch-sensor */ // constants won't change. They're used here to set pin numbers: const int SENSOR_PIN = 18; // GIOP18 pin that connects to the sensor's SIGNAL pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32's pin as aninput pinMode(SENSOR_PIN, INPUT); // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } void loop() { // read the state of the the input pin: int state = digitalRead(SENSOR_PIN); // control LED according to the sensor's state digitalWrite(LED_BUILTIN, state); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Touch your finger to the sensor and keep touching.
  • See LED state ⇒ LED should be on.
  • Release your finger from the sensor.
  • See LED state ⇒ LED should be off.

Detects the sensor is touched or released

// constants won't change. They're used here to set pin numbers: const int SENSOR_PIN = 18; // GIOP18 pin that connects to the sensor's SIGNAL pin // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: currentState = digitalRead(SENSOR_PIN); if(lastState == LOW && currentState == HIGH) Serial.println("The sensor is touched"); else if(lastState == HIGH && currentState == LOW) Serial.println("The sensor is is released"); // save the the last state lastState = currentState; }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Touch your finger to the sensor and keep touching.
  • See the result on Serial Monitor. It looks like the below:.
  • COM6
    Send
    The sensor is touched
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • Release your finger from the sensor.
  • See the result on Serial Monitor. It looks like the below:.
  • COM6
    Send
    The sensor is touched The sensor is is released
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Toggles LED when the sensor is touched.

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-touch-sensor */ // constants won't change. They're used here to set pin numbers: const int SENSOR_PIN = 18; // GIOP18 pin that connects to the sensor's SIGNAL pin // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin int ledState = LOW; // the current LED state void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32's pin as aninput pinMode(SENSOR_PIN, INPUT); // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } void loop() { // read the state of the the input pin: currentState = digitalRead(SENSOR_PIN); if(lastState == LOW && currentState == HIGH){ // toggle LED state if(ledState == LOW) ledState = HIGH; else if(ledState == HIGH) ledState = LOW; // control LED digitalWrite(LED_BUILTIN, ledState); } // save the the last state lastState = currentState; }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Touch your finger to the sensor and release.
  • See LED state ⇒ LED should be on.
  • Touch your finger to the sensor and release.
  • See LED state ⇒ LED should be off.
  • Touch your finger to the sensor and release.
  • See LED state ⇒ LED should be on.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Alternative Touch Sensors

The above code also works with the following touch sensors:

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere