Sub topic 11
Troubleshooting a UART Link
Assume you know the Arduino sketch structure, bytes and binary numbers, digital HIGH and LOW levels, and basic C or C++ syntax. This chapter uses UART, or Universal Asynchronous Receiver-Transmitter, to mean the byte stream behind Arduino serial communication.
A UART link can fail even when both programs compile. The usual causes are incorrect wiring, different voltage levels, mismatched communication settings, buffer overflow, or an application that reads the stream incorrectly. Test the link from the physical layer upward.
Start with the physical connection
For two devices, connect the lines this way:
| Device A | Device B | Purpose |
|---|---|---|
| TX | RX | A sends to B |
| RX | TX | A receives from B |
| GND | GND | Shared voltage reference |
TX means transmit. RX means receive. The lines must cross because each receiver listens to the other device's transmitter.
Disconnect power before changing wires. Confirm the board's pin labels and voltage limits. A 5 V Arduino output can damage a 3.3 V-only input. A USB-to-serial adapter may also use 3.3 V or 5 V logic. Do not confuse UART logic levels with RS-232. RS-232 uses different voltage ranges and usually needs a level converter.
Check that the device actually transmits. An oscilloscope or logic analyzer can show activity on TX. A multimeter usually cannot show the changing bit pattern reliably.
Check the UART settings
Both ends must agree on the baud rate and frame format. Baud rate is the number of signal symbols per second. In ordinary UART use, one symbol carries one bit, so 9600 baud is approximately 9600 bits per second.
The common format is 8 data bits, no parity, and 1 stop bit, written as 8N1. A byte then uses:
- One start bit
- Eight data bits
- No parity bit
- One stop bit
That is 10 bits for each 8-bit byte. At 9600 baud, one frame takes about 1.04 milliseconds, and the theoretical payload rate is about 960 bytes per second before software overhead.
Both devices must use the same baud rate, data-bit count, parity, and stop-bit count. For example, an Arduino can select an explicit format like this:
void setup() {
Serial.begin(115200, SERIAL_8E1);
}
void loop() {
}If the other device expects 115200 8N1, this configuration will fail because it adds even parity. Garbled text often indicates a baud mismatch, but it can also indicate a wrong frame format or voltage level.
Use a minimal loopback test
First test the Arduino without the other device. Connect its TX pin to its RX pin, then upload this sketch:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int b = Serial.read();
Serial.write(b);
}
}Send characters from a serial terminal. Each character should return. If it does not, check the selected serial port, terminal settings, board pins, and USB cable. Remove the loopback wire before connecting another transmitter. Two active transmitters on one line can fight each other.
Next, test each direction separately. Send a repeated known pattern such as ABC123 from one device. If A receives correctly but B does not, the fault is probably limited to one TX path, one RX path, or one program.
Find lost bytes
UART hardware commonly stores received bytes in a small receive buffer. The interrupt-driven UART can place bytes in that buffer while the main program performs other work. If the program does not remove bytes quickly enough, the buffer overruns and older or newer data is lost, depending on the platform.
Avoid long delay calls and blocking loops while receiving. Read all currently available bytes before doing slow work:
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n') {
Serial.println(F("line complete"));
}
}
// Keep other work short or schedule it without delay().
}A sender can also outpace the receiver. Lower the baud rate, send fewer bytes, or add flow control. Hardware flow control uses extra signals such as RTS and CTS. A simpler protocol can send an acknowledgement after each command.
Separate transport from message errors
UART provides a stream of bytes. It does not preserve messages. If a command such as SET 25 is split across multiple reads, that is normal. Your program must identify where a message starts and ends.
For newline-terminated text, ignore carriage returns if terminals send both carriage return and newline. Reject lines that exceed the buffer size. For binary data, use a start marker, a length, and a checksum. If a message has the correct length but a wrong checksum, suspect noise, voltage problems, or a damaged cable. If the length is wrong, suspect framing or parsing errors.
A repeatable checklist
- Confirm the selected pins and cross TX with RX.
- Confirm a common ground and compatible voltage levels.
- Match baud rate and frame format.
- Test the Arduino with loopback.
- Send a known pattern in one direction.
- Watch for buffer overrun caused by slow code.
- Add message boundaries and error detection.
- Change one variable at a time and record the result.
Exercise
Two boards use 9600 baud, 8N1. Board A transmits correctly in loopback, but Board B receives random characters. What should you check first?
Show answer
Check that A TX connects to B RX, both boards share GND, and their voltage levels are compatible. Then verify B's actual baud and frame settings.