Internal Sensors on ESP32, Touch and Hall Sensors with PWM Implementation?!?!

An II2260 Project!

Hello again! As you know, my name is Tugus and I’m an Information and System Technology student learning Embedded Systems. I decided to change the blog language from Indonesian to English since I like to write in English and also to learn English better.

On this fourth week of the semester, I decided to learn about Internal Sensors on ESP32, especially Touch and Hall Sensors. Actually, ESP32 have another built-in sensor — temperature sensor — that is pretty useful for some occasions. But in this project, I’d like to try and make a project from Touch and Hall sensor.

What is a Touch Sensor?

GPIOs capable of touch input have ‘TOUCH’ property

Project 1: Testing Touch Sensor

  1. ESP32 board (duh, obviously)
  2. Micro-USB cable (duh)
  3. A 330Ω Resistor
  4. One LED bulb
  5. Two of male-to-male jumper cables

Schematics

I use the same schematics as the one from randomnerdtutorials, except for the LED I use GPIO 19

GPIO 4 acts as a touch sensor. the jumper cable connected through the GPIO 4 pin will act like a switch for our LED. When we touch the cable end, we will see the LED lights up, and if we let go of it, the LED will turn off.

Code to make this work?

I use this code for the touch sensor project. As you can see, first we initialize our pin numbers for the LED and the touch sensor. For this project I use GPIO 19 for the LED and GPIO 4 for the touch sensor. Next, we set the threshold value to 20. This threshold value is important because we will compare the touch value from the touch sensor to the threshold. After that we do the setup function where we set the serial to 115200 baud, and setting the LED pin as an output parameter.

On the code, first we need to read the touch input using touchValue = touchRead(touchPin). This value then will be printed every time we run the loop function, and the value will change when we touch the sensor.

Touch value of a touch sensor

On the loop function, this gets interesting. The main logic is, if the touch value is lower than the threshold (when we touch the cable), the LED will light up. If not, the LED will stay turned off. But in this case, I want to be creative so I set the LED to be blinking slowly when we don’t touch the cable, and it will blink rapidly when we touch it. On top of that, we always print the touch value and the condition of the LED. Here’s the clip:

LED blinking rapidly when touched

What went wrong the first time I tried it?

Now, moving on to the next project: Hall Effect Project.

This project is quite interesting because I never knew there is such a thing called “Hall Sensor”, and it’s built in inside the metal plate of ESP32??? Very cool!

The idea of the Hall sensor is to detect changes on magnetic field. On randomnerdtutorials, they said:

A hall effect sensor can detect variations in the magnetic field in its surroundings. The greater the magnetic field, the greater the sensor’s output voltage.

So in my curiosity, I tried to test it. Is it true?

I tried it using this code

A code I got from randomnerdtutorials

So the idea of the code is to read the hall value by using magnets. We hover the magnet above the metal plate of ESP32 and start playing with it

Source: randomnerdtutorials

I recorded the value when I play with it

Hall value of the project I tried

Here’s what I analyzed from the trial:

  1. When in idle, the value is above 0 but less than 10
  2. If I put the south pole of the magnet (I don’t know but I think its the south pole), it will detect a negative value.
  3. If I put the north pole of the magnet (still, I might be wrong), it will detect a positive value
  4. The closer you get the magnet on the hall sensor, the bigger the hall absolute value.

But I thought “Man, if I only know the value, it’s very boring, I need VISUALS”. So then, I tried some research and I stumble upon PWM or Pulse-width-modulation. It is essentially trying to make an analog output from a digital input. What I mean by analog is, you can make the LED seems like it is increasing and decreasing its “brightness”. Then I thought “Hey, if I make the hall value to be the ‘brightness’ parameter, I can make something out of this!”. And so I did.

Code?

The code for the Hall Sensor PWM I made

So with PWM we can set frequency, ledChannel, and resolution. You can read about PWM here for more explanations. But to be concise, I use the value read by the Hall Sensor as the LED brightness parameter. Because the value could be negative and LED brightness parameter don’t accept negative value, I make the value absolute. By reusing the algorithm used in the touch sensor LED, I set the threshold to 20. If the absolute value is below 20, the LED wont light up. If the absolute value is above 20, it will light up depends on the Hall value. The higher the absolute hall value, the brighter the LED. Here’s the proof

Hall Sensor with PWM LED Effect — Indonesian

For the first time, I was shocked because I didn’t fail the circuit this time. It worked on the first try, and I was very happy. Also, I’m very proud that I created a creative project outside of the randomnerdtutorials module, and because of it I learned a lot more.

Maybe that’s all for this blog (I was surprised it is very long), thank you for reading and see you on the next post!