1

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:

  • Operating System: Ubuntu 22.04
  • Arduino IDE Version: 2.3.4 (Standalone)

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.

9
  • 2
    The behavior shown is outputting an ascii character greater than 0x7f to a Unicode console. For me a better solution is to output a hex dump of the bits instead of treating them as char. Commented Jan 17 at 9:32
  • Would hex dump likely to add value here? Because if not I’ll rather focus on debugging the actual problem first. Commented Jan 17 at 10:00
  • Yes, a hex dump of the data would add value, e.g. if each of the invalid chars has the a bit pattern in groups of 2 your data rate is off by 2 11000011 vs 1001, if it's all 1s or 0s for some time it's earth bounce, if it's inverted your terminal settings need updating, etc. Commented Jan 17 at 10:13
  • okay then how would one produce a hex dump from the arduino's serial monitor? Commented Jan 17 at 10:25
  • @PeteKirkham could you check my edit if what I did was right? Commented Jan 17 at 11:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.