ESP32 Chapter 2: Blinking LED, Touch Sensor, and Hall Effect

Christovito Hidajat
4 min readFeb 3, 2020

--

Hello and Welcome!

Only by reading the title, you may wonder what happened to “a new friend of mine” a.k.a the one and only ESP32. Well, yes, it now can feel a touch, it senses. Not only touches, but also magnetic force!

Moving on from the previous ESP32 mini project, this time we are to finish these following instructions: make an external LED blink, turn on an LED with the touch sensor, and deal with the “Hall Effect” a.k.a magnet sensor. Before going straight to the projects, please note that my ESP32 board has this following pinout set.

ESP32 Pinout Guide (Source Attached)

Part 1: LED Blink

Unlike the first project — blink the internal LED on the ESP32 board — this time we are to blink an external LED. To do this, we need an LED, a resistor (mine is 10k ohm), a breadboard (or jumper wires), and of course the ESP32 board. Here below are the code and the circuit of the LED blink.

const int ledPin = 23void setup(){
pinMode(ledPin, OUTPUT);
}
void loop(){
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}

Please note that the ledPin is equal to 23 (optional), meaning it refers to ESP32 GPIO 23. The following video is the proof of the red LED blinking every second.

I made a simple improvisation by adding another LED (yellow LED), so the red and yellow LED would blink every second by turns, continuously. These below are the set-ups and the proof.

const int ledPin1 = 23; // red LED
const int ledPin2 = 4; // yellow lED
void setup(){
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop(){
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
delay(1000);
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin2, LOW);
delay(1000);
}

Part 2: Touch Sensor

This one always gets me curious, because it’s said that those touch sensors are used to wake the ESP32 up from deep sleep. Interesting enough? Well let’s get to it.

At first I tested the touch sensor whether it was active or not. By connecting a jumper wire to GPIO4 — the tip of the wire acts as the touch sensor — it will sense the touch after we touch, of course, the tip of the wire.

void setup(){
Serial.begin(115200);
delay(1000);
Serial.println("ESP32 Touch Test");
}
void loop(){
Serial.println(touchRead(4));
delay(1000);
}

By opening the Serial Monitor (go to Tools, Serial Monitor), we can see values being displayed every second. At a baud rate of 115200, the values will decrease whenever the metal part of the wire is touched.

Now, we get to the most exciting part, or even satisfying, turning on — and off — an LED using the touch sensor. You will need the code and the circuit below.

const int touchPin = 4;
const int ledPin = 23;
const int threshold = 20;
int touchValue;
void setup(){
Serial.begin(115200);
delay(1000);
pinMode(ledPin, OUTPUT);
}
void loop(){
touchValue = touchRead(touchPin);
Serial.print(touchValue);
if (touchValue < threshold){
digitalWrite(ledPin, HIGH);
Serial.println(" - LED on");
}
else {
digitalWrite(ledPin, LOW);
Serial.println(" - LED off");
}
delay(1000);
}

I attached the LED to pin 23 and the jumper wire to pin 4. The code signifies that whenever we touch the metal part of the wire, the yellow LED will be on, and the other way around. You can watch this video below as a proof.

Part 3: Hall Effect

Hall Effect sensor or magnet sensor is located behind the metal lid of the ESP32 chip. It can detect magnetic field around it, by producing a series of output voltage.

int val = 0;void setup(){
Serial.begin(9600);
}
void loop(){
val = hallRead();
Serial.println(val);
delay(1000);
}

Open the serial monitor, and place a magnet close to the hall sensor. You can see the values on the serial monitor will decrease, or increase, based on the magnet pole facing the sensor and how close you put the magnet to the sensor.

While doing the third project, at first I was confused because whenever I placed a magnet near the sensor, the serial monitor kept showing random numbers, instead of the values it should be. Then I borrowed a friend’s ESP32 board and it would work just fine, the serial monitor showed relatively correct values for every condition. So I guessed there was something wrong with my ESP32 board.

Overall, it was a great experience, also working with my teammates, Patrick and Isti was fun and worthwhile. I was — and am — trying to take only the positives from doing all this stuff, that you take not only the knowledge, but also the experience on working together as a team, which is super important as well. As it is getting exciting, I am eager to learn further about IT world especially from the upcoming ESP32 projects.

Christovito Hidajat
18218043

--

--

No responses yet