DIY Projects

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

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

And this one for PlatformIO (ideally with VSCode)

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.

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 pin Digital input Digital output Note
0 PULL UP Sends a PWM signal at startup.
1 TX Debug output at startup
2 Connected to the on-board LED
3 RX Takes the HIGH state at startup
5 Sends a PWM signal at startup
6 Used for SPI flash memory
7 Used for SPI flash memory
8 Used for SPI flash memory
9 Used for SPI flash memory
10 Used for SPI flash memory
11 Used for SPI flash memory
12 Fail to start if in PULLUP mode
14 Sends a PWM signal at startup
15 Sends 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.

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

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

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

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.



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).

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
Avertissement: la tension directe ne doit pas dépasser la tension d'alimentation.
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.
Color Wavelength (nm) Voltage (V) for ⌀3mm LED Volatge (V) for ⌀5mm LED
Red 625-630  1,9-2,1 2,1-2,2
Blue 460-470 3,0-3,2 3,2-3,4
Green 520-525 2,0-2,2 2,0-2,2
Yellow 585-595 2,0-2,2 3,0-3,2
White 460-470 3,0-3,2 1,9-2,1

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]
Exit mobile version