esp32-gpio

What are and how to use GPIO in the ESP32

  • 3 min

GPIO pins (General Purpose Input/Output) are digital ports that can be configured as either inputs or 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 straightforward. Basically, it is exactly the same as it would be for a “normal” Arduino.

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

pinMode(pin, mode)
Copied!

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 logic state when there is no external signal.

Code Examples