Sub topic 4
Voltage levels and wiring: TX, RX and ground
This chapter assumes you know how a UART frame is built (start bit, data bits, stop bit) and what baud rate means. It also assumes you can read a basic Arduino sketch. The goal here is physical: what voltages represent a 0 and a 1, how to connect two devices so that bytes arrive, and what to do when the devices use different electrical standards.
Two wires and a reference
A basic UART link between two devices uses three conductors:
- TX (transmit): the pin a device drives to send data.
- RX (receive): the pin a device listens on.
- GND (ground): the shared voltage reference.
TX and RX are named from the point of view of the device that owns the pin. Device A's TX is therefore not connected to device B's TX. Each TX goes to the other side's RX, so the lines are crossed:
Each data line is driven by one device and read by the other, so UART needs no addressing. The link is full duplex: both directions can carry data at the same moment.
Why ground is mandatory
A voltage is always measured between two points. A receiver decides "high" or "low" by comparing the RX pin against its own ground. If the two devices have no common ground, the receiver's idea of 0 V is unrelated to the sender's. The result is random or constant garbage, even though the data wire is connected.
If both devices are powered from the same USB port or the same supply, their grounds are often already tied together. Two separately powered boards need an explicit ground wire between them.
Logic levels on a TTL-style UART
Microcontroller UARTs use TTL-level (or "logic-level") signaling. The name is historical. In practice it means the line swings between 0 V and the chip's supply voltage:
| Device supply | Logic 1 (HIGH) | Logic 0 (LOW) |
|---|---|---|
| 5 V (Arduino Uno, Nano, Mega) | about 5 V | about 0 V |
| 3.3 V (many ESP, ARM and sensor boards) | about 3.3 V | about 0 V |
Two points matter for UART:
- The idle line is HIGH. When nothing is being sent, TX sits at the logic-1 voltage. The start bit is a LOW pulse, which is why a receiver can detect the beginning of a frame as a falling edge.
- A data bit of 1 is HIGH and a data bit of 0 is LOW. The signal is not inverted at TTL level. The line is simply the bits in order, with the least significant bit first.
A byte of 0x55 (binary 01010101) is a useful test value. Sent least significant bit first with 8N1 framing, it produces a start bit, then alternating bits, then a stop bit, so the line toggles at every bit time. You will see this again when troubleshooting with a scope or logic analyzer.
Receiver thresholds
A receiver does not need exactly 5 V or 0 V. It has an input-high threshold (VIH) and an input-low threshold (VIL). For an ATmega328P (Uno) powered at 5 V, the datasheet gives VIH as roughly 0.6 × VCC, which is 3.0 V, and VIL as roughly 0.3 × VCC, which is 1.5 V. Voltages between those values are undefined, and the receiver may read them either way.
Mixing 5 V and 3.3 V devices
Connecting devices with different logic levels is the most common wiring mistake on UART links. The two directions behave differently.
3.3 V transmitter into a 5 V receiver
A 3.3 V output is close to the 3.0 V VIH of a 5 V ATmega328P. The link often works, but the margin is thin, and noise or a long wire can cause errors. Treat it as marginal. A level shifter is the safe choice.
5 V transmitter into a 3.3 V receiver
This is the dangerous case. Most 3.3 V chips are not rated for inputs much above their own supply voltage, often VCC + 0.3 V. A 5 V signal can overstress the input protection diodes and damage the chip, sometimes only after it has worked for a while. Always reduce the voltage first.
You have two common options:
- A resistor divider on the one signal that goes from 5 V to 3.3 V. It is cheap and fine at typical UART speeds.
- A level-shifter module (a bidirectional MOSFET shifter or a dedicated translator chip). It handles both directions and is better for faster links.
For a divider, put R1 in series from the 5 V TX pin to the 3.3 V RX pin. Put R2 from that RX pin to ground. The output is:
Vout = Vin × R2 / (R1 + R2)
With R1 = 1 kΩ and R2 = 2 kΩ, a 5 V signal becomes 5 × 2 / 3 ≈ 3.33 V. The 3.3 V device's TX can go straight to the 5 V device's RX in most cases, subject to the marginal threshold noted above.
The Arduino's own UART pins
On an Arduino Uno, the hardware UART appears on digital pins 0 (RX) and 1 (TX). These pins are also wired, through resistors, to the USB-to-serial chip that handles the USB connection to your computer. That has two practical effects:
- While the computer is using
Serialover USB, anything else driving pins 0 or 1 competes with the USB chip. Uploads may fail, and data may be corrupted. - If you need another UART device, you should move it to a different port (on boards that have more than one) or use SoftwareSerial on other pins. Both are covered in a later chapter.
On boards such as the Mega, Serial uses pins 0 and 1 for USB, and Serial1, Serial2 and Serial3 have their own separate pins. The device is then free to use those instead.
A loopback test
The quickest way to check a UART port and its wiring is a loopback: connect the port's own TX to its own RX with one jumper wire, send a byte, and see whether it comes back. This sketch assumes a board with a second hardware port, such as an Arduino Mega, with a jumper from pin 18 (TX1) to pin 19 (RX1):
void setup() {
Serial.begin(115200); // USB connection to the computer
Serial1.begin(9600); // port under test (pins 18 and 19 on a Mega)
}
void loop() {
// Discard anything left over from earlier
while (Serial1.available() > 0) {
Serial1.read();
}
Serial1.write('U'); // 0x55, alternating bits
delay(10); // one byte at 9600 baud takes about 1 ms
if (Serial1.available() > 0) {
int b = Serial1.read();
if (b == 'U') {
Serial.println(F("loopback OK"));
} else {
Serial.println(F("wrong byte received"));
}
} else {
Serial.println(F("nothing received"));
}
delay(1000);
}Open the Serial Monitor at 115200 baud. With the jumper in place, you should see "loopback OK" once per second. Remove the jumper and you should see "nothing received". This tells you the port works and separates a faulty port from a faulty device or cable.
Other physical standards
TTL-level UART is meant for short connections on a circuit board or between nearby boards. For longer cables and noisy environments, the same UART frames are carried over different electrical layers. The frame format does not change. Only the voltages and drivers do.
| Property | TTL / CMOS UART | RS-232 | RS-485 |
|---|---|---|---|
| Signal | Single-ended, 0 V to VCC | Single-ended, about ±3 V to ±15 V | Differential pair (A and B) |
| Logic 1 (idle) | HIGH (VCC) | Negative voltage | A less than B (by 200 mV or more at the receiver) |
| Logic 0 | LOW (0 V) | Positive voltage | A greater than B |
| Typical range | Under 1 m | Up to about 15 m | Up to about 1200 m |
| Topology | Point to point | Point to point | Multi-drop bus |
| Duplex | Full | Full | Usually half (two-wire) |
RS-232
RS-232 is the classic PC serial port standard. Two details cause trouble:
- Voltages are bipolar. A logic 1 (the idle "mark" state) is a negative voltage, typically −5 V to −15 V. A logic 0 (the "space" state) is positive. A receiver accepts −3 V to −15 V as 1 and +3 V to +15 V as 0.
- The logic is inverted compared with TTL. An idle TTL line is HIGH, and an idle RS-232 line is negative.
Never connect an RS-232 port directly to an Arduino pin. The voltage swing can destroy the input. Use a level converter such as a MAX232-type chip or module, which generates the ± voltages from a single supply and also inverts the signal.
On a 9-pin PC connector, pin 2 is RX, pin 3 is TX and pin 5 is ground, as seen from the PC (the DTE side). Connecting two DTE devices therefore needs a null-modem cable, which crosses pins 2 and 3. A straight-through cable connects a DTE to a modem-style (DCE) device.
RS-485
RS-485 sends each bit as a voltage difference between two wires, A and B. Noise tends to affect both wires equally, so it cancels out in the difference. This is why RS-485 works over long cables in electrically noisy places such as factories.
Points to know for a typical Arduino setup:
- You connect a transceiver chip (for example a MAX485-type module) between the UART pins and the bus.
- The common two-wire form is half duplex: only one device may transmit at a time. The transceiver has driver-enable (DE) and receiver-enable (RE) pins, often tied together and controlled by one Arduino pin.
- A bus needs a 120 Ω termination resistor across A and B at each physical end of the cable, to stop signal reflections.
- Higher-level rules (who may talk and when) are left to the software protocol, for example Modbus RTU.
The sketch below shows the direction control. It sends one byte, waits until the UART has finished shifting it out, and then goes back to receive mode:
const byte DE_RE_PIN = 2; // DE and RE tied together on the transceiver
void setup() {
pinMode(DE_RE_PIN, OUTPUT);
digitalWrite(DE_RE_PIN, LOW); // start in receive mode
Serial.begin(9600);
}
void sendByte(byte value) {
digitalWrite(DE_RE_PIN, HIGH); // enable the driver
Serial.write(value);
Serial.flush(); // wait until the last bit has left the UART
digitalWrite(DE_RE_PIN, LOW); // release the bus
}
void loop() {
sendByte('A');
delay(1000);
}Calling Serial.flush() before releasing the bus matters. write() only places the byte in the transmit buffer. If you switch to receive mode immediately, the end of the frame is cut off.
Cabling checklist
Before you debug software, check the physical link in this order:
- Do both devices share a ground wire?
- Is each TX connected to the other side's RX, not TX to TX?
- Do both sides use the same logic level, or is there a divider or shifter in the path?
- Is the cable short enough for the baud rate and the standard in use?
- Is anything else, such as a USB connection, driving the same pins?
Common errors
- Garbage characters: often a baud mismatch, but also a missing ground or a marginal voltage level.
- Nothing received: TX and RX not crossed, no common ground, or the sender idles at the wrong level.
- Works for a while, then the 3.3 V chip fails: a 5 V signal applied to a 3.3 V input.
- Uploads fail when a device is attached: the device is loading pins 0 and 1 on an Uno. Disconnect it during the upload.
Exercise
You connect an Arduino Uno (5 V) TX pin to the RX pin of a 3.3 V sensor, and the sensor's TX pin to the Uno's RX pin. The ground wire is connected. The sensor works for a short time and then stops responding. What is wrong, and how would you fix the Uno-to-sensor direction using a resistor divider with R1 = 1 kΩ?
Show answer
The 5 V output from the Uno exceeds the sensor's input rating (usually about VCC + 0.3 V), which can damage the sensor's RX input. Add a divider on that wire only: 1 kΩ in series from the Uno TX to the sensor RX, and a 2 kΩ resistor from the sensor RX to ground. The sensor sees 5 × 2 / (1 + 2) ≈ 3.3 V. The sensor-to-Uno direction can stay direct, because 3.3 V is above the Uno's roughly 3.0 V VIH, though the margin is small.
Exercise
A PC with a 9-pin RS-232 port is connected to another PC's 9-pin port with a straight-through cable, and no data arrives in either direction. Both are set to the same baud rate and frame format. Why, and what is the fix?
Show answer
Both machines are DTE devices, so each transmits on pin 3 and receives on pin 2. A straight-through cable connects TX to TX and RX to RX. Use a null-modem cable (or adapter), which crosses pins 2 and 3 and keeps ground on pin 5.
Exercise
In the RS-485 sketch, what happens if you remove the call to Serial.flush() and why?
Show answer
Serial.write() returns as soon as the byte is queued in the transmit buffer, not when it has been sent. Without flush(), the sketch sets DE/RE LOW while the UART is still shifting the frame out, so the transceiver stops driving the bus and the receiving device gets a truncated or missing byte.