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?
A touch sensor, as you can tell, is a sensor to “feel” your touch. It’s something like your smartphone’s screen. Your smartphone’s screen converts your finger touches to the screen into electrical signal for the smartphone to process. This particular touch sensor on the ESP32 measures your ‘touches’ by numbers. when there is no touches, the value will remain on somewhat ≥ 70, but when you touch the sensor, the value will change to about ≤ 10, depends on the finger contact with the sensor. In ESP32, some of the GPIO are touch capable
Project 1: Testing Touch Sensor
In this project, all we need is:
- ESP32 board (duh, obviously)
- Micro-USB cable (duh)
- A 330Ω Resistor
- One LED bulb
- Two of male-to-male jumper cables
If you are curious, this is the schematic I used in the project
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.
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:
What went wrong the first time I tried it?
Every time I tried to make a project, it always fail, big time. This time, I made a mistake by putting the cables on the wrong GPIOs so the LED wont light up. But when I put it on the right GPIO, the LED still wont light up. It only lights up when I touched the resistor. I still don’t know why it did that, but suddenly when I push the LED pin deeper to the breadboard, it lights up. Still, I don’t understand what’s the problem.
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
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
I recorded the value when I play with it
Here’s what I analyzed from the trial:
- When in idle, the value is above 0 but less than 10
- 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.
- If I put the north pole of the magnet (still, I might be wrong), it will detect a positive value
- 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.
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
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!