Page 1 of 1

Capacitive touch.

Posted: Thu Jul 06, 2017 6:50 am
by harshcht
I was trying out the capacitive touch of ESP-32 using arduino IDE. this is my code :

Code: Select all

void setup()
{
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Test");
}

void loop()
{
  Serial.println(touchRead(T0));  // get value using T0
  delay(1000);
}
(this is an example code in the library itself)

My question is what exactly does the touchRead() function return? It certainly does not work like a switch (as in MPR121) because as I press it with more intensity or use any other surface (metal) the value changes. what exactly is the function returning?

Re: Capacitive touch.

Posted: Fri Jul 07, 2017 5:35 am
by tele_player
Reading a capacitive touch pin returns an analog value which varies with capacitance. To use it, compare that value to a threshold.

Note: they don't all return the same value when not being touched, so I'd use a few reads to calibrate the threshold value.

Re: Capacitive touch.

Posted: Fri Jul 07, 2017 7:58 pm
by Fuzzyzilla
As @tele_player said, this value varies with the capacitance of the touch pin. It will range from about 2~64, with lower numbers meaning, counter-intuitively, more capacitance. To make this work as a button, you could do something like

Code: Select all

if(touchRead(T0)<15){//True if touched, false if not.
  Serial.println("T0 Touched!");
}
You should tune the threshold (15 in this example) depending on the circuit. For instance, a longer wire will end up making the end value lower, possibly false-triggering the output!