# How to use Raspberry Pi UART ?

<figure><img src="https://389921961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjPowryxiQvRDiYHC9qWy%2Fuploads%2F4QIlWcKvBx9J7cJpUr69%2Fimage.png?alt=media&#x26;token=df58a241-926f-4c31-a257-955b64046345" alt=""><figcaption></figcaption></figure>

### 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

<figure><img src="https://389921961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjPowryxiQvRDiYHC9qWy%2Fuploads%2FwdtSg3meZ7AsbAts6OrX%2Fimage.png?alt=media&#x26;token=8dabb240-a7a4-4a5c-9eba-8da158218e01" alt=""><figcaption></figcaption></figure>

&#x20;After selecting Interfacing option, select Serial option to enable UART

<figure><img src="https://389921961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjPowryxiQvRDiYHC9qWy%2Fuploads%2F5Zb3UbsilU6DCER2vqtu%2Fimage.png?alt=media&#x26;token=51a4b051-b540-4e12-b57e-6325497975b5" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://389921961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjPowryxiQvRDiYHC9qWy%2Fuploads%2FB1WfyEK0MK4nMFUkVLfm%2Fimage.png?alt=media&#x26;token=f62be215-e758-4df3-a145-afae905a8c21" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://389921961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjPowryxiQvRDiYHC9qWy%2Fuploads%2F6whW6Hop2NLxpf1BgMk2%2Fimage.png?alt=media&#x26;token=3b87a3f6-193f-4667-8ace-455fe83eaa17" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://389921961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjPowryxiQvRDiYHC9qWy%2Fuploads%2FtjQpGFmzN31jIAHbvHoL%2Fimage.png?alt=media&#x26;token=66074296-3ccd-48d6-b984-f6e6d6c5c704" alt=""><figcaption></figcaption></figure>

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,

<figure><img src="https://389921961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjPowryxiQvRDiYHC9qWy%2Fuploads%2FCjQ90aDA7sxPcqtzHxZZ%2Fimage.png?alt=media&#x26;token=fe98f17c-3ca1-473e-8d30-b88243eb438d" alt=""><figcaption></figcaption></figure>

&#x20;**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.

<figure><img src="https://389921961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjPowryxiQvRDiYHC9qWy%2Fuploads%2FiIRJp7aQtVG1aqqtlyhL%2Fimage.png?alt=media&#x26;token=ed81af15-ee51-4290-8eef-cfcaa55462e2" alt=""><figcaption></figcaption></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.
