#1 — The ESP32 Touch Sensor Pins and Hall Effect Sensor

Here comes the next challenge.

Carissa Aurelia
I learn ESP32 (and you should too).
6 min readFeb 2, 2020

--

Challenge. Photo by Igor Rodrigues on Unsplash

Well, it looks tough ‘right? But trust me though, it’s not that big of a challenge *wiggle eyebrows*

After setting up our ESP32 board and make the built-in LED blink (read here), I will show you my next project towards mastering(yep, you heard it right) the ESP32: toying with the touch sensor pins and hall effect sensor.

Touch sensor pins

Posting the ESP32’s 38 pins default pinout for your convenient :) Source: http://www.andreadrian.de/ESP_Schachzwerg/

As its name suggests, the touch sensor pins can sense variations in anything that holds an electrical charge. The easiest example: your fingers. My ESP32 board (the one with 38 pins) has 10 capacitive touch GPIOs, namely TOUCH0 to TOUCH9. You can locate the touch sensor pins (they’re highlighted in deep violet colour) by looking the board’s pinout above.

Hall effect sensor

The hall effect sensor is located behind the metal lid of the ESP32 chip. It can detect variations in the magnetic field in its surroundings. Aside from that, it can also detect proximity, calculate positioning, count the number of revolutions of a wheel, detect a door closing, and much more.

The easiest way to start toying with these sensors is, of course, by testing them. There’ll be 3 projects that I’ll feature in this article: testing the touch pin, testing the hall effect sensor, and turning on a 5mm LED by touch. The reference tutorials are taken from our wonderful trusty website: the randomnerdtutorials.com (touch sensor pin tutorial and hall effect sensor tutorial).

1. The ingredients

The ingredients. I forgot to take the picture of the magnet lol. Source: private documentation

I will be using my laptop (which has the Arduino IDE and the ESP32 add on installed. Read here for the detailed instructions), an ESP32 board, an 830-point solderless breadboard (or it can be any point breadboard, honestly), jumper wires, a 5mm LED, a 330-ohm resistor, a MicroUSB to USB cable, and a magnet.

2. Project 1: testing the touch sensor pins

Code for touch sensor testing. Source: private documentation

The ESP32 add-on in Arduino IDE has provided me with a clean code on how to test the touch sensor pins. Heading to File > Examples > ESP32 > Touch > TouchRead will open that code. The detailed code is written below:

Let’s dissect this code:

  • Serial.begin(115200) sets up the data rate in bits per second (baud) for serial data transmission. Different sensors have different baud rates. The touch sensor pins outputs are able to be displayed on the computer at 115200 baud.
  • delay(1000) gives a second delay to bring up the serial monitor.
  • Serial.println(“ESP32 Touch Test”) prints the phrase “ESP32 Touch Test” on the monitor.
  • Serial.println(touchRead(T0)) uses the touchRead() function, and pass as an argument the pin we want to read. In this case, the example uses T0, which is the touch sensor 0 or TOUCH0. In the board’s pinout, we can see that it is located in GPIO 4.
  • delay(1000) gives a second delay to before displaying the next output.

After I upload the code to the board, I stick jumper wire to the GPIO 4 pin. Then I open the serial plotter to better visualise the output of the touchRead() function. The result is shown below:

A touched jumper wire sticking in the GPIO 4 pin via the breadboard. Source: private documentation
The result as shown on the serial plotter. Source: private documentation

When untouched, the monitor will display the value output at about 70-ish. The value drops to around 15-ish when I touch the jumper wire. Based on what’s pictured in the plotter, the value 40 will be my threshold value to turn on the LED. Basically, the LED will turn on when the value is under 40 (touched) and will turn off when it’s over 40 (untouched).

2. Project 2: testing the hall effect sensor

The add-on also provided me with a code to test the hall effect sensor so I’m just gonna use that code. It can be opened by clicking File > Examples > ESP32 > HallSensor.

Code for hall effect sensor testing. Source: private documentation

I notice that in the loop part, the code doesn’t have the delay(1000) after the Serial.println(val) line. It’ll make the reading harder because there is no delay between the printed output. To fix it, I’m just gonna go ahead and add delay(1000) under the Serial.println(val) line. The code is shown below:

Looking at the code, I can see that:

  • val is a variable for storing the output value of the hallRead() function.
  • I’ll be using 9600 baud rate to read the hall sensor output.

I upload the code to my board and observe the result.

Magnet held close near the hall effect sensor. Source: private documentation
The result as shown on the serial plotter. Source: private documentation

Depending on which pole is close to the sensor, the reading will increase or decrease in value.

3. Project 3: making a touch switch

With the threshold value of 40, I will modify a code that I get from the random nerd tutorials’ website.

A few insight for the code:

  • touchPin is a variable with value 4 which corresponds to the GPIO 4 where I’ll put the jumper wire for the touch switch and ledPin is a variable with value 16 which corresponds to GPIO 16 where I’ll connect the 5mm LED.
  • touchValue is also a variable for storing the output value of the touchRead() function.
  • pinMode(ledPin, OUTPUT) will initialise ledPin as the output of the code.
  • The if conditional in the loop part will turn on the LED when the value is below the threshold, and turn the LED off if it’s above the threshold.

Now it’s time to make a simple circuit using the jumper wires, the ESP32 board, the breadboard, the resistor, and the LED.

The circuit for touch switch. Source: https://randomnerdtutorials.com/esp32-touch-pins-arduino-ide/

Here is where I modify the circuit. I have my board stuck on the breadboard, so instead of having jumper wires on the ground bus of the breadboard (the one with black colour in the picture, coming in and out of the holes marked with blue line), I directly stick a jumper wire on the ground pin of the ESP32 board via the breadboard.

It’s a wrap!!! Source: private documentation

And there you go!

4. Lessons learnt

I think it’ll be fair to tell you a few problems I encountered along the way:

  • I have no idea what type of jumper wires to use. There are 3 types of jumper wires: male-male, male-female, and female-female. I bought male-female jumper wires only and turns out what I need are male-male jumper wires. I had to use three resistors to overcome this shortcoming, as you see in the picture. And surprisingly, the circuit worked! It was very improper though 😅😅. I’ll make sure to buy the male-male jumper wires for the next projects.
  • The part where I have to press the boot button before uploading and release it after “Connecting…” appears on the screen makes uploading the code a really, really, really tricky task. Sometimes I get timeout errors even though I’ve pressed the boot button. The key to overcome this is to try again with different computer USB ports, restarts the IDE, or try to upload a different code. It could — nay, would drain your patience but it’s just the way it is with my board. If you encounter the same problem, just be patient :)

--

--