Project 3 : Working with Internal Sensor

Gresya Leman
12 min readFeb 18, 2022

Hello everyone!
Welcome to the third chapter of me learning on how to use ESP32. In this part, I’ll show you how I utilized ESP32’s built-in sensors.

All of ESP32 has 2 kinds of built-in sensors (touch sensor and hall effect sensor), and most of them has internal temperature sensor. In this case, DOIT ESP32 DevKit V1 has internal temperature sensor that we can try to use later in this tutorial.

Here’s how I used these 3 built-in sensors in my ESP32.

Necessary Components

In all of these tutorials, there’ll be some differences in required components, but these are the common necessary components for this tutorial.

ESP32+Breadboard
In this project I used ESP32 DevKit V1, but you can use any kind of ESP32. It’s better to have your ESP32 secured on a breadboard to make the arrangement easier to do.

ESP32 on a breadboard

Micro USB Cable
This cable will be used as a connector between our device and the ESP32.

Micro USB Cable

Laptop/PC
Use the device of your choice, in this project I used my laptop which is Asus TUF Gaming A15.

Laptop

PART 1 : TOUCH SENSOR

In this part, we’re going to utilize and analyze the built-in touch sensor on ESP32. The pink boxes shows where the touch sensors are available on the ESP32.

source : https://randomnerdtutorials.com/

First, we need to check the touch sensor.

To do that we only need one male-to-male jumper cable attached on the breadboard next to pin number 4.

Then go to Arduino IDE, on the top left, click on the Files > ESP32 > Touch > TouchRead.

A new window will pop up, then click on the tick icon on the top left of the Arduino IDE to compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

After it’s done uploading, go to Tools > Serial Monitor.

A new window will pop up, then set the serial to 115200 baud. Now a serial of numbers should show on that tab. The numbers should be somewhere above 65 when the open tip of the male-to-male jumper cable is untouched. But when it is touched, the numbers should go below 20.

Or go to Tools > Serial Plotter to see the graph for a better view and don’t forget to set it to 115200 baud. The spike to the numbers below 20 means that the tip of the male-to-male jumper cable is touched, and the spike to the numbers above 60 means that the tip is untouched.

Here’s a video of me trying it on my laptop.

PART 2 : HALL EFFECT SENSOR

In this part we’re going to utilize and analyze the built-in hall effect sensor on ESP32. The built-in hall effect sensor is located behind the metal lid of the ESP32 chip as shown in the image below.

source : https://randomnerdtutorials.com/

Get the ESP32 secured on a breadboard and connected to the device with a micro USB cable.

Open the Arduino IDE, on the top left click on the Files > ESP32 > Hall Sensor.

A new window will pop up, add ‘delay(1000);’ on the last part of the void loop() part, then click on the tick icon on the top left of the Arduino IDE to compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

After it’s done uploading, go to Tools > Serial Monitor.

A new window will pop up, then set the serial to 9600 baud. Now a serial of numbers should show on that tab. The numbers should be somewhere around 0 to 100 on ‘normal’ conditions. Keep in mind that the hall effect sensor is affected by electronical devices around it, so the ‘normal’ for me might not be the same for you. When the magnet’s north (positive) pole of a magnet is near the sensor, the numbers should spike up above 100 and maybe even 200 depending on how strong and how close the magnet is. When the magnet’s south pole (negative) of a magnet is near the sensor, the numbers should spike down to negatives. The closer the magnets are, the greater the absolute values.

Or go to Tools > Serial Plotter to see the graph for a better view. The spike to the numbers above 100s means that the magnet’s north (positive) pole is near the sensor, and the spike to the numbers below 0 means that the magnet’s south (negative) pole is near the sensor.

Here’s a video of me trying it on my laptop.

PART 3 : INTERNAL TEMPERATURE SENSOR

In this part we’re going to utilize the built-in internal temperature sensor on ESP32.

Get the ESP32 secured on a breadboard and connected to the device with a micro USB cable.

Open the Arduino IDE, then copy and paste the codes below.

#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();#ifdef __cplusplus
}
#endif
uint8_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);
}

Compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

After it’s done uploading, go to Tools > Serial Monitor.

A new window will pop up, then set the serial to 115200 baud. ‘Temperature : <internal temp>” should show on that tab. In my case it’s pretty stable on the 53.3 degree Celsius, because i haven’t been using it.

Here’s a video of me trying it on my laptop.

After I’m done trying all the built-in sensors, I made one variation utilizing touch sensor and one variation using hall effect sensor.

— BONUS TUTORIAL —

UTILIZING TOUCH SENSOR

Using the data that ESP32’s touch sensors produce numbers below 20 when it’s touched, we can make a simple program to turn on LED lights with this built-in touch sensor.

Additional components required

5mm LED lights (3 pcs), here I used red, yellow, and green LED lights to replicate traffic lights.

330 Ohm Resistor (3pcs)

Male-to-Male Jumper Cable (7 pcs), this ia apart from the first male-to-male jumper cable we used for the touch sensor. So in total there are 8 male-to-male jumper cable required.

Arrangements on the breadboard

Make sure the ESP32 is secured on the breadboard. Then attach the LED lights with some space between them to put the resistors and jumper cables.

Attach the resistors to a spot next to the positive leg of the LED lights and connect it to a spot on an empty row to connect to the jumper cable later on.

Connect a jumper cable from a spot near the other end of the resistor to a spot next to the pin on the ESP32 that are used. Here I used GPIO 21, 22, and 23 for the LED lights.

Then connect another jumper cable from a spot next to the negative leg of the LED lights and connect it to the negative side on the board.

Attach a jumper cable from the spot next to GND pin and attach it to the negative side of the board, connecting to other jumper cables on the negative side of the board.

Then finally attach the last jumper cable on a spot next to a pin capable for touch sensor, here I used pin D4. Then connect the micro USB cable to the device. The whole circuit should look like this.

Code the Program

Open the Arduino IDE and make a new file. Save the file where ever you like it.

Type in the code as below.

// set pin numbers
const int led1 = 21;
const int led2 = 22;
const int led3 = 23;
const int touchPin = 4;
// 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);
// initialize the LED pin as an output:
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, 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(touchValue < threshold){
// turn led1 on
digitalWrite(led3, LOW);
digitalWrite(led1, HIGH);
delay(200);
// turn led2 on
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(200);
//turn led3 on
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(100);
}else{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
delay(100);
}

Compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

Now the LED light should turn on alternately with 200ms intervals when the open end of the jumper cable is touched. The result will show up as in the video attached below.

UTILIZING HALL EFFECT SENSOR

Using the data that ESP32’s hall effect sensors produce numbers below 0 or go over 100 when it detects a magnet nearby, we can make a simple program to turn on a serial of LED lights and an active buzzer with the hall effect sensor, here I used SOS pattern so that if it detects the north (positive) pole, it’ll go from red-yellow-green, and when it detects the south (negative) pole it’ll go from green-yellow-red. The buzzer will go off simultaneously with any of the LED light when it’s turned on.

Additional components required

Since I made this after making the bonus on the touch sensor variation, and I wanted to play with a buzzer for a little, I needed one active buzzer and 2 male-to-male jumper cable. I also needed to take off the jumper cable that was used for the touch sensor since it’s not required for using hall effect sensor.

Arrangements on the breadboard

Starting off by removing the jumper cable that was on the D4 pin from the previous circuit.

Attach the active buzzer on the breadboard, here I positioned the buzzer’s positive leg on the right side since it will be connected to a GPIO pin on the ESP32 which is further on the right so my jumper cable could reach it.

Then attach a jumper cable connecting the buzzer’s positive and a GPIO pin on the ESP32, here I used pin 19.

Attach another jumper cable connecting the buzzer’s negative side with the negative side of the breadboard.

At this point, the circuit is pretty much done, the last step remaining is to connect the ESP32 board with the laptop using micro USB cable. Here’s how the overall circuit looks like connected to the laptop.

Code the Program

Open the Arduino IDE and make a new file. Save the file where ever you like it.

Type in the code as below.

// set pin numbers
const int led1 = 21;
const int led2 = 22;
const int led3 = 23;
const int buzzer = 19;
int val = 0;
// set threshold values
const int upthreshold = 100;
const int downthreshold = 0;
void setup(){
Serial.begin(9600);
delay(1000); // give me time to bring up serial monitor
// initialize the LED pin as an output:
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (buzzer, OUTPUT);
}
void loop(){
val = hallRead();
Serial.println(val);
if(val > upthreshold){
flash(1, 200); flash(1, 200); flash(1, 200); //S
delay(300);
flash(2, 500); flash(2, 500); flash(2, 500); //O
flash(3, 200); flash(3, 200); flash(3, 200); //S
delay(500);
}
else if(val < downthreshold){
flash(3, 200); flash(3, 200); flash(3, 200); //S
delay(300);
flash(2, 500); flash(2, 500); flash(2, 500); //O
flash(1, 200); flash(1, 200); flash(1, 200); //S
delay(500);
}
else{
// turn LED off
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(buzzer, LOW);
}
delay(500);
}
void flash(int led, int duration){
if(led == 1){
digitalWrite(led1, HIGH);
digitalWrite(buzzer, HIGH);
delay(duration);
digitalWrite(led1, LOW);
digitalWrite(buzzer, LOW);
delay(duration);
}else if(led == 2){
digitalWrite(led2, HIGH);
digitalWrite(buzzer, HIGH);
delay(duration);
digitalWrite(led2, LOW);
digitalWrite(buzzer, LOW);
delay(duration);
}else if(led == 3){
digitalWrite(led3, HIGH);
digitalWrite(buzzer, HIGH);
delay(duration);
digitalWrite(led3, LOW);
digitalWrite(buzzer, LOW);
delay(duration);
}
}

Compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

Now the LED light should turn on alternately making the SOS pattern when the sensor detects magnet near it. The red-yellow-green pattern when the sensor detects the positive pole and green-yellow-red pattern when the sensor detects the negative pole meanwhile the active buzzer will make a sound when any of the LED light turned on. The result will show up as in the video attached below.

This is the end of the tutorial and I hope this helps!

I didn’t really face any significant struggles doing this project, but I struggled for a bit because I didn’t know where my magnet’s north (positive) and south (negative) poles were. Once I figured it out, it was pretty easy to decide the threshold value and experimented with it. The rest of it went pretty smoothly and I am satisfied with how my experiment turned out.

See you in my next projects!

--

--