0xdeadbeef
0xdeadbeef

Reputation: 591

Why does my Arduino IDE Serial Monitor output weird characters sometimes?

I’m working on an ESP32 project using the Arduino IDE, and occasionally, the Serial Monitor displays garbled characters instead of the expected output. The baud rate in the Serial Monitor is set to 115200, and both NL and CR are enabled. Sometimes it works perfectly, but other times it doesn’t. Here's my code:

#include <BluetoothSerial.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` and enable it.
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial esp32_bt;

const int QNA_ITEM_LEN = 2;
String questions[QNA_ITEM_LEN];
String answers[QNA_ITEM_LEN];

void setup() {
  questions[0] = String("q1?");
  questions[1] = String("q2?");

  answers[0] = String("a1");
  answers[1] = String("a2");

  Serial.begin(115200);
  esp32_bt.begin("esp32-bluetooth");
}

void loop() {
  if (Serial.available()) {
    Serial.println("Sending Data...");

    String msg = Serial.readString();
    esp32_bt.println(msg);

    Serial.println("Data Sent.");
  } else if (esp32_bt.available()) {
    String receivedText = esp32_bt.readString();
    receivedText.trim();

    Serial.print("Received Text: (");
    Serial.print(receivedText);
    Serial.println(")");

    if (receivedText.indexOf("?") > 0) {
      for (int i = 0; i < QNA_ITEM_LEN; i++) {
        if (receivedText.equalsIgnoreCase(questions[i])) {
          Serial.print("Answer: ");
          Serial.println(answers[i]);
          break;
        } else {
          Serial.print("receivedText: ");
          Serial.print(receivedText);

          Serial.print(" == questions[i]: ");
          Serial.println(questions[i]);
        }
      }

      Serial.println("Answer: I don't know");
    }
  }

  delay(50);
}

When it’s not working, this is what I see in the Serial Monitor:

serial-monitor-output


Temporary Fixes I’ve Tried:

  1. Closing and reopening the Arduino IDE.
  2. Reconnecting the ESP32 by unplugging and plugging the USB cable back in.
  3. Recompiling and uploading the code again.

Sometimes, these steps fix the issue right away. Other times, I have to repeat them in combination, and eventually, the Serial Monitor starts working again.


Environment Details:


My Questions:

  1. What could be causing this issue where the Serial Monitor outputs garbled characters sporadically?
  2. Are there better troubleshooting steps or long-term fixes to prevent this from happening?
  3. Could there be an issue in my code that’s contributing to this behavior?

[EDIT]

Idk if what I did is right, here I used minicom to monitor the port this is the output.

0043c390: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c3a0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c3b0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c3c0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c3d0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c3e0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c3f0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c400: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c410: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c420: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c430: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c440: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c450: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c460: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c470: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c480: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c490: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c4a0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c4b0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c4c0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c4d0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c4e0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c4f0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c500: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c510: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c520: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c530: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c540: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c550: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c560: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c570: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c580: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c590: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c5a0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c5b0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c5c0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c5d0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c5e0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c5f0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c600: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c610: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c620: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c630: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c640: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c650: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c660: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c670: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c680: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c690: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c6a0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c6b0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c6c0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c6d0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c6e0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c6f0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c700: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c710: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c720: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c730: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c740: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c750: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c760: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c770: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c780: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c790: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c7a0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c7b0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c7c0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c7d0: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c7e0: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c7f0: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c800: 8000 0080 0000 8000 0080 0000 8000 0080  ................
0043c810: 0000 8000 0080 0000 8000 0080 0000 8000  ................
0043c820: 0080 0000 8000 0080 0000 8000 0080 0000  ................
0043c830: 8000 0080 0041 7241 7241 7241 7269 6e6f  .....ArArArArino
0043c840: 3a20 6173 6461 0d0a 0d0a 0d0a 0d0a 4172  : asda........Ar
0043c850: 6464 6173 0a0d 0a41 7264 7569 6e6f 0a0d  ddas...Arduino..
0043c860: 0a72 6475 696e 6f3a 2074 6573 7473 650d  .rduino: testse.

The garbled output starts with repeated values, but towards the end, when I typed something in the Serial Monitor of the arduino, the output changed on the last lines.

Upvotes: 1

Views: 86

Answers (0)

Related Questions