Universal Asynchronous Receiver Transmitter (UART)

Universal Asynchronous Receiver/Transmitter (UART) is a physical circuit in a microcontroller or a stand-alone IC. A UART’s main purpose is to transmit and receive serial data. We use it for minicom (A serial interface) to debug our boards, and it is often used to communicate with GPS modules and Bluetooth modules!

Serial VS Parallel?

image-20240825-014913.png

Serial reduces the number of wires significantly as shown in the picture. It also takes longer due to each data bit being sent subsequently. UART, I2C, SPI, and CAN are all serial interfaces. Many protocols follow serial because it is more resilient at longer distances and is cheaper.

Notice that serial communication sends the most significant bit (MSB) first and the least significant bit (LSB) last.

Hardware

image-20240825-015852.png

TX = Data transmitting pin. TX will always wire to another UARTs RX
RX = Data receiving pin RX will always wire to another UARTs TX
CTS = Clear to send (Optional Pin). Used for hardware flow control
RTS = Request to send (Optional Pin. Used for hardware flow control
GND = Ground. For signal integrity, both UARTs should have the same ground

Hardware Flow Control

This is similar to a handshake between two UARTs.

When UART1 wants to transmit data, it will assert the RTS line (Set it to an active state). UART2 will monitor their CTS pin, typically with an ISR.

When CTS is triggered, UART2 will verify that it is ready to receive data. If it is ready it will keep the RTS → CTS line asserted. If not ready it will release the connection by typically setting it high.

Although this requires 2 extra wires, it can help reduce the chances of data loss due to buffer overflow.

Baud Rate

Baud rate is used to represent the number of signal changes (i.e. symbols) across a transmission line per second.

A symbol is a basic unit of data in a communication system. It can represent a specific bit pattern or value.

In a simple UART system, a change in signal occurs when we transmit either a ‘1' or a '0’. So Baud Rate can be the same as the Bit Rate. A baud rate of 9600 means we can transmit 9600 bits per second or 1200 bytes. In more complex communication protocols there can be more bits in each symbol, thus the bit rate can be higher than the baud rate. The bit rate will never be less than the baud rate.

Bit rate >= Baud rate

Configuration

Full-Duplex VS Half-Duplex VS Simplex:

  • Full-Duplex: UART supports simultaneous two-way communication using separate TX and RX lines

  • Half-Duplex: UART can communicate in both directions, but not simultaneously

  • Simplex: UART can only communicate in one direction, TX or RX.

Baud Rate

  • Baud Rate: The number of symbol changes per second. Typically 9600 or 115200

Even Parity VS Odd Parity VS No Parity

  • Parity is an extra bit added to a message to ensure error detection. It makes the total number of '1's in the message either even or odd, depending on the chosen parity type:

    • Even Parity: If there is an even number of ‘1’s transmitted, the parity bit is 0. If there is an odd number of '1’s transmitted, the parity bit is 1.

    • Odd Parity: If there is an even number of ‘1’s transmitted, the parity bit is 0. If there is an odd number of '1’s transmitted, the parity bit is 1.

    • No Parity: There is no parity bit.

Packet Protocol

Standard UART Communication Steps:

  1. Start Bit:

    • A start bit is achieved by pulling the TX line low

  2. Data Bits [0:8]:

    • UART sends 1 byte at a time (8 bits). Implementation details are further specified in the data sheets.

  3. Parity [9]:

    • In the case there is a parity bit, it will be toggled depending on which type of parity.

      • In the even parity example, there are 4 HIGHs. There is already an even number of bits, so parity is 0

      • In the odd parity example, there are 4 HIGHs. There is not an odd number of bits, so parity is 1 to make it odd.

    • Data can continue to be extended if there are more bytes pending to be sent

  4. Stop Bit:

    • A stop bit is achieved by pulling the TX line high

Physical Layer: Transistor-Transistor Logic (TTL)

TTL will always remain between the limits of 0V and Vcc, which is often 5V or 3.3V(usually for historical reasons and nomenclature, TTL refers to 5v, LVTTL refers to 3.3v, but that’s besides the point here). Here is a basic circuit for UART through TTL:

Both TX and RX lines are pulled up to 5V, setting the logic level HIGH. The 2N3906 is a P channel MOSFET, meaning a negative gate-source voltage will toggle it.

  • TX = '1' (Logic High, 5V): V_GS is 0V (5V - 5V). In a P-channel MOSFET, a V_GS of 0V means the MOSFET is off, so the drain (output) remains at the source voltage, which is 5V.

  • TX = '0' (Logic Low, 0V): V_GS is -5V (0V - 5V). In a P-channel MOSFET, a negative V_GS will turn the MOSFET on, allowing current to flow from the source to the drain. This results in the drain voltage dropping close to 0V.

Put simply:

  • When TX is HIGH (5V), the MOSFET remains off, and the output remains HIGH (5V).

  • When TX is LOW (0V), the MOSFET turns on, and the output drops close to LOW (0V).

Physical Layer: RS-232 RS-422 RS-485

RS-232

  • Single-ended Signals: Uses a single-ended signal system where all signals are referenced to a common ground

  • Full-Duplex: Supports simultaneous two-way communication.

  • Short Distance: Typically used for short-distance communication, often up to 15 meters (50 feet)

  • Voltage Levels: Typically uses voltage levels ranging from -12V to +12V

RS-422

  • Differential Signals: Two wires for each signal (TXD- and TXD+ for example) to reduce noise and increase data integrity (Both wires are impacted by EMI the same, cancelling effects out)

  • Half-Duplex: Supports communication in one direction at a time

  • Longer Distance: Suitable for longer distances, typically up to 1200 meters (4000 feet)

  • Voltage Levels: Differential voltage levels are used, typically between -6V to +6V

RS-485

  • Differential Signals: Similar to RS-422, with differential in two lines (A and B) to improve common mode noise rejection

  • Multi-Point Communication: Allows up to 32 devices to communicate on the same bus

  • Half-Duplex and Full-Duplex: Can be used in half-duplex mode (one direction at a time) or full-duplex mode (simultaneous two-way communication with additional lines)

  • Long Distance: Suitable for very long distances, typically up to 1200 meters (4000 feet)

 

Basics of UART Communication

Universal asynchronous receiver-transmitter

https://onlinedocs.microchip.com/pr/GUID-167CA20A-2C0F-4CBC-A693-9FD032B9B193-en-US-1/index.html?GUID-C8B83E54-0F62-4205-98DD-B1560AACDBB4

Light indicator for TTL-level UART serial interface with a SINGLE LED?

https://www.arrow.com/en/research-and-events/articles/what-is-baud-rate

RS-232 vs RS-422 vs RS-485, What is the difference?