Serial Console to Raspberry Pi
Imagine a scenario when your Raspberry Pi doesn’t have an internet connection and you cannot SSH to it but you want to access its terminal console without purchasing a display, keyboard, and mouse to plug into your Pi. Interesting issue, right?
In this post, we’ll look at an alternative way to access your Raspberry Pi through a shell using the serial console. Not only this comes useful, in the event described above but also in case you need to debug what’s going on during the boot process — for example when you are trying to netboot your Raspberry Pi or make it boot faster/smother or your Pi doesn’t boot at all. Note that SSH is no help since the connection is only available once the boot process is already over.
That’s too bad, right?
Equipment
To follow this post on your own, you’re gonna need the following things:
- Raspberry Pi (3B+ or 4)
- PC running Linux OS (tested on Ubuntu 20.04)
- USB to UART (Universal Asynchronous Receiver/Transmitter) converter
I’m sure you can find great resources online on how to hook up the UART side of the cable to the Raspberry Pi, the rest should be pretty obvious. For reference, Raspberry Pi pinout looks as follows, where it’s nicely seen how GND, TXD, RXD pins correspond to the one in the picture above:
Your Pi can be running any Linux flavor OS since the final goal is to read the terminal output. Technically it is possible to do it with Windows OS as well by using PuTTY but I’m not gonna cover this in this tutorial.
Before we continue, let’s demystify what a serial console even is?
Serial console
The serial console is nothing other than a terminal console of the Raspberry Pi open on your PC display, where the two are connected through a USB to UART converter using serial communication. You can then manage Raspberry Pi as you would through an SSH connection. But I do need to mention one downside of serial consoles is that they are much slower, taking up to a second to fill an 80-column by 24-line screen and they generally only support non-proportional ASCII text, with limited support for languages other than English.
💥 Often time when debugging UARTs it is difficult to know if your computer serial port is not working or if serial program is broken. Here’s a trick!
🪄 Pins 2 and 3 are TX and RX and if you connect them together (serial loopback), any command you send from the computer will be received by the computer. Open your terminal program, try to connect to your serial cable. Hit any key on your keyboard (Baud rate and things don’t matter because it’s a loopback). If you see the character you sent out received on the terminal, your serial cable works! If not, you have a problem with your cable or with your serial program.
Raspberry Pi configuration
Unfortunately, the serial console on Raspberry Pi doesn’t work out-of-the-box so I had to tinker around a bit to get it working. This might change in the upcoming version of Pis, but still, most of the configurations should stay the same. The files we are going to modify on the Pi are:
/boot/config.txt
: https://www.raspberrypi.org/documentation/configuration/config-txt//boot/cmdline.txt
: https://www.raspberrypi.org/documentation/configuration/cmdline-txt.md
and you might want to have a quick glimpse through the docs, to get a feeling of how the files need to look in case you encounter any issues.
First, we need to enable UART communication on the Pi by adding the following to the/boot/config.txt
:
enable_uart=1
And configure the baud rate in/boot/cmdline.txt
:
console=ttyAMA0,115200
💡baud rate is the number of symbol changes, waveform changes, or signaling events across the transmission medium per unit of time.
Additionally, you need to remove the following lines from /boot/cmdline.txt
:
quiet # surpresses boot log
plymouth.ignore-serial-consoles
To conclude, the files should look something similar to:
/boot/config.txt
disable_overscan=1
start_x=1
gpu_mem=128
init_uart_baud=115200
boot_delay=2
dtparam=i2c_arm=on
enable_uart=1
dtoverlay=i2c-rtc,mcp7940x
/boot/cmdline.txt
dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait logo.nologo splash net.ifnames=0 biosdevname=0 usbhid.mousepoll=0 vt.global_cursor_default=0
As suggested above, in case you experience any errors or your Raspberry Pi doesn’t boot it’s most likely you misconfigured or misformatted something. You can also try the serial loopback test, which should provide you with a better idea of what’s going on.
Your Computer setup
Once you connect the USB to your computer you should be able to see the device listed under/dev
directory. Most likely it will be named /dev/ttyUSB0
. In order to properly communicate using the serial console we need to synchronize the baud rate of both devices — we could change it but I’ll continue with the default. The baud rate can be determined using the following command:
stty -F /dev/ttyUSB0 speed
Afterward, you should be able to open a serial console on a Raspberry Pi by executing:
screen /dev/ttyUSB0 115200
That was easy right and no SSH credentials huh? 😉
Sounds like some form of exploit to me as well.
If you don’t like the screen utility there are also alternatives like minicom and connect to the serial console using:
minicom -b 115200 -D /dev/ttyUSB0
Another option is CoolTerm if you’re into GUIs, that might be best for you. In its options, select the /dev/ttyUSB0
device, and then click Connect. With that said, you should be good to go.
Conclusion
This was rather a short post where we looked at how to monitor Raspberry Pi using a serial console which I think is most useful for debugging when your Pi fails to boot or in general system administration of systems without an internet connection. I’m curious whether you’re using a serial console and for what purpose. Let me know in the comments 🌟
Thanks for reading! 😎 If you enjoyed this article, hit that clap button below 👏
Would mean a lot to me and it helps other people see the story. Say Hello on Linkedin | Twitter
Do you want to start reading exclusive stories on Medium? Use this referral link 🔗
If you liked my post you can buy me a Hot dog 🌭
Are you an enthusiastic Engineer, who lacks the ability to compile compelling and inspiring technical content about it? Hire me on Upwork 🛠️
Checkout the rest of my content on Teodor J. Podobnik, @dorkamotorka and follow me for more, cheers!