ESP32 Project #3: Internal Sensor

Hello! I’m back with my 3rd project: internal ESP32 sensor. On this project, I’ll show how internal sensor: touch sensor and half-effect sensor work. Actually there is one more sensor: temperature sensor, but I’m not sure if it will really work on my ESP32. Let’s see!

Touch sensor location on ESP32 board, source: randomnerdtutorials.com

Touch Sensor

We will learn how capacitive sensor (touch) works. If the sensor is touched, the number shown on the serial monitor will be less than the threshold. What is threshold number? To know, I did the 1st trial.

Touch Sensor #1: Basic

We will use:

  • ESP32 Board
  • Arduino IDE
  • Breadboard
  • Jumper wire: male to male
  • USB Cable

Then follow this schematic. Put the ESP32 on the breadboard, then put the jumper wire into GPIO4 (touch sensor 0). Connect the ESP32 with USB cable.

circuito.io

Real pic:

The sketch:

Code for Touch #1

The code will print the value of touch 0 pin (on GPIO 4) on Serial Monitor by touchRead(4). 4 is the GPIO number.

Upload the sketch above and try to touch the male to male jumper wire. Look at the serial monitor (Tools > Serial Monitor) on 115200 baud.

Serial monitor: Touch #1

If I touch the wire, the number displayed is less than 20. So, I guess the threshold is 20. Yay! We’re done with finding the threshold value.

Finding the threshold

Touch Sensor #2: 1 LED

We will use:

  • ESP32 Board
  • Arduino IDE
  • Breadboard
  • Jumper wire: male to male
  • USB Cable
  • 1 LED
  • 330 Ohm resistor

I tried to switch on the LED by touching the wire. If the wire is touched, the LED will be on.

Schematic for 2nd trial (source: randomnerdtutorials.com)

For this trial I only followed the tutorial provided by randomnerdtutorials.com to test if it really worked.

Real pic:

The sketch:

Code for 2nd trial

First, I assigned the touchPin on GPIO 4 and ledPin on GPIO16, also the threshold value = 20. On setup(), I initialized the LED pin as an output so it can be switched on. On loop(), I assigned the touchRead value to a variable, touchValue. The value will be printed on the Serial monitor. If the touch value is less than threshold value, the LED will be on and vice versa.

Let’s try!

Voila! It worked! Let’s explore more by adding the LED and touch sensor.

Touch Sensor #3: 2 LEDs

We will use:

  • ESP32 Board
  • Arduino IDE
  • Breadboard
  • Jumper wire: male to male
  • USB Cable
  • 2 LED
  • 330 Ohm resistor (2)

If the 1st wire is touched, the 1st LED will be on, and if the 2nd wire is touched, the 2nd LED will also be on.

Real pic:

The sketch:

Code for the 3rd trial

It is the same with the trial before with 1 more LED and touchPin. So I added more variables: touchPin2 and ledPin2, also the touchValue2. The output was also added on the setup(). On loop(), the value of the touch was read twice (touchValue and touchValue2) through 2 touchPins. If the 1st touchValue is less than the threshold value, the 1st LED will be on. The same with the 2nd LED.

It worked!

Hall-Effect Sensor

The built-in hall effect sensor is located behind the metal lid of the ESP32. This sensor can detect the magnetic field. The greater the field, the greater the value shown on the serial monitor.

We will use:

  • ESP32 Board
  • Arduino IDE
  • Breadboard
  • USB Cable
  • Magnet (I used the magnet on my bag)
Code from randomnerdtutorials.com

I assigned the variable val = 0. The sensor value will be read and assigned to the variable val and printed on the serial monitor. Open the serial monitor (Tools > Serial monitor) on 9600 baud.

Serial monitor

If I put the magnet close to the sensor, the value will be around 54~68 as shown on the picture. Before, it was around 17~26.

Hall-Effect Sensor #2: LED

I tried to switch on the LED when the val was greater than 40, but it didn’t work even though the val on serial monitor was >40.

The sketch:

I guess something is wrong with my sketch :/. I followed the schema from the touch project before but the LED didn’t work :(. Let’s move to the next project.

Temperature Sensor

Some ESP32 have built-in temperature sensor to monitor its core temperature.

We will use:

  • ESP32 Board
  • Arduino IDE
  • Breadboard
  • USB Cable

Connect the board to the laptop with USB Cable and upload this sketch.

Temperature sensor sketch

To see the value, open the serial monitor on 115200 baud.

Serial monitor for temperature

The monitor showed 53.33 C because the function just returns 128 (sensor not present).

Lastly,

it was difficult to switch on the LED by using touch and hall-effect sensor. It was on when I didn’t touch the sensor, and it didn’t work even though the value on the serial monitor was right. I think the sensor was sensitive and there was noise that disturbed the sensor.

I learned something through this project by following tutorials and exploring more about the projects. See you on my next projects!

Learner || Product and UX enthusiast