Capacitive Touch, need send only 1 message on push/release

Hello everyone,
I'm Trying to setup 8 touch pins on the esp32 for a keypad I'm trying to build. The idea being when a button is pressed (touched) , A message to be sent out, and when released another message gets sent.

I'm having a bit of trouble figuring out how to accomplish this. with my current code, when a button is pressed, I get continuous messages for press/release until I let go.

here is my code as it is (I'm only focusing on the 1st touch pin at the moment):

This is un example howto use Touch Intrrerupts
The bigger the threshold, the more sensible is the touch

int threshold = 30;
bool touch1detected = false;
bool touch2detected = false;
bool touch3detected = false;
bool touch4detected = false;
bool touch5detected = false;
bool touch6detected = false;
bool touch7detected = false;
bool touch8detected = false;

bool TP1State = false;
bool TP1LastState =false;

void gotTouch1(){
 touch1detected = true;
void gotTouch2(){
 touch2detected = true;
void gotTouch3(){
 touch3detected = true;
void gotTouch4(){
 touch4detected = true;
void gotTouch5(){
 touch5detected = true;
void gotTouch6(){
 touch6detected = true;
void gotTouch7(){
 touch7detected = true;
void gotTouch8(){
 touch8detected = true;

void setup() {
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Interrupt Test");
  touchAttachInterrupt(T0, gotTouch1, threshold);
  touchAttachInterrupt(T3, gotTouch2, threshold);
  touchAttachInterrupt(T4, gotTouch3, threshold);
  touchAttachInterrupt(T5, gotTouch4, threshold);
  touchAttachInterrupt(T6, gotTouch5, threshold);
  touchAttachInterrupt(T7, gotTouch6, threshold);
  touchAttachInterrupt(T8, gotTouch7, threshold);
  touchAttachInterrupt(T9, gotTouch8, threshold);

void loop(){

  /////////// need help with this section
  if(touch1detected) TP1State = true;
  else if(!touch1detected)TP1State = false;
  if(TP1State && !TP1LastState)Serial.println("Touch 1 Pressed"); //this message should only be sent once when pin is touched
  if(!TP1State && TP1LastState)Serial.println("Touch 1 Released"); // this message should only be sent once after touch is removed
  TP1LastState = TP1State;
    touch2detected = false;
    Serial.println("Touch 2 detected");
    touch3detected = false;
    Serial.println("Touch 3 detected");
    touch4detected = false;
    Serial.println("Touch 4 detected");
    touch5detected = false;
    Serial.println("Touch 5 detected");
    touch6detected = false;
    Serial.println("Touch 6 detected");
    touch7detected = false;
    Serial.println("Touch 7 detected");
    touch8detected = false;
    Serial.println("Touch 8 detected");

If you poll the touch pins using the touchRead() function instead of using an ISR you can determine that a sensor has become touched rather than being currently touched.

if i use touchRead() for some reason I only get values from one of the pins (T3), which is why I abandoned that and went for the interrupts, also seems much more reliable (no false positives) ,

Never mind, you are right, it was my code. got it all working now, thanks for nudging me in the right direction.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.