Fix ESP32 Crash When Remapping I2C Pins: Wire.begin(34, 35) Reboot Loop
ESP32 crashing and rebooting when calling Wire.begin(34, 35) or using GPIO 34, 35, 36, 39 for I2C? Learn why GPI pins are input-only and which pins to use.
- Detailed root cause analysis for error code MCU_ERR_0057.
- Oscilloscope / logic analyzer diagnostic symptoms and pin voltage thresholds.
- Drop-in verified C++ code patch with circuit protection hardware schematics.
Quick Answer: 60-Second Summary
Symptom: You attempt to remap the ESP32 I2C bus to custom pins using Wire.begin(34, 35) (or any combination involving pins 34, 35, 36, or 39). The code compiles without a single warning, but as soon as Wire.begin() executes in setup(), the ESP32 prints an error (i2c: i2c_set_pin(826): sda_io_num error or GPIO_IS_VALID_OUTPUT_GPIO assertion failure) and enters an infinite reboot loop.
Root Cause (Input-Only Silicon Pads on GPIO 34–39): While the ESP32's peripheral GPIO Matrix allows routing I2C to almost any pin, GPIOs 34, 35, 36 (VP), and 39 (VN) are physically Input-Only pins (GPI). At the silicon level, these four pads completely lack output driver MOSFETs and internal pull-up/pull-down resistors. Because the I2C protocol requires bidirectional open-drain output capability on both SDA (to transmit data and ACK bits) and SCL (to drive the clock and support clock stretching), the ESP-IDF I2C driver asserts a fatal hardware error when passed an input-only pin.
The Solution
- Use Bidirectional GPIOs (GPIO0–GPIO33): Move your I2C SDA and SCL lines to true bidirectional pins. The default hardware I2C pins are GPIO21 (SDA) and GPIO22 (SCL).
- Recommended Safe Custom I2C Pairs: If 21 and 22 are occupied, use
Wire.begin(16, 17),Wire.begin(18, 19),Wire.begin(25, 26), orWire.begin(32, 33). - Reserve GPIOs 34, 35, 36, and 39 Strictly for Inputs: Use pins 34–39 exclusively for
analogRead(), digital button inputs (with external pull-ups), or single-direction interrupt inputs.
The Input-Only Silicon Trap on GPIO 34 & 35
One of the most celebrated features of the Espressif ESP32 is its Hardware GPIO Matrix. Unlike older microcontrollers (such as the Arduino Uno, where I2C is permanently hardwired to pins A4 and A5), the ESP32 allows you to route UART, SPI, PWM, and I2C signals to almost any pin in software using a single line of code:
#include <Wire.h>
void setup() {
Serial.begin(115200);
// Attempt to remap I2C to GPIO 34 and GPIO 35:
Wire.begin(34, 35); // <-- INSTANT CRASH & REBOOT LOOP!
}The sketch compiles cleanly. Yet the instant it runs, the serial monitor erupts with a fatal error or assertion failure:
E (312) i2c: i2c_set_pin(826): scl_io_num error
E (312) i2c: i2c_param_config(749): i2c clock choice is invalid
abort() was called at PC 0x400d182b on core 1
Guru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout / abort)
Rebooting...Technical Deep-Dive: Standard GPIOs vs. Input-Only GPIs (34–39)
To understand why Wire.begin(34, 35) crashes, we need to compare the internal silicon CMOS pad circuitry of pins 0–33 against pins 34–39.
ESP32 SILICON PAD ARCHITECTURE COMPARISON:
Standard Bidirectional GPIO (Pins 0 - 33):
┌───────────────────────────────────────────────────────────â”
│ Internal 45kΩ Pull-Up / Pull-Down │
│ │ │
│ Output ──► [Open-Drain / Push-Pull FETs] ──┬──► Physical │
│ Driver │ Pin Pad │
│ Input ◄── [Schmitt Trigger Input Buffer] ─┘ │
└───────────────────────────────────────────────────────────┘
* Can pull SDA & SCL LOW! (Full I2C Support)
Input-Only GPI Pad (Pins 34, 35, 36/VP, 39/VN):
┌───────────────────────────────────────────────────────────â”
│ [ NO Pull-Up / Pull-Down Resistors! ] │
│ [ NO Output Driver MOSFETs! ] │
│ │
│ Input ◄── [Input Buffer & ADC1 Mux] ────────── Physical │
│ Pin Pad │
└───────────────────────────────────────────────────────────┘
* Physically CANNOT drive a signal LOW! (I2C Impossible!)1. Why I2C Demands Bidirectional Output Capability
The I2C (Inter-Integrated Circuit) bus is a bidirectional open-drain bus:
Both SDA (Data) and SCL (Clock) are held HIGH to 3.3V by pull-up resistors. To transmit a start condition, clock pulse, address bit, or data bit, the ESP32 I2C master must turn on an internal N-channel MOSFET to pull the line down to 0V (GND). Even SCL (which beginners often assume is an output-only clock line) must be open-drain bidirectional so slave devices can hold SCL low during clock stretching.
2. The Silicon Missing from Pins 34, 35, 36, and 39
In the ESP32 Technical Reference Manual, pins 34, 35, 36, 37, 38, and 39 belong to the RTC/ADC low-noise analog input domain.
- They are properly named GPIs (General Purpose Inputs), not GPIOs!
- They do not have output stage transistors (
GPIO_ENABLE_W1TS_REGonly covers pins 0–33). - They do not have internal software pull-up or pull-down resistors (
pinMode(34, INPUT_PULLUP)does nothing).
3. The Driver Validation Check
When you call Wire.begin(34, 35), the Arduino core calls the underlying ESP-IDF function i2c_set_pin(). Inside i2c_set_pin(), the driver executes a hardware macro check:
I2C_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(sda_io_num), "sda_io_num error", ESP_ERR_INVALID_ARG);
I2C_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(scl_io_num), "scl_io_num error", ESP_ERR_INVALID_ARG);Because GPIO_IS_VALID_OUTPUT_GPIO(x) evaluates to false for any pin ≥34 on the classic ESP32, the driver aborts initialization, leaving uninitialized I2C handles that immediately crash or assert when accessed!
How to Fix It: Choosing Safe Custom I2C Pins on ESP32
To remap I2C cleanly on an ESP32 without crashing, simply select two pins below GPIO34 that do not conflict with internal SPI flash or boot strapping:
#include <Wire.h>
// SAFE CUSTOM I2C PIN REMAPPING OPTIONS:
// Option 1 (Default Hardware Pins): SDA = 21, SCL = 22
// Option 2 (Adjacent Analog-Capable Pins): SDA = 32, SCL = 33
// Option 3 (General Purpose Digital Pins): SDA = 25, SCL = 26
// Option 4 (Secondary UART/Digital Pins): SDA = 18, SCL = 19
#define CUSTOM_SDA 32
#define CUSTOM_SCL 33
void setup() {
Serial.begin(115200);
// Initialize I2C on bidirectional pins 32 and 33:
bool success = Wire.begin(CUSTOM_SDA, CUSTOM_SCL, 100000);
if (success) {
Serial.println("I2C successfully initialized on GPIO 32 (SDA) and GPIO 33 (SCL)!");
} else {
Serial.println("I2C initialization failed!");
}
}
void loop() {
// Scan or read I2C devices normally...
}Pro Tip for Breadboard Layouts: Notice why developers love pins 34 and 35? Because they sit right next to pins 32 and 33 on the left header of a standard ESP32 DevKit! Simply move your two jumper wires one notch over from (34, 35) to (32, 33). Pins 32 and 33 are full bidirectional GPIOs and work with Wire.begin(32, 33)!
Complete ESP32 Pin Capability Matrix for I2C Remapping
| GPIO Pin Range | Direction Capability | Safe for I2C (SDA/SCL)? | Notes & Hardware Restrictions |
|---|---|---|---|
| GPIO 21, 22 | Bidirectional | YES (Default Best Choice) | Standard Arduino-ESP32 default I2C pins. |
| GPIO 16, 17, 18, 19, 23, 25, 26, 27, 32, 33 | Bidirectional | YES (Excellent) | Clean, unrestricted bidirectional GPIOs. (Note: Avoid 16/17 on ESP32-WROVER with PSRAM). |
| GPIO 4, 5, 13, 14 | Bidirectional | YES (Good) | Safe for I2C after boot. |
| GPIO 0, 2, 12, 15 | Bidirectional (Strapping) | Caution | Boot strapping pins; I2C pull-up resistors on GPIO2 or GPIO12 can block flashing or boot! |
| GPIO 1, 3 | Bidirectional (UART0) | Avoid | Hardwired to USB-to-UART chip (TX0/RX0). |
| GPIO 6, 7, 8, 9, 10, 11 | Internal SPI Flash | STRICTLY FORBIDDEN | Connected to internal SPI Flash chip; touching them triggers an instant crash! |
| GPIO 34, 35, 36, 39 | Input-Only (GPI) | IMPOSSIBLE (Crashes) | No output transistors! Cannot pull SDA or SCL low. |
Frequently Asked Questions
Can I use GPIO 34 or 35 for SCL if the ESP32 is the only master on the bus?
No. Even on a single-master bus, SCL must be driven LOW and released HIGH by the master's output transistor on every single clock cycle (100,000 times per second at 100 kHz). Because GPIO 34 and 35 have no output transistors at all, they cannot generate a clock signal.
Can I use Two I2C Buses (Wire and Wire1) on an ESP32?
Yes! The ESP32 silicon contains two independent hardware I2C controllers (I2CNUM0 and I2CNUM1). You can initialize both buses simultaneously on any valid bidirectional pins:
Wire.begin(21, 22, 100000); // Bus 0 on GPIO 21 & 22
Wire1.begin(32, 33, 400000); // Bus 1 on GPIO 32 & 33For more bare-metal embedded tutorials, register-level debugging guides, and ESP32 hardware pinout workflows, explore Mentora School.
Learn this in our Senior Tech: Python, IoT & AI Basics
Transition from visual blocks to authentic text-based Python 3. Code interactive terminal apps, algorithmic logic, and computer vision models.