Sketch uploads to ESP32-S3R8N16 successfully but when the ESP reboots, the sketch does not run

I have an ESP32-S3R8N16 that I have programed with the following simple sketch.

const int intLEDpin = 41;
void setup() {
  Serial.begin(19200); // Starts the serial communication
  delay(1000);
  while (!Serial);
  Serial.println("Starting setup!");
  pinMode(intLEDpin, OUTPUT);

  // Serial.println("Finished with setup!");
}

void loop() {
  Serial.println("on!");
  digitalWrite(intLEDpin, HIGH);
  delay(500);
  Serial.println("off!");
  digitalWrite(intLEDpin, LOW);
  delay(500);
}

Works without an issue when I connect the ESP32 to my computer and upload the sketch to the ESP32.

But when I disconnect the ESP32 and try to power it externally with a 5v power supply it does not work.

So I connect the ESP32 back to my PC and the sketch does not run. I can then upload the sketch again and it will run without any issues. Again, when I disconnect and from my PC and then reconnect it to my PC the sketch will not run.

If I run a similar sketch on an ESP-WROOM-32 and reboot it, power it externally, the sketch works without any issues.

What am I missing here with my ESP32-S3? Am I doing something wrong? Is there something wrong with this MCU? Help! I am losing my mind!

That is because the following line is waiting for the serial connection to the PC to be established.

  while (!Serial);

It is a very common mistake, and one possibility is to replace that line with something like delay(2000); (otherwise you may miss the first few lines of Serial.print() output when connected to the PC).

That was exactly it and after thinking about it, it sure does make sense. The program halts when there is no serial connection.

Now I wish I could get the past few hours of my life.

Thank you so much for your quick response.

Wait...why does the ESP-WROOM-32 work without an issue? Is it just because it is a different MCU and this functions in a different manner?

I've never heard of such an exception, but if Espressif Corp. built one in to their Arduino support package, fine.

The problem only manifests itself on boards with native USB; that is, boards where the main processor provides the USB functionality for communication with the PC.

If the ESP-WROOM-32 board has a serial-to-usb converter (CP210x, CH34x, ...), the USB communication is not provided by the main processor but by that converter.

Find a schematic for the ESP-WROOM-32 (I could not quickly find one) and check. The datasheet for the processor might also tell you.

1 Like

I have a similar board and use the following:

  unsigned long startTime = millis();
  while (!Serial && ((millis() - startTime) < 2000) ) {}

You can probably even drop the startTime since it will be close to zero after startup. The reason I need it is I invariably use the native USB to allow hardware debugging in IDE ver 2.

1 Like

I currently do not know what most of this means but I now have even more to research! Thank you for sharing and explaining!

The check on the elapsed time makes sense here and will certainly get me out of the infinite loop. Thank you!

That's life :rofl:

I do not know which operating system you're using.
For Windows

  1. Find the board in Windows device manager
  2. Right click, select properties (or something like that, not a Windows user at the moment).
  3. Select the details tab and from the drop down select hardware ids.

For Linux

  1. Disconnect the board.
  2. Run dmesg -w (the application will not close)
  3. Connect the board.
  4. Observe the changes.
  5. Close the dmesg using CTRL+C

For a CH341 you will see e.g.

[44432.487175] usb 1-5.1: New USB device found, idVendor=1a86, idProduct=7523, bcdDevice= 2.63

idVendor is the VID, idProduct is the PID. You can look those up on the web.

The information in Windows looks a little different (longer strings containing those numbers) but you should be able to find the numbers as mentioned above.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.