UART Deep Dive
Hardware and software serial, GPS modules, and RS-232
Skills taught: embedded.uart
UART: The Universal Asynchronous Receiver-Transmitter
UART is the foundation of serial communication — simple, robust, and ubiquitous. Unlike I2C or SPI, it needs no clock wire: devices agree on a speed (baud rate) in advance and each generates its own clock. The only wires needed are TX (transmit), RX (receive), and a shared GND. Always cross-connect: TX of device A → RX of device B.
UART Frame Format (8N1 — most common)
───────────────────────────────────────
Idle │Start│ D0 │ D1 │ D2 │ D3 │ D4 │ D5 │ D6 │ D7 │Stop│ Idle
HIGH │ LOW │ b0 │ b1 │ b2 │ b3 │ b4 │ b5 │ b6 │ b7 │HIGH│ HIGH
8N1 = 8 data bits, No parity, 1 stop bit
Baud 9600 = 1 bit every 104 µs → 1 byte = 10 bits = 1.04 ms
Baud 115200 = 1 bit every 8.7 µs → 1 byte = 86.8 µs
Hardware vs Software UART
Hardware UART vs SoftwareSerial
- Hardware UART: Dedicated USART peripheral in the MCU
- SoftwareSerial: Bit-bangs UART timing on any GPIO pins
GPS Module: Reading NMEA Sentences
GPS modules like the Neo-6M and Neo-M8N output NMEA sentences at 9600 baud over UART. Each line starts with a sentence identifier ($GPGGA, $GPRMC) containing latitude, longitude, altitude, and time. The TinyGPS++ library parses these automatically.
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
// Library Manager: search "TinyGPS++" by Mikal Hart
// Neo-6M: TX → Arduino pin 4, RX → Arduino pin 3
SoftwareSerial gpsSerial(4, 3); // (RX, TX)
TinyGPSPlus gps;
void setup() {
Serial.begin(115200);
gpsSerial.begin(9600); // Neo-6M default baud
Serial.println(F("GPS ready — waiting for fix..."));
}
void loop() {
// Feed all available GPS bytes to the parser
while (gpsSerial.available()) {
gps.encode(gpsSerial.read());
}
// Print position when it updates
if (gps.location.isUpdated()) {
Serial.print(F("Lat: ")); Serial.print(gps.location.lat(), 6);
Serial.print(F(" Lng: ")); Serial.print(gps.location.lng(), 6);
Serial.print(F(" Alt: ")); Serial.print(gps.altitude.meters(), 1); Serial.print(F("m"));
Serial.print(F(" Sats: ")); Serial.print(gps.satellites.value());
Serial.print(F(" Speed: ")); Serial.print(gps.speed.kmph(), 1); Serial.println(F(" km/h"));
}
// Check if data is arriving
if (millis() > 10000 && gps.charsProcessed() < 10) {
Serial.println(F("Error: no GPS data! Check wiring."));
}
}
Tip: GPS modules need an unobstructed view of the sky to get a fix. Place the antenna near a window or outdoors. The first fix (cold start) can take 30–60 seconds. Once fixed, subsequent locks are faster (warm start ~5 s).
UART Between Two Arduinos
Two Arduinos can communicate directly over UART by connecting TX of one to RX of the other and sharing GND. This is useful for splitting a project across two boards.
// Arduino SENDER: transmits sensor data every 500ms
// Wiring: Sender TX (pin 1) → Receiver RX (pin 0)
// IMPORTANT: disconnect TX pin when uploading new firmware!
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorVal = analogRead(A0);
float voltage = sensorVal * 5.0 / 1023.0;
// Send as: VAL:value\n
Serial.print(F("VAL:"));
Serial.println(voltage, 2);
delay(500);
}
// Arduino RECEIVER: reads sensor data from sender
#include <SoftwareSerial.h>
SoftwareSerial senderSerial(10, 11); // RX=10 (← sender TX), TX=11
void setup() {
Serial.begin(115200); // USB debug
senderSerial.begin(9600); // must match sender
}
void loop() {
if (senderSerial.available()) {
String line = senderSerial.readStringUntil('\n');
line.trim();
if (line.startsWith("VAL:")) {
float voltage = line.substring(4).toFloat();
Serial.print(F("Received voltage: "));
Serial.println(voltage, 2);
}
}
}
Warning: When connecting a 5V Arduino UART to a 3.3V device (ESP32, Raspberry Pi), the 5V TX signal from Arduino will damage the 3.3V device's RX pin. Use a voltage divider (1 kΩ + 2 kΩ) or a logic-level converter on the TX→RX line. The 3.3V TX → 5V RX direction is usually fine (3.3V exceeds the HIGH threshold on most 5V AVR inputs).
Frequently Asked Questions
What is RS-232?
RS-232 is an older serial standard using ±12V logic levels (±12V for HIGH/LOW vs 0/5V TTL). It uses a DB9 or DB25 connector, common on older industrial equipment. A MAX232 chip converts between RS-232 and TTL levels. Modern microcontrollers use TTL UART; RS-232 is mainly for interfacing with legacy equipment.
How do I read a full line from UART without blocking?
Build a character-by-character buffer: append each received character to a String or char[], and when you receive '\n' (newline), process the complete line. Avoid readStringUntil('\n') in time-sensitive code as it blocks until the delimiter arrives or timeout.
My GPS shows no fix after 10 minutes. What's wrong?
Common causes: (1) indoors or obstructed sky — move near a window or outside; (2) no antenna connected; (3) cold start after long storage — may take up to 15 minutes; (4) UART wiring error — check TX→RX cross-connection. Use Serial.print(gps.charsProcessed()) to verify data is arriving.