Embedded System Project 3: ESP32 Internal Sensor

Fernaldi Fauzie
7 min readFeb 12, 2021

This time, I would like to share about my experience with ESP32 sensor which is internal sensor! (not external sensor). Right now, I am pretty curious to try 3 built-in sensor in ESP32, which is touch sensor, hall effect sensor, and temperature sensor (or maybe just 2 built-in sensor because I heard from my lecture that not all ESP32 has built-in temperature sensor, personally I haven’t try it myself so I can’t say for sure).

What are we waiting for?! Now let’s check it out!

Touch Sensor

I am intrigued to try using ESP32 touch pins with Arduino IDE. For your information, the ESP32 touch pins can sense variations in anything as long it holds an electric charge. Before we start, this is the list of component and device I used:

  • ESP32 Development Board
  • 5mm LEDs (2)
  • 330 Ohm resistors (2)
  • Male to male jumper wires (4)
  • Breadboard
  • Micro USB cable

Now I am done with all component and device that I need to prepare, it’s time to start working in making the electric circuit. You guys may want to see my electric circuit by looking at my schematic diagram below!

Touch Sensor Schematic Diagram

As you can see, I am using 2 LEDs. Similar with my project before, I want that my circuit will blink if I touch my touch sensor, while nothing will happen if I don’t touch my touch sensor, so of course at least 2 LEDs is needed.

I am done with the circuit, now how about the code? No worries! This is my code that I use for this touch sensor project. Please be careful with which pin you can use as capacitive touch GPIO, personally I use pin number 4.

I would say the key of this code is touchValue and threshold. In short, if my touchValue from touchRead(touchPin) is less than the threshold that I put, my LEDs will blink, while if touchValue is more than threshold, turn LED off. While doing this project, you may see the value of touchValue at serial monitor in Arduino IDE which can be opened with 2 alternatives :

  • Tools > Serial Monitor (Ctrl + Shift + M), or
  • Tools > Serial Plotter (Ctrl + Shift + L)

And now here how it’s done!

My Pretty Simple Circuit For Touch Sensor Experiment
First Touch Sensor Demo, Touching The Jumper Wires to Make The LEDs Blink
Second Touch Sensor Demo, Releasing The Jumper Wire to Turn Off LEDs

It seems like if I touch the male to male jumper wire that is used as touch sensor, the value read by sensor drops (as can be seen from the gif above). With this, I can make use of it and threshold value to make something works! (Example : do LEDs blink)

For those wondering what serial plotter looked like (as I am using serial monitor above as you guys can see at my gif), here it is!

Example of Serial Plotter View

To get the result as same as mine, make sure you use the right baud at the serial monitor/serial plotter! For example, because at the code I used 115200 baud, I also have to put the value of baud setting at serial monitor/serial plotter equal to 115200 baud.

Although, sometimes the value can decrease or increase by itself while we literally don’t do anything to it. But, actually that’s pretty normal because that is the reality, there are many external factors that can influence the touch sensor, so don’t worry if that thing happen to you!

I am pretty glad that everything is working as I am expected! With this, I am done with my simple touch sensor project!

But, How About Hall Effect Sensor?

What if I used hall effect sensor with magnet instead of touch sensor to turn on the LED? Is it possible? I am kind of curious so I decide to try it!

For this project, I am still using same components and devices as previous project (touch sensor), but instead of 2 LEDs and 2 resistors, I will use 1 LED and 1 resistor instead.

Simpler Schematic Diagram From Above

At this project, I want to utilize magnet to turn on the LED. This can works because ESP32 has built-in hall effect sensor that detects changes in the magnetic field in its surrounding. The greater the magnetic field, the greater the sensor’s output voltage which can be seen from serial monitor/serial plotter.

To be fair, this code can be said pretty simple and similar with the code above (touch sensor project). In short, the key points are firstThreshold, secondThreshold, and val that store the value of hallRead(). If val is more than firstThreshold or less than secondThreshold, ESP32 will turn on LED, else it will turn off LED. Just keep in mind that instead of using Serial.begin(115200), this time I’m using Serial.begin(9600).

My Pretty Simple Circuit For Hall Effect Sensor Experiment
Putting Magnet to Turn On LED
Changing The Side of Magnet

For your information, magnet can increase or decrease the value of hallRead(), depends on the magnet pole that is facing the sensor. What can be said for sure is the closer the magnet to the sensor, the greater the absolute values are (although mine feels like not really make a great difference, probably because my magnet is too weak, might be because it’s an old magnet already)

It seems like my second experiment in this project also goes well, now we only have 1 more experiment!

Temperature Sensor

Although this sensor is named temperature sensor, but actually this sensor isn’t usable to monitor external temperature. This sensor purpose is to monitor its internal temperature which is its core temperature.

This time, I do this experiment pretty simple. I just want to check my ESP32 internal temperature, so what I need only ESP32 (and of course its USB cable and PC to connect). I believe for this time schematic diagram is not needed, you guys might want to see my taken picture below instead!

BTW, here is the code which to be honest I just take from circuits4you.com

Because what I am going to do is pretty simple, to be fair it can’t be helped. More or less it will pretty similar with the code above, so I will just credit circuits4you.com directly (Of course, I will also credit randomnerdtutorials.com for the references for touch sensor and hall effect sensor above , although I didn’t copy but still used it as reference)

My Experiment For Temperature Sensor

With this project, it seems that my ESP32 has built-in temperature sensor! As we can see that the internal temperature of my ESP32 is 53.33 Celsius.

Conclusion?

To conclude this third project, the main point that I would like to highlights are :

  • I can make blink LEDs by touching the male to male jumper wires by making use of built-in touch sensor, and turn off all LEDs by not touching it. This can be happened because if we touch the touch-sensitive pin, the value read by the sensor drops (can be seen from serial monitor/serial plotter), so we can make use threshold value to make something happen.
  • I can turn on LED by putting magnet near the built-in hall effect sensor and turn off LED by moving magnet away from the built-in hall effect sensor. This can be happened because magnet can increase or decrease the value of hallRead(), depends on the magnet pole that is facing the sensor. What can be said for sure is the closer the magnet to the sensor, the greater the absolute values are, so we also can make use of threshold value to make something happen. (Example : detect proximity, calculate positioning, count the number of revolutions of a wheel, detect a door closing, etc.)
  • My ESP32 has temperature sensor which can show us what is the internal temperature of my ESP32 (not external temperature). This temperature can be seen from serial monitor in Arduino IDE.

I think that’s all for this story about internal sensor. I hope that this can bring you guys more insight and benefit that can be pretty useful. See you guys in the next story!

--

--