Strange Serial.print statements on serial monitor

I am trying to write code for a Lilygo ESP32 S3 board with a amoled touch screen. I used Squareline Studio for the output to the screen. The program connects to my WiFi network an outputs the result to the amoled screen. No more than that. No problems.
I've also included some Serial.print() statements.
But.. When I connect a I2C device to the board (an IN226 module) without changing anything in the code, I see first the output from a I2C scanner sketch I used the day before and after that I see the Serial.print statements that I put in the code. When I disconnect the I2C device and reboot the I@C statements on the serial monitor don't show up. How is it possible that the old code (I2C scanner) is still in memory of de ESP board?

#include <LilyGo_AMOLED.h>
#include <LV_Helper.h>
#include "ui.h"  // De gegenereerde ui van Squareline Studio

LilyGo_Class amoled;
#include <WiFi.h>

const char* ssid = "xxxx";
const char* password = "xxxxx";

// Variabelen voor UI widgets
extern lv_obj_t* ui_spinner;    // Directe verwijzing naar de spinner
extern lv_obj_t* ui_wifilabel;  // Directe verwijzing naar het WiFi-label
extern lv_obj_t* ui_Screen2;    // Verwijzing naar het tweede scherm (Screen2)

bool wifiConnected = false;  // Variabele om de WiFi-verbindingstatus bij te houden
unsigned long startTime = 0; // Tijd bij het starten van de verbinding

void setup() {
  delay(10);
  Serial.begin(115200);
  
  // Start de display en LVGL setup
  bool rslt = amoled.begin();
  if (!rslt) {
    while (1) {
      Serial.println("Display niet gedetecteerd.");
      delay(1000);
    }
  }

  beginLvglHelper(amoled);
  ui_init();  // UI initialiseren vanuit Squareline Studio

  // WiFi starten
  connectToWiFi();
}

void connectToWiFi() {
  // Label bijwerken naar "Verbinden met WiFi..."
  lv_label_set_text(ui_wifilabel, "Verbinden met WiFi...");
  Serial.println("Probeer verbinding te maken met WiFi...");

  // Spinner activeren
  lv_obj_clear_flag(ui_spinner, LV_OBJ_FLAG_HIDDEN);  // Zorg dat de spinner zichtbaar is

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  startTime = millis();  // Start tijd bijhouden

  // Probeer verbinding te maken
  while (!wifiConnected) {
    if (WiFi.status() == WL_CONNECTED) {
      wifiConnected = true; // WiFi is verbonden
      Serial.println("WiFi verbonden!");
    } else {
      lv_task_handler();  // Houd LVGL draaiende om de UI bij te werken
      delay(100);         // Kleine vertraging om de CPU niet te overbelasten
    }
  }

  // Als verbonden, label bijwerken naar "WiFi verbonden!"
  lv_label_set_text(ui_wifilabel, "WiFi verbonden!");

  // Verberg de spinner als de verbinding tot stand is gekomen
  lv_obj_add_flag(ui_spinner, LV_OBJ_FLAG_HIDDEN);  // Verberg de spinner

  // Wacht 5 seconden voordat je naar Screen2 gaat
  while (millis() - startTime < 5000) {
    lv_task_handler();  // Houd LVGL draaiende
    delay(100);         // Kleine vertraging om de CPU niet te overbelasten
  }

  // Schakel naar Screen2
  lv_scr_load(ui_Screen2);  // Laad Screen2 als het actieve scherm
}

void loop() {
  lv_task_handler();  // Houd LVGL draaiende
  delay(5);
}

This is the output in the serial monitor:

Devices Scan start.
I2C device found at address 0x15 !
I2C device found at address 0x40 !
Done

Probeer verbinding te maken met WiFi...
WiFi verbonden!

I don't think it is still in the ESP32; it is somewhere in your PC (and yes, I know that that is very broad).

Without the I2c device connected:

Probeer verbinding te maken met WiFi...
WiFi verbonden!

Explain? How can serial monitor show something that is on my pc and only if an I2C module is connected to the ESP32?

I really do not know.

You can do the following exercise to prove that the scanner sketch is still in the ESP32.

  1. Upload the scanner sketch.
  2. Open serial monitor if it was not open.
  3. Close Serial Monitor and upload your actual sketch. Do not open Serial Monitor again.
  4. Use a 3rd party terminal program (e.g. putty) and open the serial port.
  5. Do you have the same behaviour (showing the results of the scanner sketch first)?

If you don't get the same behaviour as with Serial Monitor (so only the results of your last loaded sketch), the scanner sketch was not in the memory of the ESP32 as you believe but the data is somewhere else.

A thing you can try to solve the problem (if you consider it a problem instead of an annoyance) is to add a delay after the Serial.begin() and see if that improves the situation. Start with e.g. 2000 ms and adjust up or down to needs.

Notes:

  1. I only replied to your opening post because you believe that the scanner sketch is still in the ESP32 and I don't believe that that is the case.
  2. I'm totally not familiar with ESP32 based boards; but there are (possibly) related issues with other boards (e.g. the Mega).

I did what you suggested ( 1..5). I used a terminal program (SerialTools) on my macbook and the result is the same as on the serial monitor. No effect after adding a delay in the setup().

For me it is not a big problem, because the program is running fine. I'm just curious about the cause.

Solved!

Found a I2C scan routine in the LilyGo_AMOLED library. I had not thought about searching in any of the used libraries because the output of that library is exactly the same as the output of my I2C scanner sketch (found on the internet).

#sterretje
Thanks for taking your time to help me.

1 Like