Serial Port setup in Raspberry Pi OS
Configure the serial port on Raspberry Pi
Created: 19/03/2016 | Last Updated: 02/07/2021
The Raspberry Pi contains a UART serial port on the GPIO header on pins 8, TXD (GPIO 14) and 10, RXD (GPIO 15). The UART port can be used to connect to a wide range of devices either directly using 3.3V UART pins or with a serial interface to convert the UART voltages to an industry standard such as RS232 or RS485. When connecting a device to the UART serial port always ensure your device operates at correct voltage, 3.3V. Connecting a 5V device to the UART port can damage the Raspberry Pi GPIO header.
Using the UART serial port
The UART port can be enabled using the raspi-config utility.
Step 1 - Install Raspberry Pi OS onto a SD card and boot the Raspberry Pi.
Login via terminal or desktop and shell
Configure the system with:
Step 2 - Select “3 Interface Options”
Step 3 - Select “P6 Serial Port”
Step 4 - A screen will ask you if you would like a login shell to be accessible over serial. If you want to control the Raspberry Pi through a serial port terminal, select Yes. If you are using the serial port to control other serial devices, select No.
Step 5 - A confirmation screen should appear showing the serial port has been enabled.
Step 6 - On the main raspi-config screen select finish and reboot your Raspberry Pi. The UART serial port should now be enabled.
Disabling the Serial Login Shell (alternative method)
To disable the serial login shell without using raspi-config, you can use the following steps.
Step 1: To disable the serial login shell, you need to edit the /boot/cmdline.txt file
sudo nano /boot/cmdline.txt
Step 2: Find the following text and remove it:
Step 3: Exit the editor saving your changes and then:
With the serial login shell disabled you can now access the UART serial port at /dev/ttyAMA0.
Notes for the Raspberry Pi 3 Model B, B+, 4 and Raspberry Pi Zero W:
The Raspberry Pi 3 Model B, B+, 4 and Raspberry Pi Zero W contain two UART controllers which can be used for serial communication, the mini UART and PL011 UART. By default, the mini UART is mapped to the TXD (GPIO 14) and RXD (GPIO 15) on the 40 pin GPIO header and the PL011 UART is used for the Bluetooth/Wireless module but either module can be mapped to the GPIO port.
The mini UART port can be accessed using the /dev/ttyS0 device in Linux. One issue with the mini UART controller is that its baud rate is linked to the VPU core frequency so as the VPU frequency changes depending on processor load so does the baud rate of the UART controller. This will cause problems as serial devices expect a constant baud rate during communication.
To fix this issue the VPU core frequency must be fixed which can either limit the speed of the Raspberry Pi or increase power consumption and heat generated depending on what speed the core frequency is set at.
The mini UART controller has a smaller FIFO buffer than the PL011 UART controller. This can cause problems at higher baud rates as it makes losing characters more likely.
The PL011 UART controller is not linked to the VPU core frequency. This means that the baud rate stays the same regardless of VPU speed. The PL011 UART controller also includes other features not present in the mini UART controller such as framing error detection, break detection, receive timeout interrupts and parity bit support.
The main disadvantage of using the PL011 UART controller is that it disables the Bluetooth/Wireless module so you will be unable to use the Bluetooth features of the Raspberry Pi 3 and Zero W.
We recommend using the PL011 UART controller in any situation where a reliable data connection is needed, or power consumption and processor speed are a priority.
If you need to use the Bluetooth module on the Raspberry Pi, then you should use the mini UART controller.
Using the PL011 UART port
For Raspberry Pi OS and Raspbian Linux there is a device tree file called disable-bt which makes the Raspberry Pi disable the Bluetooth module and map pl011 UART on pins 14 and 15.
Step 1 - Login via terminal or desktop and shell
Step 2: Device Tree settings as below:
Add device tree to /boot/config.txt to disable the bluetooth module.
sudo nano /boot/config.txt
Add at the end of the file
Exit the editor.
Step 3: You must also disable the hciuart service which initialises the bluetooth modem:
sudo systemctl disable hciuart
Step 4: Reboot your Raspberry Pi to enable the changes: