How to use “real” UART

I recently went into a deep dive on “UART” and will publish a much longer article on the topic. This is just a recap of the basics to help put things in context. Many tutorials focus on using UART over USB, which adds many layers of abstraction, hiding what it actually is. Here, I deliberately use an external adapter to make things more real.

Nowadays, you typically use UART to talk to a microcontroller (the brains of an embedded device). The microcontroller chip alone cannot do much, so you will want a devkit board. Many options exist, but I am using the ESP32-S2-Saola-1RI (~18 €), because I am interested in working with the ESP32-S2 microcontroller, and because it exposes the UART pins in a convenient manner.

Picture of a devkit board showing various components
This is an ESP32-S2-Saola-1RI board, which houses an ESP32-S2 chip (hidden under the large metal housing). Other components include PSRAM memory, flash storage, USB drivers, LEDs, buttons.

The simplest way to use UART is to connect it to your computer, and you would do that with a UART-to-USB adapter. I am using a CH340G-2 (~2 €). These usually come with “jumper wires”, which you will need to connect the adapter to the microcontroller board.

Picture of a small electronic board that looks like a USB stick with a few electronic components
The big black chip is a CH340G which does the actual conversion, and gives its name to the device.

We connect GND to GND, 5V to 5V, TX to RX, and RX to TX. In this case, the pins are labelled directly on the microcontroller board. But you usually have to check the pin layout in the documentation (or “datasheet”).

I like to use black for GND and red for 5V, but any will do.

When I plug the adapter into my computer, various LEDs light up:

  • a blue LED on the adapter informs me that it is powered;
  • a red LED on the microcontroller board informs me that it is powered as well;
  • an RGB LED slowly cycles through green, red and blue (green in the picture below); this is the default program loaded on the board, a kind of “Hello, World”.

To see what the microcontroller is sending through UART, we first need to find the name that the computer assigned to it (“device” or “port”).

On Linux, it usually shows up as /dev/ttyUSB0, but it might vary a bit (e.g. /dev/ttyUSB1, /dev/ttyACM0). To be sure, run sudo dmesg. You should see some lines like the ones below after plugging the UART-to-USB adapter. The last line in the example is telling you that the device you should use is /dev/ttyUSB0.

On macOS, the device will usually appear as /dev/tty.usbserial-* or /dev/tty.usbmodem-* where * will depend on the specific device. On Windows, it will be COM1, or COM2, etc.

On all three, you can use PuTTY as the UART client. First, in the main screen, switch the Connection type: to Serial, then put the name of the device in the text field under Serial line. For Speed, you will have to check the default baud rate of the microcontroller. In my case, this is 115200, which is the most frequent one, although 57600 is also common.

Note: On Linux, you might need to run the UART client as sudo. If you want to avoid that, look into adding udev rules and/or adding your user to the dialout group (note that you need to log out and in again after adding your user to a new group).

After clicking Open, I can see text being sent from the microcontroller periodically. Note that this might be different if you are using a different microcontroller, or if the manufacturer of the board flashed it with a different firmware.

However, this is not the most impressive thing about UART. As someone used to working with PCs rather than embedded systems, I expect to wait several seconds after boot before getting anything on the screen. With UART, you can get feedback from the microcontroller from the first millisecond!

To observe this, just press the “RST” button on the board while your UART client is running. Here is the output I get when looking at the UART output with tio when the microcontroller boots.

I could not find the exact source code for the firmware that is running on it, but it is based on an example from the esp-idf repository. Here, text is sent to UART by using the ESP_LOGI macro:

    ESP_LOGI(TAG, "LED Rainbow Chase Start"); // TAG == "example"

This relies on Espressif’s development framework, named ESP-IDF. When using other microcontrollers, other frameworks or other languages, you might have to do things differently.

And that’s all for today!

Leave a Reply

Your email address will not be published. Required fields are marked *