Several of the esp32 pins can also be used to read differences in the capacitance at the pin. This is almost like reading the presence of a charge. touchRead(pin) uses an FSM (Finite State Machine) which "charges" and "discharges" the pin by essentially adding and then subtracting electrons very quickly. The moment you touch a pin there is s surge in the number of electrons that can be absorbed and so the charge cycle takes a bit longer. This increase in charge time is detected as a touch. Conversely, when you let go, the pin suddenly takes a bit longer to discharge. This is also detected as change back to the untouched state.
To see this in action we will use the same setup we just used in digitalRead along with the LED we used in digitalWrite and analogWrite. Instead of connecting the blue wire to Vss or GND we will simply touch the copper. GPIO15 where the blue wire is connected is also called Touch3. This Dev Kit has 9 Touch pins.
In this example we are also going to use the Serial Monitor - a utility that lets the esp32 send messages back to your computer while it is running the sketch. Use medium length jumper wires for the blue and white wires shown below. They can be any colour.
// ESP32 Touch Test
// M Druiven, Jan 31, 2021
int threshold = 45;
void setup() {
Serial.begin(115200);
delay(1000); // allow time to bring up serial monitor
Serial.println("ESP32 Touch Test");
pinMode(13,OUTPUT);
}
void loop() {
Serial.println(touchRead(15));
/* Get value of Touch 3 pin = GPIO 15
You may need to adjust the threshold variable
depending on what you are seeing on your
serial monitor */
if(touchRead(15)<threshold){
digitalWrite(13,HIGH); //LED on
}
else{
digitalWrite(13,LOW); //LED off
}
delay(100);
}
SAFETY GLASSES ON!
When we run this sketch we should see the LED turn on when we touch the blue wire.
Click on Tools ==> Serial Monitor
Make sure the baud rate (lower right) is set to 115200.
The serial monitor should open and you will see numbers scrolling by and change when you touch the blue wire. Make note of the "touched" and "untouched" values in case you need to modify the sketch shown here.
Now hold the white wire (your colour may differ) in one hand and the blue wire in the other. Notice the numbers are different. The "touched" value goes lower. Your body can soak up more electrons since it is connected to ground.
Try This
Tape the end of the blue wire (your colour may be different) to the robot chassis. Now all you need to do is to touch anywhere on the chassis to change the capacitance at the touch pin. Make note of the new "touched" and "untouched" values. You should be able to turn the LED on with a touch to the chassis.
How can we turn the LED on with a touch and then off with the same touch? Read about boolean variables below.
Unplug your USB cable.
A boolean is a special variable type.
int - Integer using 16 bits, 2 bytes, -32,768 to 32,76, 7
float - Floating point number using 32 bits, 4 bytes, 6 or 7 digits, can loose precision
You can learn more about Structures, Variable and Functions in the Arduino reference. Click on Help ==> Reference
Booleans have only 2 values, true or false. Booleans can be "switched" using NOT - !
boolean boo = true; //boo is true
boo = !boo; // now boo is false
Remember IF?
if(check){
do something
}
... will do something if check is TRUE. "check" can be a mathematical expression like if(a==b){
or
check can be a boolean.
If check is a boolean variable then if(check){ do something } will work because the IF simply evaluates everything in the brackets to true or false.
Look at this code carefully. The second if statement is inside the first if statement. This is called "nesting" First we need to touch the input. If that happens then we check to see if it is already on or off and we switch the state. If it was on we turn it off. If it was off, we turn it on. The "ifs" are nested the inner code won't run until the outer if is true.
SAFETY GLASSES ON!
// ESP32 Touch On, touch Off
// M Druiven, Jan 31, 2021
// GPIO15 input jumper taped to robot chassis
int threshold = 11; // declare an integer variable called threshold and set it to 11
boolean ON = false; //assume the LED is off to start
void setup() {
Serial.begin(115200);
delay(1000); // allow time to bring up serial monitor
Serial.println("ESP32 Touch Test");
pinMode(13, OUTPUT);
}
void loop() {
Serial.println(touchRead(15));
/* Get value of Touch 3 pin = GPIO 15
You may need to adjust the threshold variable
depending on what you are seeing on your
serial monitor */
if (touchRead(15) < threshold) {
if (!ON) { // if not on
digitalWrite(13, HIGH); //turn LED on
ON = !ON; //now ON is true
}
else { // well if it is already on then
digitalWrite(13, LOW); // turn LED off
ON = !ON; // now on is false
}
}
delay(200);
// This is our "debounce" otherwise the LED might blink on and off several times
}