Skip to content
You Start with one site
Amounts in Start with one site
Microcontrollers · Concept

UART serial

One idea Beginner 5 min read
Found this useful?

Two people can pass a message by holding a lamp on or off for one second at a time, if both count the seconds at the same speed. UART serial is this agreement between two chips: one wire carries the bits one after another, at a speed that both sides were set to beforehand.

Start bitThe line falls from 1 to 0. The receiver starts its timing here.Always 0
Eight data bitsThe character, with the least significant bit first.One byte
Stop bitThe line returns to 1 and rests before the next character.Always 1

See it

Predict first

A sensor sends text at 9,600 baud. The computer that listens is set to 19,200 baud by mistake. What appears on the screen?

See it: one character on the wire, and where the receiver samples it

Sent
Stop bit as received
Received

The line is the sender at 9,600 baud. The first bit after the start bit is the least significant bit of the character. Each dot is a moment when the receiver samples the line, counted on its own clock from the start bit. When the two clocks agree, every dot sits in the middle of a bit. When they do not, the dots drift, and a dot that lands in the wrong bit is marked with an exclamation mark.

The idea

A UART link has one wire for each direction, called TX (transmit) and RX (receive), and a shared ground. While nothing is sent the line rests at 1.

Each character travels as a frame of 10 bits: a start bit that is always 0, eight data bits with the least significant bit first, and a stop bit that is always 1. This format is written 8N1. The baud rate is the number of bits sent each second.

time for one bit = 1 ÷ baud rate

at 9,600 baud one bit lasts 104 µs and a frame of 10 bits lasts 1.04 ms

The receiver starts its own timing when the line falls for the start bit, then samples the middle of each bit. It works only if both sides count at nearly the same speed. In this simple picture the clocks may differ by about 5%. Real links need a margin: keep the error under 2%.

One worked number

The question

A device sends the letter A, which is code 65, at 9,600 baud in the 8N1 format. What levels appear on the wire, in order, and how long does the frame last?

Show the worked solution, 3 steps
  1. Write the code in binary

    65 = 64 + 1, so the eight bits are 0100 0001.

  2. Put the bits in the order of the wire

    The least significant bit goes first, so read the byte from right to left: 1 0 0 0 0 0 1 0. Add the start bit before and the stop bit after: 0, then 1 0 0 0 0 0 1 0, then 1.

  3. Find the time

    One bit lasts 1 ÷ 9,600 s = 104 µs. Ten bits last 1.04 ms. The figure opens on this frame.

The mistake to avoid

Wrong characters do not mean a damaged cable. A cable that is broken or connected to the wrong pin gives nothing at all. A steady flow of wrong characters means the bits arrive but are read at the wrong moments: the two sides are set to different baud rates, or to different frame formats. Check the settings on both sides before you touch the wiring.

A second trap: TX does not connect to TX. What one side transmits the other must receive, so TX goes to RX in each direction, and the two grounds must be joined.

Check yourself

Question 1

A link runs at 115,200 baud in the 8N1 format. How many bytes of data can it carry each second?

Question 2

A GPS receiver is connected to a logger. The logger shows a steady flow of characters, but they are all wrong. What do you check first?

Question 3

The letter C is code 67, which is 0100 0011 in binary. Which two data bits follow the start bit on the wire?

Where you meet it

  • Service portsThe port a technician uses to read the messages of a controller, a router or a drive is nearly always a UART.
  • GPS and modemsA GPS receiver and a mobile network modem both pass their data to the main chip as text on a UART.
  • Energy meters and drivesThe meters on a plant network send UART frames on a pair of wires made for long cables.
Key points
  • A frame is a start bit (0), eight data bits with the least significant first, and a stop bit (1).
  • Time for one bit = 1 ÷ baud rate. Ten bits carry one byte.
  • There is no clock wire. Both sides must be set to the same baud rate and format.
  • Wrong characters point to the settings. No characters point to the wiring: TX goes to RX.
Practice

Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.