Lab 2 - Exploring the ESP32 GPIOs


 Unit 1 - ESP32 Digital Inputs and Outputs 

In this section, I am going to show you how to read digitals inputs like a button switch, and how to control a digital output, like an LED. If you've programmed the Arduino or ESP8266 before with Arduino IDE, this is not new for you. 

Let's make a simple example to see how these functions work with the ESP32 using the Arduino IDE. In this example, you'll read the state of a pushbutton, and light up and LED accordingly. 


Schematic

List of parts needed to assemble the circuit: 

  • ESP32 DOIT DEVKIT V1 Board
  • 5mm LED
  • 330 Ohm resistor
  • Pushbutton
  • 10k Ohm resistor
  • Breadboard 
  • Jumper Wires



Code 

 // set pin numbers  
 const int buttonPin = 4; // the number of the pushbutton pin  
 const int ledPin = 16; // the number of the LED pin  

 // variable for storing the pushbutton status   
 int buttonState = 0;  

 void setup() {  
 Serial.begin(115200);   
 // initialize the pushbutton pin as an input  
 pinMode(buttonPin, INPUT);  
 // initialize the LED pin as an output  
 pinMode(ledPin, OUTPUT);  
 }  

 void loop() {  
 // read the state of the pushbutton value  
 buttonState = digitalRead(buttonPin);  
 Serial.println(buttonState);  
 // check if the pushbutton is pressed.  
 // if it is, the buttonState is HIGH  
 if (buttonState == HIGH) {  
  // turn LED on  
  digitalWrite(ledPin, HIGH);  
 } else {  
  // turn LED off  
  digitalWrite(ledPin, LOW);  
 }  
 }  


Output - Unit 1 




 Unit 2 - ESP32 Touch Sensor 

Introduction

The ESP32 has 10 capacitive touch GPIOs. These GPIOs can sense variations in anything htat holds an electical charge, like the human skin. So they can detect variations induced when touching the GPIOs with a finger. 

These pins can be easily integrated into capacitive pads, and replace mechanical buttons. Take a look at your board pinout to locate the 10 different touch sensors - touch sensitive pins are higlighted in pink color. 



Code

 // ESP32 Touch Test  
 // Just test touch pin - Touch0 is T0 which is on GPIO 4.  
   
 void setup()  
 {  
 Serial.begin(115200);  
 delay(1000); // give me time to bring up serial monitor  
 Serial.println("ESP32 Touch Test");  
 }  
   
 void loop()  
 {  
 Serial.println(touchRead(4)); // get value of Touch 0 pin = GPIO 4  
 delay(1000);  
 }  
   


Result - Unit 2 (Part 1)



Code - Reading the Touch Sensor 

 // set pin numbers  
 const int touchPin = 4;  
 const int ledPin = 16;  
 // change with your threshold value  
 const int threshold = 20;  
 // variable for storing the touch pin value   
 int touchValue;  
   
 void setup(){  
 Serial.begin(115200);  
 delay(1000); // give me time to bring up serial monitor  
 // initialize the LED pin as an output:  
 pinMode (ledPin, OUTPUT);  
 }  
   
 void loop(){  
 // read the state of the pushbutton value:  
 touchValue = touchRead(touchPin);  
 Serial.print(touchValue);  
 // check if the touchValue is below the threshold  
 // if it is, set ledPin to HIGH  
 if(touchValue < threshold){  
  // turn LED on  
  digitalWrite(ledPin, HIGH);  
  Serial.println(" - LED on");  
 }  
 else{  
  // turn LED off  
  digitalWrite(ledPin, LOW);  
  Serial.println(" - LED off");  
 }  
 delay(500);  
 }  
   


Result - Unit 2 (Part 2)





 Unit 3 - ESP32 Pulse-Width Modulation (PWM) 

Introduction

In this section, we will learn how to dim an LED using the LED PWM controller of the ESP32 with the Arduino IDE. 

The ESP32 LED PWM Controller has 16 independent channels that can be configured to generate PWM signals with different properties. 


Schematic

List of parts needed to assemble the circuit: 

  • ESP32 DOIT DEVKIT V1 Board
  • 5mm LED
  • 330 Ohm resistor
  • Breadboard 
  • Jumper Wires.



Code 

 // the number of the LED pin  
 const int ledPin = 16; // 16 corresponds to GPIO16  
   
 // setting PWM properties  
 const int freq = 5000;  
 const int ledChannel = 0;  
 const int resolution = 8;  
   
 void setup(){  
 // configure LED PWM functionalitites  
 ledcSetup(ledChannel, freq, resolution);  
 // attach the channel to the GPIO to be controlled  
 ledcAttachPin(ledPin, ledChannel);  
 }  
   
 void loop(){  
 // increase the LED brightness  
 for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){   
  // changing the LED brightness with PWM  
  ledcWrite(ledChannel, dutyCycle);  
  delay(15);  
 }  
   
 // decrease the LED brightness  
 for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){  
  // changing the LED brightness with PWM  
  ledcWrite(ledChannel, dutyCycle);   
  delay(15);  
 }  
 }  
   


Result




Getting the Same Signal on Different GPIOs

Introduction

You can get the same signal from the same channel in different GPIOs. To achieve that, you just need to attach those GPIOs to the same channel on the setup(). 

Let's modify the previous example to dim 3 LEDs using the same PWM signal form the same channel.



Schematic

List of parts needed to assemble the circuit: 

  • ESP32 DOIT DEVKIT V1 Board
  • 3x 5mm LED
  • 3x 330 Ohm resistor
  • Breadboard 
  • Jumper Wires.

Code

 // the number of the LED pin  
 const int ledPin = 16; // 16 corresponds to GPIO16  
 const int ledPin2 = 17; // 17 corresponds to GPIO17  
 const int ledPin3 = 5; // 5 corresponds to GPIO5  
   
 // setting PWM properties  
 const int freq = 5000;  
 const int ledChannel = 0;  
 const int resolution = 8;  
   
 void setup(){  
 // configure LED PWM functionalitites  
 ledcSetup(ledChannel, freq, resolution);  
   
 // attach the channel to the GPIO to be controlled  
 ledcAttachPin(ledPin, ledChannel);  
 ledcAttachPin(ledPin2, ledChannel);  
 ledcAttachPin(ledPin3, ledChannel);  
 }  
   
 void loop(){  
 // increase the LED brightness  
 for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){   
  // changing the LED brightness with PWM  
  ledcWrite(ledChannel, dutyCycle);  
  delay(15);  
 }  
   
 // decrease the LED brightness  
 for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){  
  // changing the LED brightness with PWM  
  ledcWrite(ledChannel, dutyCycle);   
  delay(15);  
 }  
 }  
   


Output






 Unit 4 - ESP32 Reading Analog Inputs 

Introduction

aIn this section, we'll learn how to read an analog input with the ESP32. This is useful to read values from variable resistors like potentiometers, or analog sensors. 

To see how this works, let's make a simple example to read an analog value from a potentiometer. 


Schematic

List of parts needed to assemble the circuit: 

  • ESP32 DOIT DEVKIT V1 Board
  • Potentiometer
  • Breadboard 
  • Jumper Wires.



Code

 // Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)   
 const int potPin = 34;  
   
 // variable for storing the potentiometer value  
 int potValue = 0;  
   
 void setup() {  
 Serial.begin(115200);  
 delay(1000);  
 }  
   
 void loop() {  
 // Reading potentiometer value  
 potValue = analogRead(potPin);  
 Serial.println(potValue);  
 delay(500);  
 }  
   


Output 




 Unit 5 - ESP32 Hall Effect Sensor 

Introduction 

The ESP32 features a built-in hall effect sensor which is located as shown in the figure below (Behind the metal cap): 


A hall effect sensor can detect variations in the magnetic field in its surroundings. The greater the magnetic field, the greater the output voltage. 

A hall effect sensor can be combined with a threshold detection to act as a switch. Hall effect sensors are mainly used to: 

  • Detect proximity.
  • Calculate positioning.
  • Count the number of revolutions of a wheel.
  • Detect a door closing.
  • And many more. 

Code 

 // Simple sketch to access the internal hall effect detector on the esp32.  
 // values can be quite low.   
 // Brian Degger / @sctv   
   
 int val = 0;  
   
 void setup() {  
 Serial.begin(9600);  
 }  
   
 // put your main code here, to run repeatedly  
 void loop() {  
 // read hall effect sensor value  
 val = hallRead();  
 // print the results to the serial monitor  
 Serial.println(val);  
 delay(1000);  
 }  
   


Output 

Once the upload is finished, open the Serial Monitor at a baud rate of 9600. Approximate a magnet to the ESP32 hall sensor and see the values increasing. 


Or decreasing depending on the magnet pole that is facing the sensor: 


The closer the magnet is to the sensor, the greater the absolute values are.