Serial Monitor of Arduino IDE not working reliable with Nano ESP32 (window close and re-open required)

Formerly working with other devices of the Nano Family (Nano 33 BLE, Nano 33 BLE Sense) I changed over to the ESP32. For most of my sketches I used while(!Serial); to wait for the serial monitor connection to be established.

This does not work reliable with Nano ESP32. Sometimes the printed output of the sketch becomes visible, sometimes not. I tried multiple combinations (compiling and uploading with and without an opened Serial Monitor window; one or multiple instances of Arduino IDE opened; some or no more Serial Monitor windows opened; enabled/disabled Arduino_Create_Agent; browser not/connected to Arduino IoT cloud), but I could not reproduce a stable pattern when it works or when it does not work.

Only reliable solution is to remove while(!Serial); from the code and insert some delay(1000); command to ensure the first lines printed are catched by the serial connection. Weird.

---EDIT--- delay(1000) also not reliable, so I guess this is an issue of serial connection

Is there a known problem or another method to interrupt the execution for as long as the serial connection is not established?

BTW: I'm going to replace all of my mbed-based Nano 33 devices, since there is a major issue with mbed's Wire library. Wire.endTransmission() does a read() instead of a write() if the transmitBuffer is empty (usedTxBuffer == 0), causing problems with some I2C peripherals, especially with detecting devices with common I2C scanning methods.

see discussions:
https://forum.arduino.cc/t/nano-33-ble-i2c-problems-caused-in-mbed-wire-cpp-read-write-bit-on-i2c-scan/964676/6
https://github.com/arduino/ArduinoCore-mbed/issues/414
https://github.com/arduino/ArduinoCore-mbed/pull/415

I know, this is off-topic, but I'm so disappointed about it. It took me several weeks to find the cause, wrote some discussions in the forum and created a pull request in GitHub. But nothing happens. I'm so tired to fix this piece of sh*t in the code after each mbed_nano library's update.

Wire.cpp looks good now for Nano ESP32 and works with all my devices.

Respected Arduino team, can you please bring working stuff to the market? I know, the offered combination of hardware, software and cloud platform is complex, but at least advertised foundations should work. Thank you!

mbed_nano Wire.cpp (4.0.4):

uint8_t arduino::MbedI2C::endTransmission(bool stopBit) {
	#ifndef TARGET_PORTENTA_H7
	if (usedTxBuffer == 0) {
		// we are scanning, return 0 if the addresed device responds with an ACK
		char buf[1];
		int ret = master->read(_address, buf, 1, !stopBit);
		return ret;
	}
	#endif
	if (master->write(_address, (const char *) txBuffer, usedTxBuffer, !stopBit) == 0) return 0;
	return 2;
}

esp32 Wire.cpp (2.0.11):

/*
https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
endTransmission() returns:
0: success.
1: data too long to fit in transmit buffer.
2: received NACK on transmit of address.
3: received NACK on transmit of data.
4: other error.
5: timeout
*/
uint8_t TwoWire::endTransmission(bool sendStop)
{
    if(is_slave){
        log_e("Bus is in Slave Mode");
        return 4;
    }
    if (txBuffer == NULL){
        log_e("NULL TX buffer pointer");
        return 4;
    }
    esp_err_t err = ESP_OK;
    if(sendStop){
        err = i2cWrite(num, txAddress, txBuffer, txLength, _timeOutMillis);
#if !CONFIG_DISABLE_HAL_LOCKS
        //release lock
        xSemaphoreGive(lock);
#endif
    } else {
        //mark as non-stop
        nonStop = true;
#if !CONFIG_DISABLE_HAL_LOCKS
        nonStopTask = xTaskGetCurrentTaskHandle();
#endif
    }
    switch(err){
        case ESP_OK: return 0;
        case ESP_FAIL: return 2;
        case ESP_ERR_TIMEOUT: return 5;
        default: break;
    }
    return 4;
}

that's hard blocking code..
esp32 probably won't like that too much..
could trip a watchdog and reboot..
does it work if you change it to..

while(!Serial) vTaskDelay(5);

this should yield and allow other processes to run..

cores/esp32/main.cpp

good luck.. ~q

One step further I found that the problem is not with while(!Serial), but with the Serial Monitor. I can stable reproduce a behavior of Arduino IDE, where I can force written output to be displayed (after compilation and upload) when I close an opened Serial Monitor window an re-open again or - if not already opened - if I open, close an re-open again. Somehow this window needs to be closed and re-opened for serial connection to be established.

Any thought on that?

Moved to > Software > IDE 2.x Forum, since this seems to be a bug related to Arduino IDE (using 2.2.1 here).

Hi, can you share the steps to reproduce the error related to the Serial Monitor?

Also, share the screenshots or details of the error. If its not easy to share the outputs here please open a ticket with us using the contact us form and we can collect all the required information related to the issue.

Hi @jojo . The steps are fully described here:

PC: Windows 11
IDE: Arduino IDE 2.2.1
Board: Nano ESP32
ArduinoCreateAgent: paused or quit (tested both, same result, so it's not likely to interfere from that)

After compilation and upload the Serial Monitor needs to be closed and re-opened for output to be displayed. That's it.

I cross-checked with another board (Nano 33 BLE) and that works as expected. No need to close and re-open the Serial Monitor window.

1 Like

Hi I think I'm having the exact same issue. When resetting the board or uploading a sketch, the Serial Monitor does seem to connect to the board because I'm unable to connect the Serial Monitor in the old Arduino 1.8 program or through VSCode. But this "connection" is not complete and seems to somehow not be triggering the board to recognize that a connection has been made

  • If the Serial Monitor window is open or closed when I reset the board or upload a sketch, I have to close and reopen the Serial Monitor to trigger the correct behavior.
  • Performing the same tests with this sketch on other boards, or this board using Arduino 1.8 or VSCode serial monitors has no issues and functions as expected.

image

void setup() 
{
  Serial.begin(9600);

  while (!Serial) { yield();}
  delay(1000);

  Serial.println(F("Type any character to start"));
  while (!Serial.available()) { yield(); }
  pinMode(LED_BUILTIN, OUTPUT);
}


void loop() 
{
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(1000);                      
}

Is this the same?

Probably.

Yes, thank you!

Closing and re-opening the serial monitor displayed the data!

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