Language: EN

esp32-gpio

What are and how to use GPIO in the ESP32

GPIO pins (General Purpose Input/Output) 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 like LEDs, motors, and other components.

Using digital inputs and outputs on the ESP32

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

To configure a pin as an input or output, the function is used

pinMode(pin, mode)

Where

  • pin is the number of the pin you want to configure
  • mode can be
    • INPUT to configure it as an input
    • OUTPUT to configure it as an output.

GPIO pin modes

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

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

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

  • 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

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 establish a predefined logical state when there is no external signal.

Code examples

Configure a pin as an 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 on 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 an 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 on 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 an output
  Serial.begin(9600);
}

void loop() {
  digitalWrite(ledPin, ledState);  // Set the state of the LED
  Serial.println(ledState);         // Print the state to the serial port
  delay(1000);                      // Wait one 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);
}