这段Arduino代码存在什么问题?串口监视器显示乱码
Let's break down the issues in your code and setup that are causing the garbage output, along with clear fixes:
1. Invalid Char Initialization
You have char c = "" ; which is incorrect. A char variable holds a single character, but "" is a C-string literal (an array of characters). Assigning this to a char leads to undefined behavior, which can corrupt your data.
Fix:
Declare c without the invalid initialization—you’ll overwrite it when reading data anyway:
char c; // No need for an initial value here
2. You’re Not Actually Reading Incoming Data
Your loop checks if data is available, adds a delay, but never reads the bytes from the HC-05 module. Unread bytes pile up in the buffer, leading to corrupted or garbage output when you eventually access them.
Fix:
Add code to read all available bytes and append them to your readdata string:
void loop() { if (BT.available() > 0) { while (BT.available() > 0) { // Read every byte in the buffer char c = BT.read(); readdata += c; } Serial.println(readdata); // Print the full received message readdata = ""; // Reset for the next incoming message } }
3. Using delay() for Data Reception is Unreliable
The delay(10) blocks your code, and if more data arrives during that delay, it might not be captured properly. Instead, read all available bytes in a loop until the buffer is empty—this is far more efficient and reliable.
Fix:
Replace the delay with the while loop shown above to capture all incoming data.
4. Critical Wiring Mismatch (Likely the Main Culprit!)
Your comment says SoftwareSerial BT(10, 11); //TX, RX respectively, but Arduino’s SoftwareSerial constructor uses (rxPin, txPin) as arguments. That means:
- Pin 10 = Arduino’s RX (connect to HC-05’s TX)
- Pin 11 = Arduino’s TX (connect to HC-05’s RX)
If you followed the comment and connected HC-05’s RX to pin 10 (thinking it’s Arduino’s TX), you’ve crossed the wires. This will definitely cause garbage data because the two devices can’t communicate correctly.
Fix:
Double-check your wiring:
- HC-05 TX → Arduino Pin 10 (SoftwareSerial RX)
- HC-05 RX → Arduino Pin 11 (SoftwareSerial TX)
- Use a voltage divider on HC-05’s RX pin if connecting to Arduino’s 5V pin (HC-05 uses 3.3V logic—5V can damage it).
5. Baud Rate Mismatch
Even though you set BT.begin(9600), the HC-05 module might be configured to a different baud rate (some default to 38400 or 115200). Mismatched baud rates always result in garbage data.
Fix:
- Verify the HC-05’s current baud rate using AT commands (connect it directly to Arduino’s hardware serial pins to send commands like
AT+UART?). - Ensure both the Arduino code and HC-05 module use the same baud rate (9600 is a safe default if you reset the HC-05 to factory settings).
6. Unstable Power Supply
HC-05 modules need a stable 3.3V power source. Powering it from Arduino’s 5V pin can cause unstable communication (or damage the module), and USB alone might not provide enough current if other components are connected.
Fix:
Use a dedicated 3.3V power supply for the HC-05, or a voltage regulator if needed.
Corrected Full Code
Here’s a revised version of your code that addresses all the main issues:
#include <SoftwareSerial.h> SoftwareSerial BT(10, 11); // RX (pin10), TX (pin11) String readdata; void setup() { BT.begin(9600); Serial.begin(9600); Serial.println("Waiting for command..."); } void loop() { if (BT.available() > 0) { // Read all incoming bytes until buffer is empty while (BT.available() > 0) { char c = BT.read(); readdata += c; } // Print received data to serial monitor Serial.print("Received: "); Serial.println(readdata); // Reset the string for next message readdata = ""; } }
Start with checking your wiring and baud rate settings—those are the most common causes of garbage data with HC-05 modules.
内容的提问来源于stack exchange,提问作者Soham Bhatnagar

