Embedded System Project 3: ESP32 Internal Sensors

Michelle Lim
7 min readFeb 21, 2023

--

Hi, I’m Michelle and today I’ll be continuing my blog series of the embedded systems project! ✨🤩 (You can read the previous blog here 🤗)

The third project I’ll be doing is about internal sensors that is in ESP32. A quick intro: there are 3 internal sensors inside an ESP32 development board, which are the Touch sensor, Hall Effect sensor, and Temperature sensor. Most ESP32 development boards usually only have the hall effect sensor and the touch sensor, not all of them have a temperature sensor (but we’ll still try it later on 😉). These sensors are usually used to figure out the internal condition inside the ESP32 device, called proprioceptive sensor.

Tools (you can refer to my previous blogs for the marketplace):

  1. ESP32 development board
  2. Micro-USB Cable
  3. Breadboard (preferably 800 points, but 400 points will do just fine)
  4. Male to Male jumpers
  5. 5mm LED lights (prepare 2 colors of your own choice!)
  6. Resistors of 330 Ohm and 10K Ohm
  7. Laptop with installed and set Arduino IDE
  8. Magnet

1. Touch Sensor

Touch sensors are sensors that will respond when it’s touched. The ESP32 has 10 capacitive touch GPIOs. These GPIOs can sense variations in anything that holds an electrical charge, like our skin for example! 😀 These pins can be easily integrated into capacitive pads and replace mechanical buttons. It can also be a wakeup source when ESP32 is in deep sleep. Do take a look at your board to locate the 10 different touch sensors (Touch0 — Touch9) that are highlighted in pink.

Touch sensitive pins are highlighted in pink

Reading the Touch Sensor

Open the code in Arduino IDE: File > Examples > ESP32 > Touch > TouchRead

TouchRead sketch

Code explanation: it reads the touchPin 0 (T0) which is on GPIO4 and displays the results in the serial monitor.

To test the sketch, go to Tools > Serial Monitor > Set baud rate to 115200. After that, touch the wire connected to GPIO4 and you’ll see the values decreasing. To see better plot, go to Tools > SerialPlotter.

the wire connected to GPIO4 in ESP32, screen capture of serial plotter and monitor
values decreasing from 67 to 24 when touched

Touch Sensitive LED

After experimenting on the TouchRead, I decided to follow RandomNerdTutorials to try experimenting the touch sensors with LED.

I followed the code and schematics as shown on the picture below, and connected my LED to GPIO16.

code and schematics from RandomNerdsTutorial

Code explanation: the code reads input from touchPin and turns the LED on and off based on the touchValue. If the touchValue is below the threshold, the LED will be turned on, and vice versa.

Touch sensor for 1 LED (touchpin: GPIO4)

Modifications — Touch Sensor with 2 LEDs

After learning from the two previous experiments, I decided to add an LED and experiment on the touch sensor in switching between two LEDs.

The schematics is pretty much similar to the previous one with 1 LED, with just a little bit of additions and modifications:

I added a blue LED on GPIO5, added a 10K Ohm resistor to it, and changed the touch pin to GPIO15 (TOUCH3)

Wires used — Blue wire: ground wire; Orange wire: Touch wire (GPIO15 pin); Yellow wire: GPIO16 to Green LED; Purple wire: GPIO5 to Blue LED; Green & Grey wire: connects to ground (negative bus terminal).

I also modified the code:

Code for Touch Sensors with 2 LEDs (increased the threshold)

Code explanation: When the touchValue read is below threshold, indicating that the wire is touched, the green LED will be turned on and the blue LED will be turned off. On the contrary, when the wire isn’t touched, the blue LED will be turned on and the green LED will be turned off.

Touch sensor for 2 LEDs

In conclusion, both GPIO4 and GPIO15 works just as fine. I also modified the resistor only to decrease the brightness of Blue LED (you can actually use 330 Ohm resistors for both LEDs) 😉

2. Hall Effect Sensor

The ESP32 development board features a built-in hall effect sensor that detects changes in the magnetic field in its surroundings. The greater the magnetic field, the greater the sensor’s output voltage.

Reading the Hall Effect Sensor

Open the code in Arduino IDE: File > Examples > ESP32 > HallSensor

HallSensor sketch

Code explanation: it reads the hall sensor measurements and displays them on serial monitor. I added a delay(1000) so that I can read the values easily.

To test the sketch, go to Tools > Serial Monitor > Set baud rate to 9600. After that, put the magnet near to the sensor and see the values increasing or decreasing depending on the magnet pole that is facing the sensor. The closer the magnet is to the sensor, the greater the absolute values are.

reference of what you should see when you interchange the poles that are facing the sensor
values increasing and decreasing with different magnet poles facing the sensor

Modifications — Hall Effect Sensor with 2 LEDs

The schematics is pretty much the same as the modifications made in Touch Sensor with 2 LEDs. However, I removed the wire to the touchpin (orange wire):

schematics modified for Hall-Effect Sensor with 2 LED; I removed the orange wire

I also modified the code:

Code for Hall Effect Sensors with 2 LED

Code explanation: When the hallValue is below -5, we assume the negative pole is facing the sensor and turn on the green LED, turn off the blue LED. We assume otherwise (the positive pole is facing the sensor) when the hallValue is above 4, and turn on the blue LED, turn off the green LED. Besides that, we assume it’s neutral and turn both LEDs off.

Hall Effect Sensor with 2 LEDs

In conclusion, when the ESP32 detects the negative pole, the monitor shows a negative value. Meanwhile, when it detects the positive pole, it shows a positive value. After analyzing more detections, I found that the range of hallValue when there is no magnet nearby is actually quite small, between -5 and 4, hence the range I used in the code. However, I can’t conclude anything from the range of hallValue that I chose (such as the meaning of the numbers), as I do not know about the exact magnetic force. 😊

3. Temperature Sensor

As I’ve said before, most ESP32 development boards, including mine unfortunately, doesn’t have an internal temperature sensor. However, since I was curious, I followed the code from theengineeringprojects.com, connected the ESP32 to my laptop, compiled and uploaded the program (no circuits needed in this experiment 😆):

Temperature sensor code and value printed on serial monitor

As seen on the picture above, the temperature value printed is constantly at 53.33˚C. This aligns with what is stated above, that ESP32 has no internal built-in temperature sensor. Because if it does have, a slight change in temperature would be detected. But since it doesn’t, the function temprature_sens_read() will always return the value 128 (in Fahrenheit), 53.33˚C.

In conclusion, the ESP32 development board has several internal sensors that we have explored, such as Touch sensor, Hall Effect sensor and Temperature sensor. Even though it is unfortunate that I didn’t get to explore more about the temperature sensor due to my unsupportive ESP32, I have learnt a bunch of stuff through these experiments (which I hope you do too). 😊

So yea, we’re finally done with our third Embedded System Project: ESP32 Internal Sensors. (yeah!! 🥳) Stay tune for the next projects and stay safe and healthy 🥰

--

--