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).
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.
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.
I do not know which operating system you're using.
For Windows
Find the board in Windows device manager
Right click, select properties (or something like that, not a Windows user at the moment).
Select the details tab and from the drop down select hardware ids.
For Linux
Disconnect the board.
Run dmesg -w (the application will not close)
Connect the board.
Observe the changes.
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.