ESP32. How to use GPIO digital IO with Arduino code

How to use the digital I / O of the ESP32 GPIO with Arduino code. Internal pull-up and pull-down resistors, compatible pins
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp
Table of Contents

The ESP32 has many pins that can be used as a digital input or output. The ESP32 also has pull-up and pull-down resistors which simplify the circuits. Programming is identical to Arduino projects. We will learn on a simple example how to read the state of a digital input and drive an output to light an LED.


Avoid using pins that are reserved for the ESP32 processor or that are used at boot time.

Install the ESP-IDF SDK for ESP32 on IDE Arduino and PlatformIO

If you are new to ESP32 development boards you must first install the ESP-IDF development kit. Here are two tutorials to get started depending on your code editor

Follow the instructions in this tutorial for the Arduino IDE

Read Also
ESP32. Getting started with ESP32-Arduino on IDE Arduino, macOS, Windows, Linux

And this one for PlatformIO (ideally with VSCode)

Read Also
Getting started with PlatformIO IDE on VSCode (Windows, macOS, Raspbian, Linux)

Identify the digital IO pins of the ESP32

The ESP32 has many pins that can be used as a digital input and / or output. Some pins are reserved or are used by the processor for example to access the flash memory via the SPI bus (pins 6 to 11), serial port for uploading the program or debugging.

esp32 digital io available reserved not devkitc v4

The pins framed in green can be used for digital input or output as well.

The pins framed in orange but you have to be careful because they may have unexpected behavior on startup.

It is not recommended – forbidden – to use the pins framed in red . Usually they are reserved (and used) by the ESP32 processor . You may run into serious problems using these pins in your projects.

GPIO pinDigital inputDigital outputNote
0PULL UPSends a PWM signal at startup.
1TXDebug output at startup
2Connected to the on-board LED
3RXTakes the HIGH state at startup
5Sends a PWM signal at startup
6Used for SPI flash memory
7Used for SPI flash memory
8Used for SPI flash memory
9Used for SPI flash memory
10Used for SPI flash memory
11Used for SPI flash memory
12Fail to start if in PULLUP mode
14Sends a PWM signal at startup
15Sends a PWM signal at startup

Data collected in official documentation .

Spindle to be avoided or prohibited

Some GPIO pins automatically go HIGH or output PWM signals on startup or reset. You may get unexpected results in your programs when the ESP32 resets or starts up.

  • GPIO0
  • GPIO1
  • GPIO3
  • GPIO5
  • GPIO6 to GPIO11 connected to SPI flash memory. Use prohibited.
  • GPIO14
  • GPIO15

How to read a digital input from ESP32?

To be able to read on a digital input of the GPIO, you must first configure the pin as an input using the pinMode() function like this

pinMode(pin, mode);

The pinMode method has two arguments

  • The pin number
  • The mode

3 modes are available to configure the spindle as a digital input

  • INPUT Depending on the input device used, the circuit must be adapted. For example, when a Momentary Switch type push button is used, a pull-up or pull-down  resistor must be integrated into the circuit. Without a pull-up or pull-down resistor, the ESP32 input is floating. The logic level is undefined which can lead to erratic operation of the program. For example not detecting the pressure on a button or on the contrary detecting that it is pressed when it is not.
  • INPUT_PULLUP We use the pulling resistance (PULL UP) of the ESP32
  • INPUT_PULLDOWN  We use the pull-down resistor (PULL DOWN) of the ESP32

Each pull-up or pull-down resistor has a value of 45 kΩ according to the DC Characteristics table (3.3V, 25°C) of technical documentation.

To read a digital input – like a button – we use the digitalRead() function, which takes the GPIO pin number as argument.


Use the previous diagram and table to choose the GPIO pins for your project.

How to control a digital output of the ESP32?

To be able to use a GPIO pin as a digital output, you must first configure the pin using the pinMode() function by indicating the number of the pin and passing the OUTPUT argument for the mode.

pinMode(pin, OUTPUT);

The spindle can take 2 states

  • LOW the pin is low, i.e. the voltage is zero (0V).
  • HIGH the pin is high, i.e. the voltage is 3.3V

The digitalWrite() function allows the pin to be set high (HIGH) or low (LOW) for a pin of the GPIO.

digitalWrite(pin, state);

Note on current consumption by GPIO

The ESP32’s GPIO is not designed to send logic signals. The GPIO will be able to deliver enough power to power a few LEDs for demonstration purposes.

On the other hand, the GPIO cannot be used to supply a motor for example.

According to the Absolute Maximum Ratings section of the technical documentation , the total power used by the GPIO should not exceed 1200mA .

Each output can deliver a current of 40mA maximum.

Example for lighting an LED

Here is an example of Arduino code that illustrates how the digital IO of the ESP32 work.

The LED lights up as soon as the push button is pressed and goes out as soon as it is released.

esp32 digital io switch press release



The push button is connected to 4. The LED is connected to output 32.

You can test the operation of the program in the absence of a pull resistor (PULL_UP). You can test using the ESP32’s internal pull-up resistor (PULL_UP) or add a resistor to the circuit ( 10kΩ for example).

esp32 gpio io input output digital led button

The LED must be protected by a resistor, the value of which depends on the output voltage and current of the pin (3.3V – 40mA) and the maximum supply voltage of the LED.

See other assortments

You can use this calculator to determine the required resistance value for your circuit.

Series resistance calculator for one or more LEDs
Supply Voltage in Volt
Forward Tension in Volt
Current in mA
Resistor calculated in Ω
Estimated Power in W

This calculator is used to determine the resistance required to drive one or more LEDs connected in series from a voltage source at a specified current level.

Note. It is preferable to supply the circuit with a nominal power between 2 and 10 times the calculated value in order to avoid overheating.
calculateur resistance serie led resistor calculator
ColorWavelength (nm)Voltage (V) for ⌀3mm LEDVolatge (V) for ⌀5mm LED
Red625-630 1,9-2,12,1-2,2

Upload the Arduino code of the project

Create a new sketch on the Arduino IDE or a new PlatformIO project.

On the Arduino IDE you can remove the first line #include <Arduino.h>.

#include <Arduino.h>

const int PIN_BUTTON = 4;   
const int PIN_LED =  32;    

// variable used to store the state of the button
int button_state = 0;

void setup() {
  // Initializes the input of the button as an input with resistance PULL_DOWN
  // Configure LED output
  pinMode(PIN_LED, OUTPUT);

void loop() {
  // Read the switch value
  button_state = digitalRead(PIN_BUTTON);
  // if the button is pressed, changes the state of the output to HIGH, otherwise LOW
  if (button_state == HIGH) {
    // Switch ON the LED
    digitalWrite(PIN_LED, HIGH);
  } else {
    // Switch OFF the LED
    digitalWrite(PIN_LED, LOW);

PlatformIO configuration for a LoLin D32

Here is an example platformio.ini configuration file for a LoLin D32 Pro development board

platform = espressif32
board = lolin_d32_pro
framework = arduino
monitor_speed = 115200


2/10/2020 Publication of the article

Version Française

Click to rate this post!
[Total: 0 Average: 0]

Thanks for your reading

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
We will be happy to hear your thoughts

      Leave a Reply

      Read more
      Recent posts on the Forum
      DIY Projects
      DIY Projects