ESP32 TOUCH SENSOR
MOHAMMAD SAIFIQUL AIMAN B MOHAMMAD ALI
192011145
Here’s a list of parts you need to assemble the circuit
ESP32 DOIT DEVKIT V1 Board
5mm LED
330 Ohm resistor
Breadboard
Jumper wires
touchRead()
Reading the touch sensor is straightforward. In the Arduino IDE, you use the touchRead() function, that accepts as argument, the GPIO you want to read.
Assemble the circuit shown below
// set pin numbers
const int touchPin = 4;
const int ledPin = 16;
// change with your threshold value
const int threshold = 20;
// variable for storing the touch pin value
int touchValue;
void setup(){
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
// initialize the LED pin as an output:
pinMode (ledPin, OUTPUT);
}
void loop(){
// read the state of the pushbutton value:
touchValue = touchRead(touchPin);
Serial.print(touchValue);
// check if the touchValue is below the threshold
// if it is, set ledPin to HIGH
if(touchValue < threshold){
// turn LED on
digitalWrite(ledPin, HIGH);
Serial.println(" - LED on");
}
else{
// turn LED off
digitalWrite(ledPin, LOW);
Serial.println(" - LED off");
}
delay(500);
}
Uploading the Sketch :
Before clicking the upload button, go to Tools Board, and select the board you’re using. In my case. It’s the DOIT ESP32 DEVKIT V1 board. Also don’t forget to select your ESP32’s COM port.
Now, press the upload button.
Then, wait for the “Done uploading.” message:
Testing the example :
Connect a jumper wire to GPIO 4. You will touch the metal part of this wire so that it senses the touch. In the Arduino IDE window, go to Tools and open the Serial Monitor at a baud rate of 115200. You’ll see the new values being displayed every second. Touch the wire connected to GPIO 4 and you’ll see the values decreasing.
You can also use the serial plotter to better see the values. Close the serial monitor, go to Tools > SerialPlotter
Testing your project :
Comments
Post a Comment