[Solved] Problem running my own sketches. Sketches included with the IDE are working fine

Hi!
First post since I can't find a solution to this problem.
All other questions, problems etc since first started using Arduinos, I've found help in this forum (thank you!!).

I've been (slowly) working on a project of mine, involving 2 Nano ESP32.
ESP-NOW, GPS, RTC, SD, Gyro and so on...
But problem is not related to this. It's more of a problem with the IDE and uploading my own sketches.

I ran into a problem where I ended up having to reset the bootloader.
Following these steps.
Since that failed, I found this excellent guide posted by Iburelli (thank you!).
That made it at least possible to upload sketches that comes with the IDE
Examples, Basics, Blink and the BareMinimum (Blink working as supposed to).

As soon as I upload any sketch I've made, both new and old. Nothing happens/is working.

Not even the "simple"

#include "WiFi.h"


void setup(){

  Serial.begin(115200);

  WiFi.mode(WIFI_MODE_STA);

  Serial.println(WiFi.macAddress());

}

 

void loop(){


}

Found in the guide here

Any suggestions on what to try/do next would be much appreciated!!
Thanks in advance
Kind Regards!
DI

1 Like

Hi @mdkdio. The classic Arduino boards like the UNO R3, Mega 2560, and Nano have a special circuit that automatically resets the microcontroller when a serial connection is opened via the board's USB connection. That meant you would always see all the serial output produced by the program running on the board in the Arduino IDE Serial Monitor.

The situation is different with the more modern boards like the Nano ESP32 that have a direct connection between the microcontroller and the USB connection to the computer. There is no auto-reset mechanism on these boards. So any serial output that the sketch program produces between the time the program starts and when the serial connection to the board is initialized will not appear in Serial Monitor.

This is why you'll often see something like this in Arduino sketches:

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

This code will make your program stay in an infinite loop until the serial port is opened in Arduino IDE Serial Monitor (or an equivalent application).

In applications where missing that serial output would be a problem, it's useful to add this code to the sketch in order to make the program wait for the Serial Monitor before running. In your case:

#include "WiFi.h"


void setup(){

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

  WiFi.mode(WIFI_MODE_STA);

  Serial.println(WiFi.macAddress());

}

 

void loop(){


}

Give that a try. I think you will then see the expected output in Serial Monitor.


:warning: Note that if you are wanting to run your program when the board is not connected to Serial Monitor, you must remove that while loop to allow the rest of the sketch to run.


Awesome!
Thank you so much!
And yes, I've seen the while (!Serial)... in code-samples before + used it.
But I didn't think of adding it to this "get MAC address..." sketch

It makes perfect sense, especially with your explanation.
Thank you! Much appreciated!

You are welcome. I'm glad if I was able to be of assistance.

Regards, Per