How to use Raspberry Pi UART ?

Prepare:

Hardware: Raspberry Pi A+/B+/2/3/4

OS: Raspberry Pi OS

Step 1:

Flashing image to Micro SD Card;

Step 2:

Configure UART on Raspberry Pi

In Raspberry Pi, enter following command in Terminal window to enable UART,

sudo raspi-config

Select -> Interfacing Options

After selecting Interfacing option, select Serial option to enable UART

Then it will ask for login shell to be accessible over Serial, select No shown as follows.

At the end, it will ask for enabling Hardware Serial port, select Yes,

Finally, our UART is enabled for Serial Communication on RX and TX pin of Raspberry Pi 3.

Then, reboot the Raspberry Pi.

Serial Port for UART Communication

By default, mini UART is mapped to UART pins (TX and RX) while PL011 is connected to on-board Bluetooth module on Raspberry Pi 3.

In previous version of Raspberry Pi models, PL011 is used for Linux Console output (mapped to UART pins) and there is no on-board Bluetooth module.

After making above configuration, UART can be used at UART pins (GPIO14 and GPIO15).

To access mini UART in Raspberry Pi 3, ttyS0 port is assigned. And to access PL011 in Raspberry Pi 3 ttyAMA0 port is assigned. But in other models of Raspberry Pi, there is only ttyAMA0 port is accessible.

Hardware UART port i.e. GPIO14(TXD) and GPIO15 (RXD) is also known as serial0 while other UART port which is connected to Bluetooth module is known as serial1.These names are created as serial aliases for Raspberry Pi version portability.

We can check which UART i.e. mini UART (ttyS0) or PL011 UART (ttyAMA0) is mapped to UART pins (GPIO14 and GPIO15). To check UART mapping, enter following commands.

ls -l /dev

The UART mapping for /dev/ttyS0 and /dev/ttyAMA0 is shown below,

Test serial communication in between Raspberry Pi and PC

To test that our Serial communication is working or not make the connections shown in below figure.

Raspberry Pi Interface with PC/Laptop for Serial Communication

Open Terminal on Laptop/PC to receive the data which will be transmitted from the Raspberry Pi.

Now, enter the following command to transmit data from Raspberry Pi terminal.

echo “Hello” > /dev/ttyS0

This command will output “Hello” string on UART port i.e. Tx pin and will display it on terminal application of PC/Laptop.

By default, mini UART is mapped to the GPIO14 (TX) and GPIO15(RX). While PL011 i.e. ttyAMA0 is connected to the on-board Bluetooth module.

Last updated