Talking to your computer: UART
The serial port, the one tool you will use on every project from now on.
Read this first
The UART is the simplest serial port and the first thing to bring up on any board. It gives you printf: your program prints text and a terminal on your computer shows it. From now on every project you build should print what it is doing.
A UART link is two wires plus ground: TX carries data out, RX carries data in. Your TX goes to their RX and their TX to your RX, the classic beginner crossing mistake. Because each direction has its own wire, both sides can send at the same time, which 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 wrapped 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 8 data bits, then a stop bit that returns the line high. With no parity bit this 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. That is not RS-232. A real RS-232 port, the old 9-pin PC connector, swings between plus and minus 5 to 15 V, often plus and minus 12 V, with inverted signalling, and wiring it straight to an MCU pin can destroy the pin. Use a USB to serial adapter, which speaks logic level, or the USB port already on your board.
To remember
- TX to RX, RX to TX, and share ground.
- Both sides agree on the baud rate. There is no clock wire.
- A frame is start bit, 8 data bits, stop bit: 8N1, ten bits per byte.
- MCU pins are logic level. Never wire a true RS-232 port to them directly.
Check what you read
3 questions from the question bank on the ideas above. Each one comes with an explanation after you answer.
Sign in to answer the questions. Your answers count towards your level.
Sign inThe lesson text and the project are free to read without an account.
Build this
Serial monitor and a command line
You need
- Your board and its USB cable, plus the button and LED from earlier lessons.
- A serial terminal: the Arduino IDE monitor, PuTTY, or screen on Linux and macOS.
Steps
- Print a line with a counter once a second at 115200 baud. Open the terminal and confirm you see it. Change the baud rate on one side only and look at the garbage that appears, then fix it. If your board talks over native USB (Pico, ESP32-S3) the baud rate is ignored on that link and nothing changes; try it later with a USB to serial adapter on a real UART pin.
- Print the button state every time it changes, using your debounced reading.
- Read what the terminal sends. Accept the commands on, off and status, end each with Enter, and act on the LED. Reply with a clear message for an unknown command.
Done when
Typing on and off in the terminal controls the LED, and status prints the button state and the uptime.