ESP32 I2C for LCD 20x4 Inteface Issue

Hi everyone, I ask for your help with a rather strange problem.
I connected a 20x4 LCD to the ESP32 module with the VIN, GND, SCL (D22) and SDA (D21) connections, and scanned with the code below.

byte error, address;
  for(address = 1; address < 127; address++ ) {
  error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address<16) {

if instead I connect it to GPIO 2 and 4, and add the code:


It works fine, although in this case the sketch must be uploaded with the connections disconnected. If you don't, you get this error:

Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, Coding Scheme None
Crystal is 40MHz
MAC: 30:ae:a4:1b:70:38
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Configuring flash size...
Warning: Could not auto-detect Flash size (FlashID=0xffffff, SizeID=0xff), defaulting to 4MB
Compressed 8192 bytes to 47...

A fatal error occurred: Timed out waiting for packet content
A fatal error occurred: Timed out waiting for packet content

What am I missing?

Dont let pin 2 be pulled high during reset\powerup, otherwise you get the Warning about Flash size and cannot upload.

I found that adding 4.7kΩ external 5V pull-ups to the ESP32's SDA and SCL lines were improving the stability of my I2C connection