"Built-in Touch, Hall-Effect, and Temperature Sensor" on ESP32 using Arduino

 Bonjour...!

Today's article will show how to use 3 built-in sensors in ESP32. Those 3 sensors are touch sensor, hall effect sensor, and temperature sensor. In case you don't know, the temperature sensor in ESP32 only detect the internal temperature of itself and not its external. Without further ado, let's get straight into it!

Required Hardware and Software

  1. ESP32 Development Board
  2. PC with Arduino IDE
  3. Micro USB Cable
  4. Mini LED Lamp
    For output indicator
  5. Resistor
    330 Ohm resistance
  6. Male-to-male Jumper Cable
  7. Magnet
    For testing hall-effect sensor
  8. Breadboard (Optional)

Touch Sensor

STEP 1: Assembling the Circuit

Since not all pin in ESP32 can read touch input, make sure to use pin that accept touch input. For guideline, here is ESP32 DEVKIT V1 with 30 pins.
Source: randomnerdtutorials.com

The circuit is not complex, I just connect GPIO4 to a jumper cable as a touch input. Since there are no output indicator, I will be using serial monitor.

STEP 2: The Source Code

For the source code, go to Arduino IDE and open File > Examples > ESP32 > Touch > TouchRead. Then it will open this code.

Make sure the parameter in touchRead() is the pin number (i use pin 4). After that, verify the code to make sure there are no error.

STEP 3: Uploading the Code

After the code has been verified, connect ESP32 board into PC and upload the code. After the code successfully uploaded, open Serial Monitor from Tools > Serial Monitor. The result will shown as below.



The result on the right is in serial monitor. When the tip of the cable isn't being touched, the value is high. But when the tip is being touched, the value will drop (in my case, it drop below 20). The threshold is not always the same, so observe the result to obtain the threshold.

Hall-Effect Sensor

For the next one, I only need magnet for the input trigger and serial monitor to track the result value.

STEP 1: Circuit?

It actually doesn't need anything. Since it just need to connect to the PC and get the source code.

STEP 2: For the Source Code

Since it also has an example, go to File > Examples > ESP32 > HallSensor. The code will open an example file as shown below.

Make sure to verify the code before uploading.

STEP 3: Upload and Testing

Upload the code after it verified. Then you can test the sensor by holding a magnet on top of ESP32 metal part. Make sure to open serial monitor to see the result.

As the result shown in right picture, the normal values are between 140~160. When the negative pole of magnet is close to the sensor, the value will drop (shown as -56, -59). But when the positive pole of magnet is the one close to the sensor, the value will rise (into >300 in my case).

Temperature Sensor

As I state earlier in the article, the ESP32 internal temperature sensor will only read internal temperature of ESP32 and not external temperature.

STEP 1: Circuit and Coding

I don't need any circuit since it will be detect internal only. And for the code I will use is the code I shown below.
After the code has been written, verify before uploading it to ESP32.

STEP 2: Uploading and Viewing the Result

Upload the code into ESP32 then open serial monitor in PC. Make sure the serial number is correct. Then, in the serial monitor will show the temperature of your ESP32.
The temperature of ESP32 usually doesn't change very often, even when I use 5 seconds delay in my code, the temperature still the same.

The More Advanced Project using Touch and Hall-Effect Sensor

In this section of more advanced project, I will be using two kind of sensor as input. Those are 2 touch input and hall-effect sensor input. I also will be using LED as output indicator.

Step 1: The Circuit

For the circuit is not overly complex. For each LED, the circuit would be more or less like diagram below.


The left input will be coming from ESP32 pins (I will be using pin 22 & 23) and the ground will go into GND pin. For the touch pins, I will be using pin 15 and 4. 


The black cable is going to GND pin, the white cable will connect the output pin to the LED, and the red and blue cable are the touch input.

STEP 2: The Source Code

The source code I will be using is shown below.




In the loop() section, I can divide into 4 part. The first part is checking the hall-effect sensor. If it detect negative pole, it turn ON the first LED. Otherwise, if it detect positive pole, it turn ON the second LED.

The second part is checking the first touch input. If there is a new input, the LED will turn on with a cycle. The cycle is none LED are ON > only 1st LED turn ON > both LED turn ON > only 2nd LED turn ON > both LED turn OFF. It will repeat the cycle continuously.

The third part is checking input on second touch input. If there is an input, it will trigger the blinking loop on fourth part. So basically, the fourth part is making both LED take turn blinking with 250 ms delay until there is another input.

After the code is done, verify it.

STEP 3: Uploading and Testing the Circuit

Finally, after the code has been verified. Upload the code into ESP32 board. After the uploading is finished, check if every input is correct. My result is shown in video below.


Comments

Popular posts from this blog

Programming "Blinking Internal LED" on ESP32 using Arduino IDE

Multiple I2C Devices using ESP32