Skip to content

Serial output with UART

The serial port: a tool you will use on every project from now on.

Explanation

The UART is the simplest serial port and the first peripheral to set up on any board. It gives you printf: your program prints text, and a terminal on your computer displays it. From now on, every project you build should print what it is doing.

A UART link uses 2 wires plus ground: TX sends data and RX receives it. Connect your TX to the other side's RX, and their TX to your RX. Connecting TX to TX is a common beginner mistake. Because each direction has its own wire, both sides can send at the same time. This is called full duplex.

There is no clock wire. Both sides agree on a speed in advance, the baud rate, such as 115200 bits per second. Each byte is sent in a frame. The line rests high. A start bit pulls it low for one bit time, so the receiver knows a byte is coming. Then come 8 data bits and a stop bit that returns the line high. With no parity bit, this format is written 8N1. At 9600 baud each of the 10 bits takes about 104 microseconds, so a byte takes about 1 ms.

The signal on the MCU pins is at logic level, 3.3 V or 5 V. This is not RS-232. A true RS-232 port, the older 9-pin PC connector, swings between plus and minus 5 to 15 V, often plus and minus 12 V, with inverted signalling. Wiring it directly to an MCU pin can destroy the pin. Use a USB to serial adapter, which works at logic level, or the USB port already on your board.

Key points

  • TX to RX, RX to TX, and a shared ground.
  • Both sides agree on the baud rate. There is no clock wire.
  • A frame is a start bit, 8 data bits and a stop bit: 8N1, 10 bits per byte.
  • MCU pins are logic level. Never wire a true RS-232 port to them directly.

Check your understanding

3 questions on this lesson. An explanation follows each answer.

Sign in to answer the questions. Your answers count towards your level.

Sign in

You can read the lesson and the project without an account.

Project

Serial monitor and a command line

What you need

  • Your board and its USB cable, plus the button and LED from earlier lessons.
  • A serial terminal: the Arduino IDE serial monitor, PuTTY, or screen on Linux and macOS.

Steps

  1. 1Print a line with a counter once a second at 115200 baud. Open the terminal and confirm that the line appears. Change the baud rate on one side only and observe the unreadable output, then correct it. If your board communicates over native USB (Pico, ESP32-S3), the baud rate is ignored on that link and nothing changes; repeat this step later with a USB to serial adapter on a real UART pin.
  2. 2Print the button state each time it changes, using your debounced reading.
  3. 3Read the text the terminal sends. Accept the commands on, off and status, each followed by Enter, and carry out each command. Reply with a clear message to an unknown command.

Done when

Typing on and off in the terminal controls the LED, and status prints the button state and the uptime.