ESP32 Internal Sensor
Hello everyone, I'm back! This week I'm going to explain the built-in sensor feature on ESP32 and do experiments with them.
ESP32 has 3 kinds of built-in sensors:
1. Touch sensor
2. Hall Effect sensor
3. Temperature sensor (but not all ESP32 has this sensor)
First, we're going to take a look at the touch sensor. ESP32 has 10 capacitive touch GPIOs that can sense variations in anything that has an electrical charge, such as human skin. You can refer to this pinout reference to know where the touch sensors are.
Now it's time to experiment!
Touch Sensor
First, prepare the components you'll need.
1. ESP32
3. Micro USB
5. Male-to-male jumper
1. Attach the ESP32 on the breadboard.
2. Connect a jumper on the GPIO 4.
Yeah, just like that.
Now, move to your Laptop/PC and do these steps
1. Open arduino.exe.
2. You can use the example provided in the library. Go to File > Examples > ESP32 > Touch > TouchRead.
3. After opening the sketch, don't forget to connect your Laptop/PC with the ESP32 board by using a micro USB cable.
4. Start uploading the code to the ESP32 board.
5. After you're done uploading, open Serial Monitor by going to Tools > Serial Monitor.
6. Set the baud rate to 115200.
7. Try touching the jumper connected to GPIO 4 and you'll see the values decreasing.
See the values decreasing when I'm touching the wire.
8. You can also try opening the Serial Plotter in Tools > Serial Plotter to see the graph representation of the values you saw in the monitor.
Cool, right?
Turning LED on with Touch Sensor
Moving on, let's tinker a bit with the touch sensor and connect it with LED, just like what we've learned in the previous posts. We're going to light the LED by touching the jumper wire.
In this experiment, I'm adding these components apart from the ones I've mentioned before.
Let's arrange the circuitry!
1. LED 5mm
Let's arrange the circuitry!
1. Connect the ground with the negative side of the power rail.
2. Put a resistor and connect it with GPIO 16.
3. Put a LED light connected to the resistor.
4. Connect the LED with the negative side of the power rail.
Moving on to the code~
1. Use this code from randomnerdtutorials
// 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);
}
2. Upload to the ESP32 board.
Now you can try touching the wire to see if it works. You can also open the Serial Monitor and set the baud rate to 115200 to see the status of the sensor and the LED.
Partying LED with Touch Sensor
Another experiment~
I'm trying to use three LEDs and two touch sensors, so you can just add more of those after you prepared the components needed for turning the LED on.
Let's follow these steps:
1. Arrange the additional part. You can add another LED circuit.
2. Add another LED circuit.
3. Connect your board with the laptop/PC.
4. Open arduino.exe
5. I'm using this code:
// set pin numbers
const int touchPin = 4;
const int touchPin2 = 15;
const int ledPin = 16;
const int ledPin2 = 21;
const int ledPin3 = 19;
// change with your threshold value
const int threshold = 25;
// variable for storing the touch pin value
int touchValue;
int touchValue2;
void setup(){
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
pinMode (touchPin, INPUT);
pinMode (touchPin2, INPUT);
pinMode (ledPin, OUTPUT);
pinMode (ledPin2, OUTPUT);
pinMode (ledPin3, OUTPUT);
}
void loop(){
// read the state of the pushbutton value:
touchValue = touchRead(touchPin);
touchValue2 = touchRead(touchPin2);
Serial.println(touchValue);
Serial.println(touchValue2);
if(touchValue < threshold && touchValue2 > threshold){
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
Serial.println("LED 1 on");
Serial.println("LED 2 off");
Serial.println("LED 3 off");
} else if (touchValue > threshold && touchValue2 < threshold) {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
Serial.println("LED 1 off");
Serial.println("LED 2 on");
Serial.println("LED 3 off");
} else if (touchValue > threshold && touchValue2 > threshold) {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
Serial.println("LED 1 off");
Serial.println("LED 2 off");
Serial.println("LED 3 on");
} else {
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
delay(200);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(200);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
delay(200);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
}
delay(500);
}
6. Upload the code to the board.
7. You'll see
- When you're not touching any sensor, the third LED will turn on while the others are turned off.
- When you're touching the first sensor only, the first LED will turn on while the others are turned off.
- When you're touching the second sensor only, the second LED will turn on while the others are turned off.
- When you're touching both sensors, all LEDs will turn on and make some kind of blinking show :)
party time~~~
Hall Effect Sensor
Now let's try experimenting with the hall effect sensor! It is a built-in sensor that detects changes in the magnetic field in its surroundings.
First, prepare the components
5. Magnet
No jumper? Yes! Because the sensor is inside the chip (the silver square on your board) and we can directly use it. Let's follow these steps:
1. Attach the ESP32 board on the breadboard.
2. Connect ESP32 with your laptop.
3. Open arduino.exe. You'll find the code for testing this sensor in File > Examples > ESP32 > HallSensor or just copy this
//Simple sketch to access the internal hall effect detector on the esp32.
//values can be quite low.
//Brian Degger / @sctv
int val = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
val = hallRead();
// print the results to the serial monitor:
//Serial.print("sensor = ");
Serial.println(val);//to graph
delay(1000);
}
Add delay so you can read the values. You can adjust the time.
4. Upload it to the ESP32.
5. After you're done uploading, open the serial monitor or plotter to see the values at baud rate 9600. The closer the magnet to the sensor, the greater the absolute values. I'm using the serial plotter to see the difference better.
Look at how the sensor shows a higher positive value when is close to the positive magnet and a higher negative value when is close to the negative magnet.
???? it only lasts like 1/10 second
As you can see, I'm having a hard time with this sensor. I don't know if it's usually like that or it's just my ESP32 board being faulty so I won't try making anything with this sensor...
But you can try making another experiment, like lighting your LED when the magnet is close.
Temperature Sensor
Alright! Let's move on to the temperature sensor.
As stated before, not all ESP32 have a temperature sensor. According to this website, the temp sensor is obsolete on most of the ESP32, but we'll try anyway :D
The components needed are the same as the hall sensor experiment one, except you won't need a magnet here.
Follow these steps:
1. Connect your ESP32 with the Laptop/PC by micro USB cable.
2. Write this code on arduino.exe
/*
* https://circuits4you.com
* ESP32 Internal Temperature Sensor Example
*/
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
//====================================================
// Setup
//====================================================
void setup() {
Serial.begin(115200);
}
//====================================================
// Loop
//====================================================
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);
}
3. Upload the code and open Serial monitor at baud rate 115200.
You might question why is it constant? I even tried heating the board with a hairdryer until I read that it only shows 53.33°C because the function just returns 128... which means the sensor is not present :)
That's all for this week's experiment!
Hope this helps! <3
Comments
Post a Comment