Dear Rui
I just finished the touch sensor tutorial but got stuck in a very basic thing
If I want to use the module to emulate a keypad
How about a simple sketch to read each touch pin and depending on the state of the pin read do something.
As example if I touch the touch sensor 0 turn on led 1, if I touch the touch sensor 1 turn on led 2
Thanks
Hi Belen.
For that you need to create several touchPin varibales, for example touchPin1, touchPin2, etc… Then, you need to create a threshold value to each touch pin. And then, create several ledPin variables for each LED.
Then, in the loop(), you would need several if statements to check if the value of each touchPin is above or below the threshold and turn the LED on or off. For example:
if(touchValue1 < threshold1){ // turn LED 1 on digitalWrite(ledPin1, HIGH); Serial.println(" - LED 1 on"); } if(touchValue2 < threshold2){ // turn LED 2 on digitalWrite(ledPin2, HIGH); Serial.println(" - LED 2 on"); }
… And so on.
I hope this is helpful 🙂
There is also a sketch example if you go to File>Examples>ESP32>Touch>TouchInterrupt.
That sketch executes a callback function when the value read on a touchpin goes below the threshold.
I think this sketch might be more helpful.
🙂
Dear Sara
Thank you fro answering my question and I’m sorry I did not replied before
I did as you told me and here is my sketch and please keep on reading since something happened
**********************
/ set pin numbers
const int touchPin_1 = 4; // TouchPin 0 connected to GPIO4
const int touchPin_2 = 2; // TouchPin 2 connected to GPIO2
const int ledPin_1 = 16; // Led connected to GPIO 16
const int ledPin_2 = 21; // Led connected to GPIO 21
// change with your threshold value for each pin
const int threshold_1 = 20;
const int threshold_2 = 10;
// variable for storing the touch pin value
int touchValue_1;
int touchValue_2;
void setup(){
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
// initialize the LED pins as an output:
pinMode (ledPin_1, OUTPUT);
pinMode (ledPin_2, OUTPUT);
}
void loop(){
// read the value of Touchpin_1
touchValue_1 = touchRead(touchPin_1);
Serial.print(touchValue_1);
// check if the touchValue is below the threshold
// if it is, set ledPin to HIGH
if(touchValue_1 < threshold_1){
// turn LED on
digitalWrite(ledPin_1, HIGH);
Serial.println(” – LED on”);
}
else{
// turn LED off
digitalWrite(ledPin_1, LOW);
Serial.println(” – LED off”);
}
delay(500);
// read the value of Touchpin_2
touchValue_2 = touchRead(touchPin_2);
Serial.print(touchValue_2);
// check if the touchValue is below the threshold
// if it is, set ledPin to HIGH
if(touchValue_2 < threshold_2){
// turn LED on
digitalWrite(ledPin_2, HIGH);
Serial.println(” – LED on”);
}
else{
// turn LED off
digitalWrite(ledPin_2, LOW);
Serial.println(” – LED off”);
}
delay(500);
}
****************
But something really weird happens, after I run the sketch and make the proper connections of pins and leds, the LedPin_2 stays lit forever it doesn’t matter if I touch it or not again it remains lit.
I changed the let and the code to another pin and now the led stays lit at that pin
If I change back the led to the previous pin is lit without coding like I got voltage over the pin
Is really weird you should try it and see
Do you happen to know what might be wrong?
Thanks!
Hi again.
The problem is not with the LED pin but with the touchpin.
The touchRead() function is always reading 0 on the Touch 2 (GPIO2) pin. So, the value is always below the treshold, and the LED is always lit.
I have no idea why that happens on that specific Touch pin, I’ll have to investigate and try to find out what’s causing this behavior.
Meanwhile, experiment with other touch pin and see what you get.
I hope this helps.
Thank yous Sara! That was making me crazy
here is the code that worked with different threshold values
Start your code here
// set pin numbers
const int touchPin_1 = 15; // TouchPin 0 connected to GPIO4
const int touchPin_2 = 4; // TouchPin 2 connected to GPIO2
const int ledPin_1 = 5; // Led connected to GPIO 5
const int ledPin_2 = 21; // Led connected to GPIO 21
// change with your threshold value for each pin
const int threshold_1 = 15;
const int threshold_2 = 20;
// variable for storing the touch pin value
int touchValue_1;
int touchValue_2;
void setup(){
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
// initialize the LED pins as an output:
pinMode (ledPin_1, OUTPUT);
pinMode (ledPin_2, OUTPUT);
}
void loop(){
// read the value of Touchpin_1
touchValue_1 = touchRead(touchPin_1);
Serial.print(touchValue_1);
// check if the touchValue is below the threshold
// if it is, set ledPin to HIGH
if(touchValue_1 < threshold_1){
// turn LED on
digitalWrite(ledPin_1, HIGH);
Serial.println(” – LED on”);
}
else{
// turn LED off
digitalWrite(ledPin_1, LOW);
Serial.println(” – LED off”);
}
delay(500);
// read the value of Touchpin_2
touchValue_2 = touchRead(touchPin_2);
Serial.print(touchValue_2);
// check if the touchValue is below the threshold
// if it is, set ledPin to HIGH
if(touchValue_2 < threshold_2){
// turn LED on
digitalWrite(ledPin_2, HIGH);
Serial.println(” – LED on”);
}
else{
// turn LED off
digitalWrite(ledPin_2, LOW);
Serial.println(” – LED off”);
}
delay(500);
}