Get to Know ESP32 #2: Working with Touch and Hall Effect Sensors
It’s starting to get real..
Last week, we have our very first experience with ESP32, which was to make it blink. As I’ve stated before, there are gonna be new projects every week. And yeah, you can guess from the title, this week’s task is about sensors, specifically touch and magnetic sensors.
Touch Sensors
ESP32 has 10 capacitive touch GPIOs that are made to sense electrical charges, including those in our body. In our experiment, we’re only going to use one of the GPIOs.
For your information, this part itself is divided into two sections, where the first talks about checking the touch sensor pins, and the latter about using the touch sensors to light up an LED.
First Section: Checking Touch Sensor Pins
In the first section, you’re gonna need:
- 1 Laptop with Arduino IDE installed and configured (see how here);
- 1 USB-A to Micro-USB cable;
- 1 ESP32-S Development Board (mine consists of 38 pins);
- 1 Male-to-Female Jumper Wire;
To find out whether your ESP32’s touch sensor pins are working or not, you can use the example provided by Arduino IDE’s ESP32 add-on. Go to File>Examples>ESP32>Touch>TouchRead.
A new window with the TouchRead code will appear.
Since we’re going to use Touch0 sensor for this example, we have to know to which GPIO it corresponds to. Based on this pinout, we can see that GPIO4 is the pin corresponding.
Therefore, we make edits to the program. The Serial.println(touchRead(4)) means that it reads the values from GPIO4 capacitive sensor, while delay(1000) means that it takes 1 second delay before the Serial Monitor displays next value (not-so-spoiler: you can change this delay value).
Finishing with the program, now let’s proceed with the ESP32. Take the wire jumper, and put the female end at P4 pin, while the male end is the place where stimulus (a.k.a touch in this project) will be received.
Now, connect your laptop with the ESP32. Make sure to use the correct board and port. Then, open the serial monitor and make sure that the baud rate is at 115200 — matching with the code below, since touch sensor pins outputs are to be transmitted at this rate. Verify the code and upload it to the development board.
Okay, you’re all set! Open the Serial Monitor which you can access in Tools, or simply press Ctrl+Shift+M. You can touch and untouch the male end of the wire jumper, and there must be difference in the values — where if you touch the wire, the value is supposed to be lower. In my case, when touched, the serial monitor displays approximately 50 to 60 values, and when untouched the values go down to around 10.
Below is my video of the serial monitor while touching (and not touching) the wire jumper.
Second Section: Using Touch Sensor for LED
Moving on to the next part, making a touch-sensitive LED. You’re going to need to prepare these additional equipments:
- 2 Male-to-Female Wire Jumpers (3 in total)
- 1 Male-to-Male Wire Jumper
- 1 LED
- 1 330 Ohm resistor
- 1 Breadboard
For this part, we’re going to have to build a circuit.
- Attach a LED to the breadboard. Note: the longer leg is anode, the positive side of LED, and the shorter one is cathode, the negative side.
- Grab one leg of the 330 Ohm resistor, and put it in the same column as the anode.
- Take the male-to-male wire jumper. Place one end in the same column as the cathode of LED, and another end at ground of the breadboard.
- Grab one male-to-female wire jumper. Put the male end in the same column as the other leg of the 330 ohm resistor, while the female end is to be plugged at P16 at ESP32.
- Take another male-to-female wire jumper. At ground of the breadboard, put the male end. Then, plug the female end at GND pin in the breadboard.
- Just like our experiment before, the last male-to-female wire jumper is to connect the ESP32 to the stimulus. Put the female end at P4.
Done! Time to code now. You can find the code which I use at randomnerdtutorials. If you’re not using P4 for touch sensor or P16 for LED, you can change the values on the program. Next, from our experiment before, we can see how the values change when touched and untouched. Set a value — called threshold — where if the sensors receive values under the threshold, the LED will turn on. In my case, it’s always below 20 when touched, so i set the threshold to 20. Oh, you can also set how often you want to read inputs from the sensor, by editing delay value inside loop() part. Mine is set to 500 ms or 0.5 second.
Clearly, when you’re done editing the code, you verify, connect your laptop to ESP32, and upload the code. Again, please make sure you’re currently using the correct board, COM, and matching baud rate with your code (115200 in this project)!
Open the Serial Monitor (yes, again) and proceed to touch and untouch the male end. You can examine the values and how it affects the LED. Fortunately, my project went smoothly. Here’s how my project went.
Magnetic/Hall Effect Sensors
By now, you must’ve already noticed that this little microcontroller is sort of astonishing (I know, it’s a really simple task, but as someone with no electrical thingy background, I’m already amazed). So, without further ado, let me introduce you to another feature by ESP32, magnetic sensor — or hall effect sensor. I’m doing this experiment with the help of, of course, randomnerdtutorials.
These are the equipments you’ll need to prepare:
- 1 Laptop with Arduino IDE installed and configured
- 1 USB-A to Micro-USB cable
- 1 NodeMCU-32S ESP32 (mine with 38 pins)
- Magnet (any magnet, you can use your refrigerator magnet if you wish)
Now, with your Arduino IDE opened, go to Files>Examples>ESP32>Hall Sensor.
A window will appear, and below is how the code should look like.
You see, in the setup part, the Serial.begin value is set to 9600, which means we ought to change the baud rate at Serial Monitor to 9600. Also, in this code, I added delay(1000) at the end of loop() function so the response won’t be too frequent.
As usual, connect your ESP32 development board to your laptop, verify, and upload the code. When you’re finished uploading, open Serial Monitor and observe. When the magnet is close, the values should change. High positive numbers in serial monitor means the side close to sensor is the positive one, while negative numbers mean the opposite. Alas, my magnet is a refrigerator magnet, so I can’t switch sides and therefore I only get to see negative values.
That’s it, fellas.. Have fun, adios!