Language: EN

esp32-gpio

What are and how to use GPIO in the ESP32

The GPIO (General Purpose Input/Output) pins are digital ports that can be configured as both inputs and outputs on a microcontroller.

Inputs are used to read signals from the outside, such as sensors, switches, or any device that generates an electrical signal.

Outputs, on the other hand, allow the microcontroller to control devices such as LEDs, motors, and other components.

Using digital inputs and outputs in the ESP32

In the Arduino environment for ESP32, handling GPIO pins is quite simple. Basically, it is exactly the same as we would have in the case of a “normal” Arduino.

To configure a pin as input or output, the pinMode(pin, mode) function is used where pin is the pin number to be configured and mode can be INPUT to configure it as an input or OUTPUT to configure it as an output.

Modes of GPIO pins

In addition to the INPUT and OUTPUT modes, GPIO pins on the ESP32 can also be configured in other modes that allow more advanced functionalities. Some of these modes include:

  • INPUT_PULLUP: Configures the pin as an input with internal pull-up resistor activated. This is useful for detecting changes in switches or buttons.

  • INPUT_PULLDOWN: Similar to the previous one, but with internal pull-down resistor activated.

  • OUTPUT_OD: Configures the pin as an open-drain output, which is useful when working with external circuits.

Internal pull-up and pull-down resistors

The GPIO pins on the ESP32 have internal pull-up and pull-down resistors that can be activated as needed.

These resistors are useful when working with inputs, such as switches or buttons, as they help to establish a predefined logical state when there is no external signal.

Code examples

Configure a pin as input and read its value

const int sensorPin = 13; // Example pin for sensor
int sensorValue;

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  sensorValue = digitalRead(sensorPin);
  Serial.println(sensorValue);
  delay(1000);
}

Using a digital input in the ESP32

const int buttonPin = 12;  // Pin for the button
int buttonState;            // Variable to store the button state

void setup() {
  pinMode(buttonPin, INPUT);  // Configure the pin as input
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);  // Read the button state
  Serial.println(buttonState);            // Print the state to the serial port
  delay(100);                             // Small pause to avoid erratic readings
}

Using a digital output in the ESP32

const int ledPin = 13;  // Pin for the LED
int ledState = LOW;     // Initial state of the LED

void setup() {
  pinMode(ledPin, OUTPUT);  // Configure the pin as output
  Serial.begin(9600);
}

void loop() {
  digitalWrite(ledPin, ledState);  // Set the LED state
  Serial.println(ledState);         // Print the state to the serial port
  delay(1000);                      // Wait for a second
  ledState = !ledState;            // Change the state of the LED
}

Configure a button with internal pull-up resistor

const int buttonPin = 12;
int buttonState;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    Serial.println("Button pressed");
  }
  delay(100);
}