Raspberry Pi 4 with Multiple UART Interfaces
Environment
## Test Command
$ cat /proc/cpuinfo
$ cat /etc/os-release
$ uname -a
$ uname -m
- Model : Raspberry Pi 4 Model B Rev 1.4
- Hardware :
- BCM2177* [ref]
- BCM2835 ( 2023/09/05 Updated) - Operation System : Respbian Linux 11
- Arch :
- armv7l
- aarch64 ( 2023/09/05 Updated)
There are 2 types of UART interface existed on Raspberry Pi, mini UART
& PL011 UART
.
By default, /dev/ttyS0
maps to mini UART
; on the other hand, /dev/ttyAMA0
maps to PL011 UART
.
Hardware Check ( 2023/09/05 Updated)
Thanks to Filip Dobrovolný ’s reply, I had check device tree introduction from the /boot/overlays/README
, here is the output related to UART:
Name: uart0
Info: Change the pin usage of uart0
Load: dtoverlay=uart0,<param>=<val>
Params: txd0_pin GPIO pin for TXD0 (14, 32 or 36 - default 14)
rxd0_pin GPIO pin for RXD0 (15, 33 or 37 - default 15)
pin_func Alternative pin function - 4(Alt0) for 14&15,
7(Alt3) for 32&33, 6(Alt2) for 36&37
Name: uart1
Info: Change the pin usage of uart1
Load: dtoverlay=uart1,<param>=<val>
Params: txd1_pin GPIO pin for TXD1 (14, 32 or 40 - default 14)
rxd1_pin GPIO pin for RXD1 (15, 33 or 41 - default 15)
Name: uart2
Info: Enable uart 2 on GPIOs 0-3. BCM2711 only.
Load: dtoverlay=uart2,<param>
Params: ctsrts Enable CTS/RTS on GPIOs 2-3 (default off)
Name: uart3
Info: Enable uart 3 on GPIOs 4-7. BCM2711 only.
Load: dtoverlay=uart3,<param>
Params: ctsrts Enable CTS/RTS on GPIOs 6-7 (default off)
Name: uart4
Info: Enable uart 4 on GPIOs 8-11. BCM2711 only.
Load: dtoverlay=uart4,<param>
Params: ctsrts Enable CTS/RTS on GPIOs 10-11 (default off)
Name: uart5
Info: Enable uart 5 on GPIOs 12-15. BCM2711 only.
Load: dtoverlay=uart5,<param>
Params: ctsrts Enable CTS/RTS on GPIOs 14-15 (default off)
According to the above information, BCM2711 has the capability handled additional 4 UART interface as well.
Software Setup
Open a terminal and change the file config.txt
in /boot
directory with root privilege.
$ sudo su
# vim /boot/config.txt
add following lines at the end of the file
# enable serial interface
enable_uart=1
dtoverlay=uart0
dtoverlay=uart1
dtoverlay=uart2
dtoverlay=uart3
dtoverlay=uart4
dtoverlay=uart5
save & exit, then reboot !
Check system open the serial by command $ ls -al /dev/ttyAMA*
$ ls -al /dev/ttyAMA*
crw-rw---- 1 root dialout 204, 64 Dec 16 16:01 /dev/ttyAMA0
crw-rw---- 1 root dialout 204, 65 Dec 16 16:01 /dev/ttyAMA1
crw-rw---- 1 root dialout 204, 66 Dec 16 16:01 /dev/ttyAMA2
crw-rw---- 1 root dialout 204, 67 Dec 16 16:01 /dev/ttyAMA3
crw-rw---- 1 root dialout 204, 68 Dec 16 16:01 /dev/ttyAMA4
Let’s take a look into the Raspberry Pi 4 Model B GPIO Pinout:
Hardware Setup
Raspberry Pi Pin pair with uart :
TXD PIN | RXD PIN | Communication Port
uart0 : GPIO 14 8 | GPIO 15 10 | /dev/ttyAMA0
uart1 : GPIO 0 27 | GPIO 1 28 | /dev/ttyAMA1
uart2 : GPIO 4 7 | GPIO 5 29 | /dev/ttyAMA2
uart3 : GPIO 8 24 | GPIO 9 21 | /dev/ttyAMA3
uart4 : GPIO 12 32 | GPIO 13 33 | /dev/ttyAMA4
PC setup with USB-to-UART Adapter :
Please make sure GND is connected correctly !
USB-to-UART TXD
connect to Raspberry Pi uart RXD
GPIO Pin
USB-to-UART RXD
connect to Raspberry Pi uart TXD
GPIO Pin
Take uart3 for example
USB-to-UART TXD <--> GPIO 8
USB-to-UART RXD <--> GPIO 9
USB-to-UART GND <--> GND
Implementation
Once finish setup, open a serial console on PC (ex. Putty) with correct COM Port (ex. COM3) & Baud Rate set to 115200
Then open a terminal in Raspberry Pi, send message from CLI to PC :
$ echo 'hello' > /dev/ttyAMA3
This command should send the string hello
to PC via /dev/ttyAMA3
, then the string will be parsed by PC, finally display on serial console.
Test Script ( 2023/09/05 Updated ) from [ref link]
#!/bin/sh
sudo stty -F /dev/ttyAMA0 115200
sudo stty -F /dev/ttyAMA1 115200
sudo stty -F /dev/ttyAMA2 115200
sudo stty -F /dev/ttyAMA3 115200
sudo stty -F /dev/ttyAMA4 115200
while true; do
sudo sh -c "echo 0 > /dev/ttyAMA0"
sudo sh -c "echo 1 > /dev/ttyAMA1"
sudo sh -c "echo 2 > /dev/ttyAMA2"
sudo sh -c "echo 3 > /dev/ttyAMA3"
sudo sh -c "echo 4 > /dev/ttyAMA4"
sleep 1
done
Hope this article can help those makers connect more devices via multiple UART interfaces !! Have Fun !!
Please feel free to contact me by mail anytime if there is anything I could help!
Email : jason19970210@gmail.com