1
\$\begingroup\$

I am trying to implement touch functionality of esp32 development board to toggle a led. But on uploading a simple touch detection sketch, output have spikes i.e. usually value remains near 100 but falls to around 30 randomly for no reason. This triggers false touch. I have tried this with two esp32 dev boards I have and the problem persists in both. Also I have tried this with and without external wire from the touch pin.

Code I've used:-

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println(touchRead(27));
  delay(10);
}

Serial plotter output:-

  1. Without external wire connected with touch pin Without external wire connected with touch pin

  2. With external wire connected with touch pin With external wire connected with touch pin

UPDATE:- 3. Actual touch represented by T1, T2, T3 Actual touch

\$\endgroup\$
  • \$\begingroup\$ It's hard to know what is going on here as 99% of the functionality is not in your code but hiding behind the scenes. But with an apparent cycle time of 100 readings per second, a single-sample glitch cannot reasonably be a touch, and should probably be ignored. Eg, don't act unless you see several readings over a short period of time. Something purporting to be a simple to use touch detection would seemingly already need to do that, but perhaps tuning the time filtering is too need dependent to hide it in the arduino method body, or perhaps the built in filtering is just not good. \$\endgroup\$ – Chris Stratton Oct 4 '20 at 11:54
  • \$\begingroup\$ That doesn't mean there aren't inappropriate sources of electrical noise in your setup, but it's quite possible to write software rules which "look" at your plots and say "nope, that doesn't look like a touch". Doing so is perhaps a bit beyond the scope of an SE answer, but editing your question to include a plot of an actual touch would be an illustrative comparison. \$\endgroup\$ – Chris Stratton Oct 4 '20 at 11:57
  • \$\begingroup\$ Thanks chris-stratton for your interest. I've updated the question with actual touch graph. Actually I had tried filtering false touch by calcualting timings of actual touch but I think that actual touch is very much similar to false one hence sometimes false touch is also considered as actual touch. Anyway please let me know if false touch can be filtered. \$\endgroup\$ – Rishabh Oct 4 '20 at 12:18
  • 1
    \$\begingroup\$ Fatter pulses = touch. Start from there. \$\endgroup\$ – Andy aka Oct 4 '20 at 13:09
  • \$\begingroup\$ please share circuit diagram \$\endgroup\$ – Deepak Oct 4 '20 at 13:12
1
\$\begingroup\$

On studying graph, it is observed that actual touch lasts around 80 to 100 ms and false touch lasts for much less time. Although I had tried some filters before but turns out that was just too complicated, a simple filter works fine (my siliness).

Working code:-

boolean touchStarted = false;
unsigned long touchTime = 0;
int threshold = 90;
int touchMinDuration = 100;

void setup() {
  Serial.begin(115200);
}

void loop() {
  int t = touchRead(27);
  if (t < threshold && !touchStarted) { // on fresh touch
    touchStarted = true;
    touchTime = millis();
  } else if (t >= threshold && touchStarted) { // untouched
    if (millis() - touchTime > touchMinDuration)
      touched;
    touchStarted = false;
  }
  delay(10);
}

void touched(){
  Serial.println("Touched");
}

Thanks to the comments which helped me to come on this solution.

\$\endgroup\$
0
\$\begingroup\$

Just started playing with the touch sensor on ESP32 and Arduino myself and saw your post. I also see unstable readings occasionally. It seems to work much better if I do a dummy read then delay 10 microseconds and then read the real touch value. I have not played much with altering the delay between reads.

Not sure what is going on but it seems to work well with the pre-read dummy read. Extra filtering and a time duration is probably a good idea to add for touch sensing also.

uiTouchByte = touchRead(TOUCH_PIN);  // EXTRA READ IS NECESSARY?
delayMicroseconds(10);

uiTouchByte = touchRead(TOUCH_PIN);  // run the sampling of touch
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.