Serial Output in setup not working

Hi,

if I try to put an Serial-Output inside the setup, it's not working.

#include <WiFi.h>

void setup() {
  Serial.begin(115200); // Set up Serial Monitor  
  WiFi.mode(WIFI_STA);  // Set ESP32 as a Wi-Fi Station
  Serial.print("MAC-Address: ");
  Serial.println(WiFi.macAddress()); // Show MAC-Address
}
 
void loop() {/*...*/}

Any idea? There's nothing in the serial-monitor!

Best

Erik

You are not giving the board much time to initialise the Serial interface before trying to use it

Start by adding a delay() of 500 milliseconds after the Serial.begin()

Or, rather than an arbitrary delay, wait until it is ready:

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
}

As shown here:

Shouldn’t the board be ready when it returns from begin()

(The issue might be more on the computer side?)

@EriksArduino hasn't said what board it is; in particular, whether it has native USB

I'm using a Arduino-Nano-ESP32

A great idea until you unplug the USB and power the board from an external power supply

That board has native USB as far as I know; so try the suggestion in post #3.

There are ways around that. One also has to be careful when it's first connected to external power and USB and later USB is disconnected; on at least the 32U4 based boards, that can bring your board to a near grinding halt if one keeps on printing to Serial. OP needs to test that if it's a requirement.

I know, but don't let's make things any more complicated than it needs to be. Adding a delay() after the begin() is a quick and dirty way of identifying the problem before deciding the the most appropriate solution based on requirements

Thanks a lot! That works!

Best

Erik

:nerd_face:

Note the warnings from @UKHeliBob that your code now relies upon that USB connection being active at startup, and remaining active...