ESP32 touch pins unstable and triggering on their own

Hi,
I need help with my Nodemcu esp32s touch pinouts,
so I am trying to integrate the touch pins in a project, and the touch pins are so unstable, the threshold values change, and sometimes even triggering other touchpins even if not touched. I will affix my code below, could someone please point me in the right direction or if possible correct my code please, it's been many months since I've been trying to get my project done and I've only been hitting a wall with this issue as everything else is completed.

I have even tried to skip the delay() function just to prevent any pausing by using the millis() function.

I am no expert in this, it is my first project and I do not know how to move forward.

const int touchPin1 = 4; 
const int touchPin2 = 32;  
const int touchPin5 = 13; 
const int touchPin6 = 12;


const int relay1 = 16; 
const int relay2 = 17; 
const int relay5 = 19; 
const int relay6 = 21;  


const int thresholdKS = 13;
const int thresholdS = 12;
const int thresholdLI = 9;
const int thresholdRI = 9;

int touchValue1;
int touchValue2;
int touchValue5;
int touchValue6;

unsigned long shortpress1 = 500;  //killson
unsigned long longpress1 = 1500; //killsoff
unsigned long shortpress2 = 100;  //enginebutdisable
unsigned long longpress2 = 1500;  //enginebutenable
unsigned long shortpress4 = 200;  //indioff
unsigned long longpress4 = 500;   //indion

unsigned long counter1 = 0;
unsigned long counter2 = 0;
unsigned long counter5 = 0;
unsigned long counter6 = 0;

void setup()
{
  Serial.begin(9600);

  pinMode (relay1, OUTPUT);
  pinMode (relay2, OUTPUT);
  pinMode (relay5, OUTPUT);
  pinMode (relay6, OUTPUT);

  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay5, HIGH);
  digitalWrite(relay6, HIGH);

}

void loop()

 {
  
  // switch touchpin:
  touchValue1 = touchRead(touchPin1);
  Serial.println(touchValue1);
  // check if the touchValue is below the threshold
 
  // Turn on switch
 if(touchValue1 > thresholdKS)
 {
  counter1 = millis();
} 
  if(touchValue1 < thresholdKS)
  {
    unsigned long currentMillis1 = millis();

if((currentMillis1 - counter1 >= shortpress1) && !(currentMillis1 - counter1 >= longpress1))
{
  digitalWrite(relay1, LOW);
  Serial.println("SWITCH ON");
}
if((currentMillis1 - counter1 >= longpress1))
{
    Serial.println("SWITCH OFF");
   digitalWrite(relay1, HIGH);
    }
  
}

  // Start touchpin
  touchValue2 = touchRead(touchPin2);
  
  
  // Start Engine
if(touchValue2 > thresholdS)
 {
  counter2 = millis();
  digitalWrite(relay2, HIGH);
} 
  if(touchValue2 < thresholdS)
  {
    unsigned long currentMillis2 = millis();

if((currentMillis2 - counter2 >= shortpress2) && !(currentMillis2 - counter2 >= longpress2))
{
  digitalWrite(relay2, LOW);
  Serial.println(" START OFF");
  
}

}

 touchValue5 = touchRead(touchPin5);
 if(touchValue5 > thresholdLI)
 {
  counter5 = millis();
}  
  if(touchValue5 < thresholdLI)
  {
    unsigned long currentMillis5 = millis();
    
if((currentMillis5 - counter5 >= shortpress4) && !(currentMillis5 - counter5 >= longpress4))
{
  digitalWrite(relay5, LOW);
  Serial.println(" - LEFT ON");
}
if((currentMillis5 - counter5 >= longpress4))
{
  digitalWrite(relay5, HIGH);  
}
}   



touchValue6 = touchRead(touchPin6);

 if(touchValue6 > thresholdRI)
 {
  counter6 = millis();
} 
  if(touchValue6 < thresholdRI)
  {
    unsigned long currentMillis6 = millis();

if((currentMillis6 - counter6 >= shortpress4) && !(currentMillis6 - counter6 >= longpress4) )
{  
  digitalWrite(relay6, LOW);
  Serial.println(" - RIGHT ON");
}
if((currentMillis6 - counter6 >= longpress4))
{
digitalWrite(relay6, HIGH);
    }
  
}

Thank you so much for your help, will be awaiting your responses.

Please edit your post, select all code a click the </> button to apply code tags; next save the post. They make it easier to read and copy, indentations are preserved and it prevents the forum software from mangling the code.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Hi, @craigjm280710.

I ran into problems with noise and 'glitching' using the capacitive touch inputs on the ESP32. I wrote a little code to de-glitch the data and do some digital smoothing to clean up noise. I put an example together here: GitHub - digamesystems/esp32_capacitive_touch_test: Experimenting with the Capacitive Touch Inputs on the ESP32

'Hope this helps.

Regards,
John

Touch Sensor - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com). The ESP32's Touch APi may be helpful.

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