Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Exploring the GPIOs in ESP32: Usage and Configuration

April 21, 2024

Exploring the GPIOs in ESP32: Usage and Configuration

The ESP32 is a powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities, making it a favorite among IoT enthusiasts. One of the key features of the ESP32 is its General Purpose Input/Output (GPIO) pins, which allow the microcontroller to interact with the physical world. In this blog post, we will explore the usage and configuration of GPIOs in the ESP32, providing extensive examples and explanations.

Understanding GPIOs

GPIO pins are digital pins on a microcontroller that can be programmed to either receive input or provide output signals. The ESP32 microcontroller has a large number of GPIO pins, each identified by a unique number. In the case of the ESP32, there are a total of 36 GPIO pins.

GPIO pins can be classified into several groups:

  1. Digital Input: These pins can read a digital signal (0 or 1) from an external device.
  2. Digital Output: These pins can send a digital signal (0 or 1) to an external device.
  3. Analog Input: These pins can read analog voltage levels between 0 and 3.3V.
  4. Analog Output: These pins can output analog voltage levels between 0 and 3.3V.

Using GPIOs

To use a GPIO in our ESP32 project, we first need to configure it based on our requirements. This involves setting the pin mode and direction. The two most common modes for a GPIO pin are INPUT and OUTPUT.

When configuring a pin as an INPUT, we can read the state of the pin using the digitalRead() function. For example, the following code snippet reads the state of GPIO 4 and prints the result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const int inputPin = 4;

void setup() {
  pinMode(inputPin, INPUT);
  Serial.begin(115200);
}

void loop() {
  int state = digitalRead(inputPin);
  Serial.print("State: ");
  Serial.println(state);
  delay(1000);
}

On the other hand, when configuring a pin as an OUTPUT, we can set its state using the digitalWrite() function. For instance, the following code snippet blinks an LED connected to GPIO 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
const int outputPin = 2;

void setup() {
  pinMode(outputPin, OUTPUT);
}

void loop() {
  digitalWrite(outputPin, HIGH);
  delay(1000);
  digitalWrite(outputPin, LOW);
  delay(1000);
}

It’s worth noting that the ESP32 supports Pulse-Width Modulation (PWM) on some GPIO pins. PWM enables us to control the intensity of an output signal. By varying the duty cycle of the signal, we can simulate analog output. Here’s an example that demonstrates using PWM to fade an LED connected to GPIO 5:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
const int pwmPin = 5;
const int pwmFrequency = 5000;
const int pwmChannel = 0;
const int resolution = 8;

void setup() {
  ledcSetup(pwmChannel, pwmFrequency, resolution);
  ledcAttachPin(pwmPin, pwmChannel);
}

void loop() {
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
    ledcWrite(pwmChannel, dutyCycle);
    delay(10);
  }
  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
    ledcWrite(pwmChannel, dutyCycle);
    delay(10);
  }
}

GPIO Configuration

The general configuration of a GPIO pin includes settings such as pinMode, interrupt configuration, and pull-up/down resistors.

The pinMode() function determines whether a pin is set as INPUT or OUTPUT. Additionally, it provides options for INPUT_PULLUP and INPUT_PULLDOWN, enabling us to enable internal pull-up or pull-down resistors. Here’s an example that sets GPIO 18 as an INPUT with an internal pull-up resistor:

1
2
3
4
5
const int inputPin = 18;

void setup() {
  pinMode(inputPin, INPUT_PULLUP);
}

To configure an interrupt on a specific GPIO pin, we can use the attachInterrupt() function. This function takes as arguments the pin number and an interrupt handler function. Here’s an example of setting up an interrupt that triggers when a rising edge is detected on GPIO 19:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
const int interruptPin = 19;

void IRAM_ATTR handleInterrupt() {
  // Interrupt logic here
}

void setup() {
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, RISING);
}

Conclusion

Exploring and utilizing the GPIOs in the ESP32 microcontroller opens up a world of possibilities. From reading sensors to controlling external devices, GPIOs are essential for interacting with the physical world. In this blog post, we have covered the basics of GPIO usage and configuration in the ESP32, providing extensive examples and explanations. Hopefully, this will serve as a useful starting point for your ESP32 projects. Happy exploring!


➡️ Linux 'mkdir' command


⬅️ Exampe event handling library in C/CMake


Go back to Posts.