Capacitive touch.

harshcht
Posts: 6
Joined: Fri Mar 31, 2017 2:57 am

Capacitive touch.

Postby harshcht » Thu Jul 06, 2017 6:50 am

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?

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Capacitive touch.

Postby tele_player » Fri Jul 07, 2017 5:35 am

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.

User avatar
Fuzzyzilla
Posts: 22
Joined: Sun Apr 23, 2017 3:19 am

Re: Capacitive touch.

Postby Fuzzyzilla » Fri Jul 07, 2017 7:58 pm

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!

Who is online

Users browsing this forum: No registered users and 47 guests