Embedded System Journey #3: Touch Sensor and Hall Effect Sensor on ESP32’s Internal Sensor with Arduino IDE

Vincent Franstyo
9 min readFeb 21, 2023

--

Hello there! Back with me again, Vincent. In this article, we’re gonna talk about internal sensors inside the ESP32. Unlike the projects we’ve done before, this project don’t actually require much. We’re just gonna talk about the internal sensor in the ESP32, which are touch sensor, hall effect sensor, and temperature sensor. However, we’re just gonna talk about the touch sensor and hall effect sensor since I can’t find the temperature sensor in my ESP32.

TOUCH SENSOR

— Introduction🧠

We are gonna try the touch sensor built inside the ESP32. We will be using our hand as the trigger for the touch sensor. We will use LEDs to show the concrete evidence of the touch sensor. Let’s jump into it!!

— Components 🤌🏻

As what we’ve talked before, this project won’t require many components. The essentials will only be:

  • ESP32 Board DOIT DEVKIT V1
  • Breadboard
  • 1 Male-to-male jumper cable as input

However, I thought it is too boring to only watch how the serial plotter goes up and down. Sooooo, Imma just use some LEDs to show you how the touch sensor takes effect. You can refer to this article to analyze the serial plotter using the essential components.

Imma drop the additional components needed in my circuit here:

  • an LED
  • minimum of 3 male-to-male jumper cable
  • 1 300 Ohm resistors

Let’s jump to the steps!

— Steps👣

Here I tried using 1 LED as output and touch sensor and input.

Here is my first scheme. You can use it as reference.

1 LED touch sensor scheme
  1. Attach your ESP32 to the board (of course). Leave the right side of ESP32 (where 3v3 and GND can be spotted) open since we’re gonna use the right side. Push it down ’til you can’t see the legs no more. (fr, or else it won’t work).
  2. Connect the GND pin to anode side (blue line) of the breadboard so that the power can be distributed to other components.
  3. Connect the LED to the breadboard. Note that LED has 2 legs. The long leg indicates the cathode of the LED and the other one is the anode side. If you are a newbie, it is recommended to just follow the scheme shown above.
  4. Connect the resistor from the anode side of the LED to the anode side of the breadboard (at the same line with the pin connected to the ground).
  5. Connect the other leg of the LED to the input pin in the ESP32 (in my case I use the pin 23. I didn’t know why but it just didn’t work in other pin. I dunno if the same thing happens to others)
  6. Lastly, for the input, we’re gonna use the Pin 4 in the ESP32. Connect the jumper cable to the pin and leave the other side disconnected. You will use the other side as input from your touch as shown in the result section.

— Code Overview💻

// Pin initialization
const int touchPin = 4;
const int ledPin1 = 23;
const int threshold = 20; // treshold value
int touchValue; // the sensor's value

void setup(){
Serial.begin(115200);
delay(1000);
pinMode (ledPin1, OUTPUT); // initiation of led as output
// pinMode (ledPin2, OUTPUT);
}

void loop(){
// sensor reading
touchValue = touchRead(touchPin);
Serial.print(touchValue);
// if touchValue < threshold -> touched
// LED is turned on

if(touchValue < threshold){
digitalWrite(ledPin1, HIGH);
Serial.println(" touch");
}
// if touchValue > threshold -> touched
// turn the LED off
else{
digitalWrite(ledPin1, LOW);
Serial.println(" not");
}
Serial.println(touchValue);
delay(500);
}

First, we initiate the usage of the pins here. Pin 4 for touch pin (input), pin 23 for LED (output), threshold is the value we get on full tolerance. I followed the tutorial and it actually worked. I assume that this is still the right threshold value to use. You can also read the right touch value by uploading the code and print the touch value when both touched and untouched and analyze the result. We also have touchValue to store the value when the pin is on touched or untouched condition.

In the void setup function, as usual we setup the serial monitor and set the led pin as output.

In the void loop function, we read the touch value from the touch pin. If the touchValue is lower than the threshold, it indicates that the it is currently touched and the led pin is set to be high. Else, it won’t turn on since it is not touched and the touch value is higher than the threshold.

— Result 🏁

Here’s how it turns out!
Every time the input pin is touched, it will turn the LED on. I mean it’s actually cool rite. The ESP32 of that size is capable of doing such thing!
OMG!OMG!OMG! 🤯🤯🤯

1 LED touch sensor result

— Personal Experiment 🧪

I tried to add another LED since I was curious. Here’s the circuit I made. It’s a little different from the one provided above since I made them at 2 different timeline, but they both are the same functionally. I wanted them to be lighted up alternately.

2 LEDs touch sensor scheme

I also modified the code to match how I want it to be. Here’s the code I used for this experiment.

// Inisiasi nomor pin
const int touchPin = 4;
const int ledPin1 = 18;
const int ledPin2 = 23;
const int threshold = 20;
int touchValue;

void setup(){
Serial.begin(115200);
delay(1000);
pinMode (ledPin1, OUTPUT);
pinMode (ledPin2, OUTPUT);
}

void loop(){
touchValue = touchRead(touchPin);
Serial.print(touchValue);
// if the touch value is lower than the threshold (touched)
// the blue LED will light up while the red LED will be off

if(touchValue < threshold){
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
Serial.println(" touch");
}
// if the touch value is higher than the threshold (untouched)
// the red LED will light up while the blue LED will be off
else{
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
Serial.println(" not");
}
delay(500);
}

And here’s the result!!! 🎉🎉
They both lighted up alternately! Hooray!

2 LEDs touch sensor result

HALL EFFECT SENSOR

— Introduction🧠

Hall effect sensor is detected through magnet (positive and negative side of a magnet) and what’s surprising is that ESP32 is capable of doing so!! In this experiment we will try to light up an LED again with the magnet. Let’s jump into it.

— Components 🤌🏻

Not that much different than the Touch Sensor, this project also won’t require many components. In fact, you can just use the components listed in the touch sensor and just change the input and the code, then you’re done.

Imma just list the components again here so that you won’t need to scroll up again. The essentials are just:

  • ESP32 Board DOIT DEVKIT V1
  • Breadboard
  • A magnet with both north and south pole

And again, I still feel it’s too boring to only watch the graph going up and down. Sooo, I add an LED as output so that it will be more colorful 💥💥. Here are the additional components I added to the scheme aside from the essential components.

  • an LED
  • minimum of 2 male-to-male jumper cable
  • 1 300 Ohm resistor

Let’s jump to the steps! 💃🏻

— Steps👣

1 LED scheme

As I mentioned above, you can just remove the input cable from the touch sensor scheme, change some code and you’re good to go. But Imma leave the steps here so that you won’t need to scroll up again.

  1. Attach the ESP32 to the board as usual and leave the right side (where you can find 3v3 and GND) open. And again, push it down til you can see none of the legs no more.
  2. Connect the GND (ground) pin to the anode side (blue line) of the breadboard with a male-to-male jumper cable.
  3. Connect the LED to the breadboard. Remember that the long leg represents the cathode side of the LED and the short one represents the anode side of the LED.
  4. Connect the resistor to the anode side of the LED’s leg and add a jumper cable to connect the cathode legs of the LED to the input pin (in my case I use the 4th pin) as shown in the picture above
  5. You’re done! 🥳🥳Just put your magnet closer (or even stick it) and watch the result

— Code Overview 💻

Here’s the code I used to light up the LED using magnet.

int val = 0;
const int ledPin1 = 4;

void setup() {
Serial.begin(115200);
pinMode (ledPin1, OUTPUT);
}

// put your main code here, to run repeatedly
void loop() {
// read hall effect sensor value
val = hallRead();
// print the results to the serial monitor
if (val > 50){
// kutub positif
digitalWrite(ledPin1, HIGH);
Serial.println("+");
}

else if (val < -50){
// kutub negatif
digitalWrite(ledPin1, LOW);
Serial.println("-");
}
else{
// netral
digitalWrite(ledPin1, LOW);
Serial.println("netral");
}
Serial.println(val);
delay(1000);
}

Firstly, we have to initialize the value that will be read by the hall effect sensor. I named it val. As usual, we initialize the LED Pin. Here I used 4th Pin.

In void setup() function, as usual, we initialize where we would want the serial monitor to show the result at. Here I used the 115200 baud. We also set the LED pin to be the output.

In void loop() function, we store the value read by the ESP in value using hallread() function. In my case, I have analyzed the value that is read by the ESP32. On netral, the value was around 0–30. When the magnet is close, the value will go up to 50 ++. When the magnet is flipped, the value will go down lower than -50. Therefore, I used 50 as the threshold for the positive limit and -50 for the negative limit. When the value went up over 50, the LED will light up and print “positive” at the serial monitor. However, if it goes down under 50, the LED will shut itself down and print “netral” at the serial monitor. When it goes even lower, the LED will still be off, but “negative” will be printed in the serial monitor.

It originally was a code I used to light up 2 LEDs and I’m confused how I’m supposed to alter the code. Therefore, I didn’t really handle the case when it goes down lower than -50. I’m sorry 😔😔. If you wanna see how it worked out, stay tuned and read ’til the end 🙌🏻🙌🏻.

— Result 🏁

Here’s how the scheme turns out! 🎉🎉I divided it into three section so that you will be able to see the difference. Take a look! 👀👀

Positive Side Result➕:

Positive side result

Negative Side Result ➖:

negative side result

Netral Side Result 🧔🏻‍♂️👩🏻‍🦰:

netral (no magnet attached)

— Personal Experiment 🧪

I actually made this one first since I thought it would show the difference more. However, I thought using 2 LEDs is somewhat over the top. So, I decided to alter the code and the scheme to only use 1 LED 😶😶.

I will still leave my personal experiment with 2 LEDs here as I promised above. You can try to understand the code yourself since it wasn’t very different from the 1 LED version. 🤐🤐

int val = 0;
const int ledPin1 = 4;
const int ledPin2 = 5;

void setup() {
Serial.begin(115200);
pinMode (ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}

// put your main code here, to run repeatedly
void loop() {
// read hall effect sensor value
val = hallRead();
// print the results to the serial monitor
if (val > 50){
// kutub positif
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
Serial.println("+");
}

else if (val < -50){
// kutub negatif
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
Serial.println("-");
}
else{
// netral
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
Serial.println("netral");
}
Serial.println(val);
delay(1000);
}

Here’s the final scheme for the 2 LED version. Take a look !!

2 LED hall effect scheme

Result:

Positive Side Result➕:

positive side 2 LEDs scheme

Negative Side Result ➖:

Negative side 2 LEDs scheme

Netral Side Result 🧔🏻‍♂️👩🏻‍🦰:

Reaction on no magnet

— Closing 👋🏻👋🏻

Thanks for staying tuned and reading this article ’til the end. Don’t be bored with me since Imma still upload these kinds of blog in the near future 🙌🏻🙌🏻

--

--