Skip to content
Web app (opens in a new tab)Get the kit
Web app (opens in a new tab)Get the kit

ESP32 Guides

ESP32 Pinout Explained: Which GPIO Pins Are Safe to Use

Most ESP32 pins are free to use, but a few are wired to the flash chip, some can only read, five decide how the board starts, and the ADC2 analog pins stop working when Wi-Fi is on. This guide sorts every pin into safe, careful and avoid, then shows a real board's pin map as a worked example.

A green ESP32 DevKit seen from above with three parts wired to its header pins: a turning knob on a green wire marked with a tick, a push button on a yellow wire marked with a warning triangle, and a wire held back from a red-circled pin with a no-entry sign.
A green ESP32 DevKit seen from above with three parts wired to its header pins: a turning knob on a green wire marked with a tick, a push button on a yellow wire marked with a warning triangle, and a wire held back from a red-circled pin with a no-entry sign.

The ESP32 pinout looks crowded, but the rule behind it is short. The chip has 34 GPIO pins. Most of them can be inputs or outputs. A few cannot be used at all, because they talk to the flash memory that holds your program. Four can only read. Five are strapping pins that decide how the chip starts, so a part on them can stop the board from booting. And the analog pins on the second converter, ADC2, give no readings while Wi-Fi is on.

So the safe pins for everyday work are GPIO 4, 13, 14, 16 to 19, 21 to 23, 25 to 27, 32 and 33, plus the input-only pins 34 to 39 for reading sensors. This guide explains each group, gives you a table to keep beside your desk, and walks through the pin map of a real board, the MakerBuddy IoT Kit V1.0, so you can see how the rules work in practice.

ESP32 GPIO pins at a glance

Here is every GPIO on the ESP32 chip, sorted by how freely you can use it. The pin numbers, channels and limits come from the ESP32 Series Datasheet (opens in a new tab) (table 2-1 and section 3) and Espressif's GPIO reference (opens in a new tab), which lists GPIO 0 to 19, 21 to 23, 25 to 27 and 32 to 39 as the 34 physical GPIO pins.

GPIOGroupAnalog channelCan it output?What to know
0StrappingADC2YesLow at start-up puts the chip into download mode. The BOOT button pulls it low
1, 3Serial (TX0, RX0)noneYesUsed for uploading and the Serial Monitor. Leave them free
2StrappingADC2YesMust be low or unconnected to enter download mode
4SafeADC2YesGood general pin. Analog only with Wi-Fi off
5StrappingnoneYesSets an SDIO timing option at start-up. Usually harmless
6 to 11FlashnoneDo not useWired to the SPI flash chip. Using them can crash the program
12StrappingADC2YesMust be low at start-up, or the flash gets the wrong voltage
13, 14SafeADC2YesAlso JTAG debug pins, which most projects never use
15StrappingADC2YesLow at start-up hides the boot messages. Otherwise harmless
16, 17Safe on WROOMnoneYesTaken by the PSRAM on WROVER modules
18, 19, 21, 22, 23SafenoneYes21 and 22 are the default I²C pins in Arduino
25, 26SafeADC2YesAlso the two DAC outputs
27SafeADC2YesGood general pin
32, 33SafeADC1YesAnalog that keeps working with Wi-Fi on
34, 35, 36, 39Input onlyADC1NoNo output and no internal pull-up or pull-down
GPIO
0
Group
Strapping
Analog channel
ADC2
Can it output?
Yes
What to know
Low at start-up puts the chip into download mode. The BOOT button pulls it low
GPIO
1, 3
Group
Serial (TX0, RX0)
Analog channel
none
Can it output?
Yes
What to know
Used for uploading and the Serial Monitor. Leave them free
GPIO
2
Group
Strapping
Analog channel
ADC2
Can it output?
Yes
What to know
Must be low or unconnected to enter download mode
GPIO
4
Group
Safe
Analog channel
ADC2
Can it output?
Yes
What to know
Good general pin. Analog only with Wi-Fi off
GPIO
5
Group
Strapping
Analog channel
none
Can it output?
Yes
What to know
Sets an SDIO timing option at start-up. Usually harmless
GPIO
6 to 11
Group
Flash
Analog channel
none
Can it output?
Do not use
What to know
Wired to the SPI flash chip. Using them can crash the program
GPIO
12
Group
Strapping
Analog channel
ADC2
Can it output?
Yes
What to know
Must be low at start-up, or the flash gets the wrong voltage
GPIO
13, 14
Group
Safe
Analog channel
ADC2
Can it output?
Yes
What to know
Also JTAG debug pins, which most projects never use
GPIO
15
Group
Strapping
Analog channel
ADC2
Can it output?
Yes
What to know
Low at start-up hides the boot messages. Otherwise harmless
GPIO
16, 17
Group
Safe on WROOM
Analog channel
none
Can it output?
Yes
What to know
Taken by the PSRAM on WROVER modules
GPIO
18, 19, 21, 22, 23
Group
Safe
Analog channel
none
Can it output?
Yes
What to know
21 and 22 are the default I²C pins in Arduino
GPIO
25, 26
Group
Safe
Analog channel
ADC2
Can it output?
Yes
What to know
Also the two DAC outputs
GPIO
27
Group
Safe
Analog channel
ADC2
Can it output?
Yes
What to know
Good general pin
GPIO
32, 33
Group
Safe
Analog channel
ADC1
Can it output?
Yes
What to know
Analog that keeps working with Wi-Fi on
GPIO
34, 35, 36, 39
Group
Input only
Analog channel
ADC1
Can it output?
No
What to know
No output and no internal pull-up or pull-down

Two more GPIOs, 37 and 38, exist inside the chip, but the common ESP32-WROOM-32E module does not bring them out to a pin. That module's own datasheet (opens in a new tab) lists 38 module pins and up to 26 usable GPIOs.

If you are new to the chip itself, start with ESP32 for beginners: what it is and how to start, then come back here when you are choosing pins.

How to read an ESP32 pin diagram

A pin diagram, or pinout, maps the header pins on your board and every job each pin can do. It looks busy because one ESP32 pin can be a GPIO, an analog input, a touch sensor and part of a serial bus. You pick one job in your code. Three things trip people up.

First, there are two kinds of names. The number you use in Arduino code is the GPIO number. Board makers often print shorter labels, such as IO2 or just 2, and both mean GPIO 2. Be careful with labels that start with D. On Espressif's own ESP32-DevKitC (opens in a new tab), D0 to D3 are flash pins you must not use. Some diagrams also show VP and VN, or SENSOR_VP and SENSOR_VN: the DevKitC guide and the ESP32-WROOM-32E datasheet (opens in a new tab) show these are GPIO 36 and GPIO 39.

Second, the chip, the module and the board are different things. The ESP32 chip sits inside a metal-covered module, such as the ESP32-WROOM-32E. The module sits on a development board, or DevKit, which adds the USB port, the voltage regulator and the BOOT and EN buttons. The ESP32-WROOM-32E datasheet (opens in a new tab) shows the module's 38 pins, and its note 2 says GPIO 6 to 11 connect to the module's flash and are not brought out at all. So an "esp32 wroom 32 pinout" diagram shows the module's pads, while a DevKit diagram shows the header pins you plug wires into.

Third, boards come in different sizes. Espressif's own ESP32-DevKitC (opens in a new tab) has two rows of 19 pins, 38 in all. Many low-cost DevKits have 30 pins and leave some GPIOs out. The GPIO numbers stay the same, but the physical position of each pin changes between boards. Always read the labels printed on your own board, not a diagram of a different one. The guide to ESP32 development boards, DevKit V1 and WROOM-32 compares the common layouts.

The power pins sit on the same headers: 3V3, GND, 5V or VIN, and EN, which resets the chip when pulled low. The DevKitC guide (opens in a new tab) warns you to power the board through one option only, USB or the 5V pin or the 3V3 pin, or the board or the supply can be damaged.

Pins you should never use: GPIO 6 to 11

GPIO 6 to 11 connect the chip to its SPI flash memory, which holds your program. The ESP32 reads instructions from that flash while it runs. Wire a part to one of these pins and the chip loses its link to its own program, and can crash or reset.

On Espressif's DevKitC these pins are labelled CLK, CMD and D0 to D3, and the DevKitC guide (opens in a new tab) says to avoid them, as they may disrupt access to the flash. On a WROOM-32E module they are not brought out at all. Treat any pin with those labels as off limits.

A smaller version of the rule covers GPIO 16 and 17. The DevKitC guide (opens in a new tab) says they are free on WROOM modules but reserved on boards with a WROVER module, which adds extra memory called PSRAM. If the metal can on your board says WROVER, leave them alone.

Input-only pins: GPIO 34, 35, 36 and 39

Four pins on the header can only read. The Espressif GPIO page (opens in a new tab) says GPIO 34 to 39 can only be set as inputs and have no software pull-up or pull-down. The ESP32 Series Datasheet (opens in a new tab) marks them as input-only pins with no pull-up or pull-down resistors.

That makes them good for some jobs and wrong for others.

JobOn GPIO 34 to 39?Why
Analog sensor (light sensor, knob, soil sensor)Yes, idealThey are ADC1 pins, so they work with Wi-Fi on
Digital sensor with its own output driver (a PIR module)YesThe sensor drives the line high and low itself
Push button with INPUT_PULLUPOnly with an outside resistorNo internal pull-up, so the pin floats when the button is open
LED, buzzer, relay, servoNoThey need an output pin
Job
Analog sensor (light sensor, knob, soil sensor)
On GPIO 34 to 39?
Yes, ideal
Why
They are ADC1 pins, so they work with Wi-Fi on
Job
Digital sensor with its own output driver (a PIR module)
On GPIO 34 to 39?
Yes
Why
The sensor drives the line high and low itself
Job
Push button with INPUT_PULLUP
On GPIO 34 to 39?
Only with an outside resistor
Why
No internal pull-up, so the pin floats when the button is open
Job
LED, buzzer, relay, servo
On GPIO 34 to 39?
No
Why
They need an output pin

A floating input is the classic trap. Put a plain button on GPIO 34 with INPUT_PULLUP and the code compiles, but no pull-up turns on, so the reading drifts as your hand nears the wire. Add a 10 kΩ resistor from the pin to 3.3 V, or use a pin with an internal pull-up.

Strapping pins: GPIO 0, 2, 5, 12 and 15

Strapping pins are read once, at the instant the chip powers up or resets. Each one sets an option, such as whether to run your program or wait for a new one over USB. The datasheet says the chip latches these values at reset and keeps them until power-down, and the pins then work as normal GPIOs. The chip reads them during a hold time of at least 1 ms after reset is released, according to table 3-2 of the ESP32 Series Datasheet (opens in a new tab).

So a strapping pin is not forbidden. It is a pin where anything connected must not pull it the wrong way during start-up. Here is what each one does, from section 3 of the same datasheet.

PinDefault at resetWhat it setsSafe-use rule
GPIO 0Pulled up (1)Low together with GPIO 2 low means download mode instead of running your programDo not let a part pull it low at start-up. The BOOT button uses it on purpose
GPIO 2Pulled down (0)Must be 0 for download modeDo not let a part pull it high at start-up, or uploads fail
GPIO 12 (MTDI)Pulled down (0)0 powers the flash at about 3.3 V, 1 switches it to 1.8 VDo not let a part pull it high at start-up
GPIO 15 (MTDO)Pulled up (1)0 turns off the boot messages on the serial portMostly harmless. A low level only silences the start-up log
GPIO 5Pulled up (1)Timing of the SDIO slave interfaceHarmless for normal use
Pin
GPIO 0
Default at reset
Pulled up (1)
What it sets
Low together with GPIO 2 low means download mode instead of running your program
Safe-use rule
Do not let a part pull it low at start-up. The BOOT button uses it on purpose
Pin
GPIO 2
Default at reset
Pulled down (0)
What it sets
Must be 0 for download mode
Safe-use rule
Do not let a part pull it high at start-up, or uploads fail
Pin
GPIO 12 (MTDI)
Default at reset
Pulled down (0)
What it sets
0 powers the flash at about 3.3 V, 1 switches it to 1.8 V
Safe-use rule
Do not let a part pull it high at start-up
Pin
GPIO 15 (MTDO)
Default at reset
Pulled up (1)
What it sets
0 turns off the boot messages on the serial port
Safe-use rule
Mostly harmless. A low level only silences the start-up log
Pin
GPIO 5
Default at reset
Pulled up (1)
What it sets
Timing of the SDIO slave interface
Safe-use rule
Harmless for normal use

GPIO 12 is the one that bites. If a sensor or pull-up holds it high while the board starts, the chip switches the flash supply to 1.8 V instead of the usual 3.3 V. The esptool boot mode guide (opens in a new tab) says this can stop a board with 3.3 V flash from booting or uploading, because the flash browns out. Nothing is broken. Unplug the part and it starts again.

GPIO 0 and GPIO 2 choose the boot mode. The esptool troubleshooting guide (opens in a new tab) describes manual download mode as holding BOOT and pressing EN, which works because BOOT pulls GPIO 0 low as the reset ends.

ESP32 start-up timeline: the 3.3 V supply rises, EN goes high, and for at least 1 ms the chip latches GPIO 0, 2, 5, 12 and 15 before releasing them as normal GPIOs. GPIO 0 high runs your program; GPIO 0 low with GPIO 2 low waits for an upload, which is what the BOOT button does.
ESP32 start-up timeline: the 3.3 V supply rises, EN goes high, and for at least 1 ms the chip latches GPIO 0, 2, 5, 12 and 15 before releasing them as normal GPIOs. GPIO 0 high runs your program; GPIO 0 low with GPIO 2 low waits for an upload, which is what the BOOT button does.

The safe pattern is to put parts that stay quiet at power-up on strapping pins: an LED driven only by your code, a button not pressed at start-up, or a sensor trigger line. Avoid parts that pull the pin the wrong way on their own, such as a module with a built-in pull-up on GPIO 12.

ADC1 vs ADC2: which analog pins work with Wi-Fi

The ESP32 has two analog-to-digital converters. The ESP32 Series Datasheet (opens in a new tab) says they cover 18 channels with 12-bit resolution. In Arduino, analogRead() returns a raw number from 0 to 4095, according to the Arduino-ESP32 ADC reference (opens in a new tab).

The catch is that the Wi-Fi driver uses ADC2. Espressif's GPIO page (opens in a new tab) states it plainly: ADC2 pins cannot be used when Wi-Fi is used. Since most ESP32 projects use Wi-Fi, the practical rule is to put every analog sensor on ADC1.

ConverterHeader pinsWorks with Wi-Fi on?Use it for
ADC1GPIO 32, 33, 34, 35, 36, 39YesEvery analog sensor in a Wi-Fi project
ADC2GPIO 0, 2, 4, 12, 13, 14, 15, 25, 26, 27NoAnalog only in projects with Wi-Fi off; otherwise use these pins as digital GPIOs
Converter
ADC1
Header pins
GPIO 32, 33, 34, 35, 36, 39
Works with Wi-Fi on?
Yes
Use it for
Every analog sensor in a Wi-Fi project
Converter
ADC2
Header pins
GPIO 0, 2, 4, 12, 13, 14, 15, 25, 26, 27
Works with Wi-Fi on?
No
Use it for
Analog only in projects with Wi-Fi off; otherwise use these pins as digital GPIOs

That gives you six analog pins in a Wi-Fi project, four of them input-only, which suits sensors well.

Two more limits matter. The Arduino-ESP32 page gives the widest setting, 11 dB attenuation, a useful range of about 150 mV to 3100 mV on the ESP32, not the full 0 to 3.3 V. And raw readings are uncalibrated; analogReadMilliVolts() returns a calibrated value when you need a real voltage. The upcoming guide to the ESP32 ADC and reading analog sensors accurately covers calibration and averaging in depth.

Voltage and current limits for every pin

Every ESP32 pin works at 3.3 V. The datasheet's DC characteristics (table 5-3) set the highest input voltage a pin should see at the supply voltage plus 0.3 V, which is 3.6 V on a 3.3 V board. A 5 V signal is above that limit. A 5 V sensor output wired straight to a GPIO is outside the chip's ratings, so use a voltage divider or a level shifter. The guide to 3.3 V vs 5 V logic and why the ESP32 does not like 5 V signals shows both fixes.

Current is the other limit. These figures come from tables 5-1 and 5-3 of the ESP32 Series Datasheet (opens in a new tab).

LimitValue in the ESP32 Series Datasheet (opens in a new tab)What it means for you
Current a pin can source (output high)About 40 mA at maximum drive on most pins, falling to about 29 mA as more pins source current. GPIO 16 and 17 sit in the flash supply domain, rated 20 mAFine for an LED with a resistor. Not enough for a motor
Current a pin can sink (output low)About 28 mA at maximum driveSame idea when the load connects to 3.3 V
All pins together1200 mA absolute maximumA ceiling, not a target. Stay far below it
Input high levelAt least 0.75 × supply voltageAbout 2.5 V on a 3.3 V board counts as HIGH
Input low levelAt most 0.25 × supply voltageAbout 0.8 V or less counts as LOW
Limit
Current a pin can source (output high)
Value in the ESP32 Series Datasheet (opens in a new tab)
About 40 mA at maximum drive on most pins, falling to about 29 mA as more pins source current. GPIO 16 and 17 sit in the flash supply domain, rated 20 mA
What it means for you
Fine for an LED with a resistor. Not enough for a motor
Limit
Current a pin can sink (output low)
Value in the ESP32 Series Datasheet (opens in a new tab)
About 28 mA at maximum drive
What it means for you
Same idea when the load connects to 3.3 V
Limit
All pins together
Value in the ESP32 Series Datasheet (opens in a new tab)
1200 mA absolute maximum
What it means for you
A ceiling, not a target. Stay far below it
Limit
Input high level
Value in the ESP32 Series Datasheet (opens in a new tab)
At least 0.75 × supply voltage
What it means for you
About 2.5 V on a 3.3 V board counts as HIGH
Limit
Input low level
Value in the ESP32 Series Datasheet (opens in a new tab)
At most 0.25 × supply voltage
What it means for you
About 0.8 V or less counts as LOW

Those are maximums, not everyday loads. Drive LEDs through a resistor, and motors or relay coils through a transistor, driver board or relay module, never straight from a pin. The guide to motor drivers and why a pin cannot power a motor explains the reason with numbers.

Worked example: the MakerBuddy board's pin map

Rules are easier to remember when you see them applied. The MakerBuddy IoT Kit V1.0 is a learning board built around a 30-pin ESP32 DevKit. It has six parts built in and nine plug-in parts, each with its own labelled socket. Every pin below comes from the MakerBuddy firmware source and the MakerBuddy technical details page.

PartGPIOPin groupWhy this pin works
Turning knob (potentiometer)34Input only, ADC1A knob only needs reading, and ADC1 works with Wi-Fi on
Light sensor (LDR)35Input only, ADC1Same reasons as the knob
MQ-2 smoke and gas sensor33ADC1Analog, so it must be on ADC1
Capacitive soil moisture sensor32ADC1Analog, so it must be on ADC1
Push button5Strapping (SDIO timing)Read with INPUT_PULLUP, which matches the pin's own pulled-up default
Blue light12Strapping (flash voltage)An output the firmware drives only after start-up
Colour light red, green, blue13, 14, 27SafePlain outputs for PWM dimming
Buzzer4SafeA plain on/off output
DHT11 temperature and humidity25SafeA digital data line, so ADC2 does not matter
DS18B20 waterproof probe26SafeA 1-Wire digital line
HC-SR04 distance trigger and echo15 and 2StrappingThe trigger is an output pulsed by the code; the echo is read only after start-up
PIR motion sensor (HC-SR501)18SafeA digital on/off input
SG90 servo19SafePWM output at 50 Hz
Relay (low-voltage loads only)23SafeA plain on/off output
1602 text screen21 (SDA), 22 (SCL)Safe, default I²CArduino-ESP32's default I²C pins, address 0x27
Part
Turning knob (potentiometer)
GPIO
34
Pin group
Input only, ADC1
Why this pin works
A knob only needs reading, and ADC1 works with Wi-Fi on
Part
Light sensor (LDR)
GPIO
35
Pin group
Input only, ADC1
Why this pin works
Same reasons as the knob
Part
MQ-2 smoke and gas sensor
GPIO
33
Pin group
ADC1
Why this pin works
Analog, so it must be on ADC1
Part
Capacitive soil moisture sensor
GPIO
32
Pin group
ADC1
Why this pin works
Analog, so it must be on ADC1
Part
Push button
GPIO
5
Pin group
Strapping (SDIO timing)
Why this pin works
Read with INPUT_PULLUP, which matches the pin's own pulled-up default
Part
Blue light
GPIO
12
Pin group
Strapping (flash voltage)
Why this pin works
An output the firmware drives only after start-up
Part
Colour light red, green, blue
GPIO
13, 14, 27
Pin group
Safe
Why this pin works
Plain outputs for PWM dimming
Part
Buzzer
GPIO
4
Pin group
Safe
Why this pin works
A plain on/off output
Part
DHT11 temperature and humidity
GPIO
25
Pin group
Safe
Why this pin works
A digital data line, so ADC2 does not matter
Part
DS18B20 waterproof probe
GPIO
26
Pin group
Safe
Why this pin works
A 1-Wire digital line
Part
HC-SR04 distance trigger and echo
GPIO
15 and 2
Pin group
Strapping
Why this pin works
The trigger is an output pulsed by the code; the echo is read only after start-up
Part
PIR motion sensor (HC-SR501)
GPIO
18
Pin group
Safe
Why this pin works
A digital on/off input
Part
SG90 servo
GPIO
19
Pin group
Safe
Why this pin works
PWM output at 50 Hz
Part
Relay (low-voltage loads only)
GPIO
23
Pin group
Safe
Why this pin works
A plain on/off output
Part
1602 text screen
GPIO
21 (SDA), 22 (SCL)
Pin group
Safe, default I²C
Why this pin works
Arduino-ESP32's default I²C pins, address 0x27
MakerBuddy IoT Kit V1.0 pin map grouped by pin type: knob 34, light sensor 35, MQ-2 33 and soil sensor 32 on ADC1; push button 5, blue light 12 and distance sensor 15 and 2 on strapping pins; colour light 13, 14 and 27, buzzer 4, DHT11 25, DS18B20 26, PIR 18, servo 19, relay 23 and text screen 21 and 22 on safe pins. GPIO 6 to 11, the flash pins, stay unused.
MakerBuddy IoT Kit V1.0 pin map grouped by pin type: knob 34, light sensor 35, MQ-2 33 and soil sensor 32 on ADC1; push button 5, blue light 12 and distance sensor 15 and 2 on strapping pins; colour light 13, 14 and 27, buzzer 4, DHT11 25, DS18B20 26, PIR 18, servo 19, relay 23 and text screen 21 and 22 on safe pins. GPIO 6 to 11, the flash pins, stay unused.

Three design choices stand out. Every analog sensor sits on ADC1, because the board is always on Wi-Fi for the browser dashboard; the firmware averages four readings of each and scales them to 0 to 100. The input-only pins 34 and 35 carry the knob and the light sensor, parts that never need to output anything. And the four strapping pins in use each hold a part that the firmware controls after start-up. The button on GPIO 5 also has a second job: holding it for 10 seconds clears the saved Wi-Fi.

The same principles apply to your own projects: analog on ADC1, readers on input-only pins, quiet parts on strapping pins and nothing on the flash pins. Week 2 of the MakerBuddy course, Hardware Components and Board Tour walks students through the board's parts and their numbered sockets, and Week 9, Analog Inputs shows the 0 to 4095 readings becoming the 0 to 100 values on the dashboard.

A pin-check sketch you can upload

On MakerBuddy you do not need code: the browser dashboard reads every sensor once a second and switches the outputs for you. If you program the ESP32 yourself in the Arduino IDE, this sketch tests four pin groups on the MakerBuddy board: input-only ADC1 pins, a strapping pin as an input, a strapping pin as a PWM output, and an ordinary output.

cpp
// Pin check for the MakerBuddy board (Arduino-ESP32 3.x).
// The knob dims the blue light, the button sounds the buzzer,
// and the light sensor is printed. Wi-Fi is switched on to show
// that ADC1 pins keep reading while the radio runs.
#include <WiFi.h>

const int KNOB_PIN   = 34;  // input-only pin, ADC1
const int LIGHT_PIN  = 35;  // input-only pin, ADC1
const int BUTTON_PIN = 5;   // strapping pin, read only after start-up
const int LED_PIN    = 12;  // strapping pin (MTDI), driven only after start-up
const int BUZZER_PIN = 4;   // ordinary output pin

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);               // radio on: ADC2 pins would now fail
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // HIGH until pressed, LOW when pressed
  pinMode(BUZZER_PIN, OUTPUT);
  ledcAttach(LED_PIN, 5000, 8);       // 5 kHz PWM, 8-bit duty (0-255)
}

void loop() {
  int knobRaw  = analogRead(KNOB_PIN);   // 0-4095
  int lightRaw = analogRead(LIGHT_PIN);  // 0-4095
  ledcWrite(LED_PIN, map(knobRaw, 0, 4095, 0, 255));

  bool pressed = (digitalRead(BUTTON_PIN) == LOW);
  digitalWrite(BUZZER_PIN, pressed ? HIGH : LOW);

  Serial.printf("knob=%4d light=%4d button=%s\n",
                knobRaw, lightRaw, pressed ? "pressed" : "up");
  delay(200);
}

The lines that matter for pins:

  • WiFi.mode(WIFI_STA) starts the radio without joining a network. Move the knob to an ADC2 pin such as GPIO 25 and its reading would now fail.
  • INPUT_PULLUP works on GPIO 5. It would do nothing on GPIO 34 to 39.
  • ledcAttach(LED_PIN, 5000, 8) sets up 5,000 Hz PWM with 8-bit steps, using the functions in the Arduino-ESP32 LEDC reference (opens in a new tab). GPIO 12 is only driven after setup() runs, long after the strapping pins were read.

Open the Serial Monitor at 115200 baud, turn the knob and cover the light sensor. The numbers should move between near 0 and 4095.

New to uploading? The guide to programming an ESP32 with the Arduino IDE covers installing the esp32 board package and choosing the port.

Common ESP32 pin mistakes and how to fix them

Most pin problems show up as one of these symptoms.

SymptomCauseFix
The board resets in a loop or crashes as soon as the code sets a pinThe pin is one of GPIO 6 to 11, wired to the flashMove the part to a safe pin
Upload fails with a "wrong boot mode" or timeout error, but only with your circuit attachedA part holds GPIO 0 low or GPIO 2 high at start-upUnplug parts from GPIO 0 and 2, upload, then move them to safe pins
The board will not start with one sensor connected, and starts fine without itSomething pulls GPIO 12 high, so the flash gets 1.8 VMove the part off GPIO 12, or remove the pull-up on it
An analog sensor reads fine in a test sketch but gives errors or 0 once Wi-Fi startsThe sensor is on an ADC2 pinMove it to GPIO 32, 33, 34, 35, 36 or 39
An LED or relay on GPIO 34 never turns onGPIO 34 to 39 are input onlyUse an output-capable pin such as 23 or 27
A button on GPIO 35 reads random valuesINPUT_PULLUP does nothing on input-only pins, so the line floatsAdd a 10 kΩ pull-up to 3.3 V, or use a pin with an internal pull-up
The Serial Monitor shows garbage, or uploads stop workingA part is wired to GPIO 1 or 3, the USB serial pinsLeave TX0 and RX0 free
A pin dies or reads wrongly after wiring a 5 V sensorThe input saw more than 3.6 VAdd a voltage divider or level shifter; test the pin before reuse
The analog reading stops at about 4095 well before the sensor's endThe input is above the ADC's useful range, about 3.1 V at 11 dBScale the signal down with a divider
Symptom
The board resets in a loop or crashes as soon as the code sets a pin
Cause
The pin is one of GPIO 6 to 11, wired to the flash
Fix
Move the part to a safe pin
Symptom
Upload fails with a "wrong boot mode" or timeout error, but only with your circuit attached
Cause
A part holds GPIO 0 low or GPIO 2 high at start-up
Fix
Unplug parts from GPIO 0 and 2, upload, then move them to safe pins
Symptom
The board will not start with one sensor connected, and starts fine without it
Cause
Something pulls GPIO 12 high, so the flash gets 1.8 V
Fix
Move the part off GPIO 12, or remove the pull-up on it
Symptom
An analog sensor reads fine in a test sketch but gives errors or 0 once Wi-Fi starts
Cause
The sensor is on an ADC2 pin
Fix
Move it to GPIO 32, 33, 34, 35, 36 or 39
Symptom
An LED or relay on GPIO 34 never turns on
Cause
GPIO 34 to 39 are input only
Fix
Use an output-capable pin such as 23 or 27
Symptom
A button on GPIO 35 reads random values
Cause
INPUT_PULLUP does nothing on input-only pins, so the line floats
Fix
Add a 10 kΩ pull-up to 3.3 V, or use a pin with an internal pull-up
Symptom
The Serial Monitor shows garbage, or uploads stop working
Cause
A part is wired to GPIO 1 or 3, the USB serial pins
Fix
Leave TX0 and RX0 free
Symptom
A pin dies or reads wrongly after wiring a 5 V sensor
Cause
The input saw more than 3.6 V
Fix
Add a voltage divider or level shifter; test the pin before reuse
Symptom
The analog reading stops at about 4095 well before the sensor's end
Cause
The input is above the ADC's useful range, about 3.1 V at 11 dB
Fix
Scale the signal down with a divider

If your board is not detected at all, the problem is usually the cable or the driver rather than a pin. The guide to fixing an ESP32 that is not detected or will not upload works through those causes.

How to choose pins for your own project

When you plan a new build, assign pins in this order.

  1. Cross out GPIO 6 to 11, and 16 and 17 on a WROVER board.

  2. Cross out GPIO 1 and 3, kept for uploads and the Serial Monitor.

  3. Place every analog sensor on ADC1. Fill 34 to 39 first, which saves 32 and 33 for parts that need output or a pull-up.

  4. Place I²C parts, such as a text screen, on GPIO 21 (SDA) and 22 (SCL), the Arduino-ESP32 defaults listed in the core's pins_arduino.h file for the ESP32 (opens in a new tab).

  5. Place outputs and digital sensors on the safe pins: 4, 13, 14, 16 to 19, 23, 25, 26, 27.

  6. Only when the safe pins run out, use strapping pins, and only for parts that stay quiet at start-up. Use GPIO 5 and 15 before 2, 12 and 0.

  7. Write the plan down as a table, like the MakerBuddy map above, before you solder or plug anything in.

Flowchart for choosing an ESP32 pin: first cross out 6 to 11 and 1 and 3. Analog parts go on ADC1 (32, 33, 34, 35, 36, 39), read-only parts on input-only 34, 35, 36 and 39, I²C on 21 and 22, and everything else on the safe pins 4, 13, 14, 16 to 19, 23, 25, 26 and 27. Only when those run out, use strapping pins 5 and 15 first, then 2, 12 and 0.
Flowchart for choosing an ESP32 pin: first cross out 6 to 11 and 1 and 3. Analog parts go on ADC1 (32, 33, 34, 35, 36, 39), read-only parts on input-only 34, 35, 36 and 39, I²C on 21 and 22, and everything else on the safe pins 4, 13, 14, 16 to 19, 23, 25, 26 and 27. Only when those run out, use strapping pins 5 and 15 first, then 2, 12 and 0.

A board with the parts already on sensible pins removes this step for beginners. That is the idea behind the MakerBuddy IoT kit: every socket is labelled, and students see live readings before they ever pick a pin. More guides on the ESP32, sensors and classroom projects are on the MakerBuddy blog.

FAQ

Which ESP32 pins are safe to use?

For most projects the safe pins are GPIO 4, 13, 14, 16 to 19, 21 to 23, 25 to 27, 32 and 33, which can be inputs or outputs. GPIO 34, 35, 36 and 39 are safe as inputs only. On a WROVER board, skip 16 and 17.

Which ESP32 pins should I avoid?

Never use GPIO 6 to 11, because they connect to the flash chip. Leave GPIO 1 and 3 free for uploading and the Serial Monitor. Be careful with the strapping pins 0, 2, 5, 12 and 15, and remember that ADC2 pins cannot read analog values while Wi-Fi is on.

How many GPIO pins does the ESP32 have?

The ESP32 chip has 34 physical GPIO pins, according to Espressif's GPIO reference (opens in a new tab). The ESP32-WROOM-32E module brings out up to 26 of them, according to its datasheet (opens in a new tab). A DevKit board may show fewer, depending on its size. Once you remove the serial and strapping pins from those 26, the table in this guide leaves 15 general pins and 4 input-only pins for parts.

What is the difference between a 30-pin and a 38-pin ESP32 board?

Both use the same chip and GPIO numbers. A 38-pin board, such as Espressif's ESP32-DevKitC, brings out more pins, including the flash pins. A 30-pin board leaves some out and orders the rest differently, so follow the labels on your own board.

Can ESP32 GPIO pins handle 5 V?

No. The ESP32 Series Datasheet (opens in a new tab) sets the highest input level at the supply voltage plus 0.3 V, which is 3.6 V on a 3.3 V board. A 5 V signal is above that rating. Use a voltage divider or a level shifter between a 5 V sensor output and an ESP32 pin.

Why do my analog readings stop working when Wi-Fi is on?

Your sensor is on an ADC2 pin. Espressif's GPIO reference (opens in a new tab) states that ADC2 pins cannot be used while Wi-Fi is in use. Move the sensor to an ADC1 pin: GPIO 32, 33, 34, 35, 36 or 39.

Can I use GPIO 0, 2 or 12 in my project?

Yes, with care. They are strapping pins, read once at start-up. GPIO 0 must not be pulled low and GPIO 2 must not be pulled high then, and GPIO 12 must stay low, or the board will not start or upload normally.

Which ESP32 pins can do PWM?

In Arduino-ESP32, the LEDC peripheral produces PWM, and ledcAttach(pin, freq, resolution) connects it to an output-capable pin you choose. The original ESP32 has 16 LEDC channels, according to the Arduino-ESP32 LEDC reference (opens in a new tab). Input-only pins 34 to 39 cannot output PWM.

Sources

From the blog

Keep reading

ESP32 Guides

Arduino vs Raspberry Pi vs ESP32: Which Board for Which Project?

An Arduino and an ESP32 are microcontroller boards that run one program, while a Raspberry Pi is a small computer with an operating system. See them side by side, then match five classroom projects to the right board.

19 min read

Learn with MakerBuddy

Start building today.

Every part in these guides has its own lesson. Read them free, or get a kit for your class.