Hello everyone, today I will show you my third experience with ESP32. In this project, I will use built-in sensor to turn on LED and also buzzer. There will be 3 sensors, which are touch sensor, hall effect sensor and temperature sensor. Before we get into the tutorial, I will show you necessary items to build the system.
- ESP32
2. Micro USB Cable
3. Laptop/PC with Arduino IDE app installed
4. LED Light
5. Male to male jumper cable
6. Resistor 330 Ohm
7. Buzzer
8. Magnet
After you have all of this, you can start the project.
Touch Sensor
In this project I will show you 2 examples, one with LED light and the other one using Buzzer.
LED light
First you need to arrange your breadboard like this :
I put my touch sensor on GPIO 4.
After you arrange the breadboard like that, you need your Arduino IDE for the code. And the code will be like this:
// set pin numbers
const int touchPin = 4;
const int ledPin1 = 16;
const int ledPin2 = 23;// 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 (ledPin1, OUTPUT);
pinMode (ledPin2, 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(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
Serial.println(" - LED on");
}
else{
// turn LED off
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
Serial.println(" - LED off");
}
delay(100);
}
I put the threshold on 20, because when you touch the jumper cable, usually the touch value will go under 20.
After you upload the code your ESP32 will look like this:
Buzzer
In buzzer project, you need to arrange you breadboard like this:
I put my touch sensor on GPIO 4.
After you arrange the breadboard like that, you need your Arduino IDE for the code. And the code will be like this:
// set pin numbers
const int touchPin = 4;
const int buzzer = 23;// 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
pinMode (buzzer, OUTPUT);
}void loop(){
// read the state of the pushbutton value:
touchValue = touchRead(touchPin);
Serial.print(touchValue);
if(touchValue < threshold){
digitalWrite(buzzer, HIGH);
Serial.println(" - buzzer on");
}
else{
digitalWrite(buzzer, LOW);
Serial.println(" - buzzer off");
}
delay(500);
}
Still the same as LED light, I put my threshold at 20.
And the result will be like this :
Hall Effect Sensor
In this project, you will need magnet. If you’re wandering where is the sensor, the sensor is in ESP32, inside the silver square thing in the middle of ESP32.
I’m using LED light as output for this project. To start the project you need to arrange your breadboard like this:
Pretty similar to our first project right? But the difference is in the code. In the first project, there is no sensor, but this one is using hall effect sensor. This the code I use :
// Simple sketch to access the internal hall effect detector on the esp32.
// values can be quite low.
// Brian Degger / @sctvint val;
const int ledPin = 23;
const int threshold = 60;void setup() {
Serial.begin(9600);
delay(1000);
pinMode (ledPin, OUTPUT);
}// put your main code here, to run repeatedly
void loop() {
// read hall effect sensor value
val = hallRead();
Serial.println(val);
if(val > threshold){
// turn LED on
digitalWrite(ledPin, HIGH);
Serial.println(" - LED on");
}
else{
// turn LED off
digitalWrite(ledPin, LOW);
Serial.println(" - LED off");
}
delay(500);
}
I put the threshold on 60. Because when you put your magnet right above the ESP32, mostly the value will go above 60.
After you insert your code on Arduino IDE, the result will look like this:
ps : I’m using refrigerator magnet :)
Temperature Sensor
In this project, you just need your ESP32 and breadboard. That’s it! Because the temperature sensor is in the ESP32. So you just need the code. The code will look like this :
/*
* https://circuits4you.com
* ESP32 Internal Temperature Sensor Example
*/#ifdef __cplusplus
extern "C" {
#endifuint8_t temprature_sens_read();#ifdef __cplusplus
}
#endifuint8_t temprature_sens_read();void setup() {
Serial.begin(115200);
}void loop() {
Serial.print("Temperature: ");
// Convert raw temperature in F to Celsius degrees
Serial.print((temprature_sens_read() - 32) / 1.8);
Serial.println(" C");
delay(1000);
}
You can see the result in Tools → Serial monitor
For addition, all my project above can show result on Serial monitor, so you can check the serial monitor for further information in touch value and hall effect value.
So, this is the end of my experience with ESP32 Built in sensor. Thank you for your attention. In approx. one week, I will update my next experience with ESP32 :)
Pavita Andrea (18220014)