Using ESP touch pin for refined single touch sensing

Hello friends, I am trying to use the ESP touch pin for a capacitive button. For single touch, I need to make it to that single touch would register only once. If I touch and hold, it doesn't register multiple single touches. Adding double touch and touch hold would be very helpful. How can I achieve that?

Current basic code from google:

const unsigned long interval=300;
const unsigned long interval_s=600;    
unsigned long previoustime=0;
int button=0;
void setup() {
 Serial.begin(115200);
}

void loop() {
  unsigned long currenttime=millis(); 
 
if (touchRead(4) < 30){  
  if ((currenttime - previoustime >= interval){
   Serial.write ("TOUCH");
   button=1;
   previoustime=currenttime;
  }
}

I'm not sure if this approach of using a time interval will work for sensing a hold while registering only one touch for a hold, but any help on this would be greatly appreciated!

btw look at this:

You already have libraries with the functions you need, for example : GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses

Sorry thats stupid, I was trying something. But I updated it. Thanks I'll take a look!

Edit: Didn't realize I was using bitwise

1 Like

Touch is a little bit different from a button because the touchread() function returns a constant stream of values. Based on those values I can tell if the button is being pressed. But I only want to register only one touch if it is someone is holding the touch sensor.

The ESP32's Arduino core is wrote on top of the ESP32's API and is not as configurable as the ESP32's API.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/touch_pad.html?highlight=touch%20pad

I have not used the touch sensor API with the Arduino IDE, so I do not know what issues you may encounter and their workarounds.

Good luck.

This does the job for now, but not completely sure if it will work for my use case. I would still appreciate some help on adding double touch.

unsigned long interval=300;
const unsigned long interval_s=600;    
unsigned long previoustime=0;
int button=0;
void setup() {
 Serial.begin(115200);
}

void loop() {
  unsigned long currenttime=millis(); 
 
 if (touchRead(4) < 30){  
  if ((currenttime - previoustime >= interval)){
   Serial.write ("TOUCH\n");
   while(touchRead(4) < 30){
    delay(100);
   }
   previoustime=currenttime;
    }
  }
}

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