Very simple question Touch sensor

bmetasonic
Posts: 1
Joined: Fri Apr 06, 2018 4:21 am

Very simple question Touch sensor

Postby bmetasonic » Fri Apr 06, 2018 4:31 am

Hi guys
This is my first post here.

I'm doing some learning and one of my first examples is getting to know the ESP32 touch sensors

The idea is that I got a cable connected to each touch pin and when I touch the pin a led connected to another GPIO turns on

I got the cables connected to each pin, each led with proper resistor to the output pins and I'm using the ESP32 Doit Dev Board V1

Something really weird is happening after the led of the second touch pin turns on it stays on is like it has now a short and it doesn't matter thr threshold of the sensitivity of the pin
Even if I change the led pin to another GPIO after I run again the modified sketch and do the excerise not the new pin stays lit

Here is my code and I don't know what is wrong

Code: Select all


/ 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!

chegewara
Posts: 2361
Joined: Wed Jun 14, 2017 9:00 pm

Re: Very simple question Touch sensor

Postby chegewara » Fri Apr 06, 2018 12:19 pm

Could you change logging level to debug and share logs?

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 73 guests