This used to be relatively straightforward, but with move from Raspbian Wheezy to Raspbian Jessie, (and then Raspbian Stretch and Buster) things changed. Add to this, the newer Raspberry Pi 3 and 4 family with new hardware and the whole thing became a bit of a Dog’s Breakfast and issues with the Bluetooth to boot.

It suddenly got very confusing.

Following lots of wasted time, I’ve noted down what I think I know so far in this post. With luck you can have your cake and eat it: use the serial port on a Raspberry Pi 3 / 4 and use the Bluetooth AND have the same code work on other Raspberry Pi’s (non RPi3 / 4).

NOTE: You will need the latest firmware May 2016 or later for this to work (works with August 2019 firmware). Should this change again, I’ll update this post.

History

Before I dive into the configuration, it’s worth taking a moment for a little history and orientation about the serial port on the Raspberry Pi.

If you’re a bit old school like me, you’d be expecting to find something called COM1 or similar on a header. In Raspberry Pi / Linux land this COM1 equivalent is found on pins 14 and 15 of the GPIO header and is called /dev/ttyAMA0 (obvious, right?).

Also in Raspberry Pi land, you can use the serial port as a terminal to log in, which is useful if you don’t have a network connection to hand. You can connect to another computer via their serial ports and run a terminal emulator on the other computer and you’ll get a login prompt.

By default the Raspberry Pi uses the serial port for this “console” login and via a software service called “getty”.

Using the serial port with other hardware

So that’s the ‘normal” configuration of the serial port, but serial ports are very useful things. What if we want to use the serial port to get data from a GPS card or program an arduino? In this case we need to disable the console login so that we alone get control of the port. Easy right? Yes and no. There is a big elephant in the room and he’s called Raspberry Pi 3 (also applies to 4).

Before we can describe using the serial port, we have to talk about Raspberry Pi 3 / 4, which throws a great big spanner in the works as far as serial ports are concerned.

Raspberry Pi 3 / 4

Raspberry Pi 3’s and 4’s are great little beasts, and add Bluetooth, yay! However, in order to use the Bluetooth correctly the /dev/ttyAMA0 has been “stolen” from the GPIO header and an inferior second one has been substituted in it’s place. No-one will ever know! Unfortunately /dev/ttyAMA0 was a hardware serial port (uart) and high performance (hence it was nabbed for the Bluetooth) and the second port is partly software and a bit flaky. Many people’s applications got broken.

The second serial port you will see referred to as the “mini uart” and lives at /dev/ttyS0. It also calculates it’s bit timing’s from the CPU cores frequency and if the CPU is under heavy load it can corrupt the serial communications. Not good.

In order to work around this, many people “fix” the CPU core frequency so that the serial port is stable. This comes at a slight loss in performance (though normally not noticeable). I’ll describe how you do this in the next section.

By the way, it’s not all bad for the change of serial port on the Raspberry Pi 3 / 4. The Arduino IDE expects the serial communications to be on /dev/ttyS0 so you have no work to do to map the serial ports across. Yay!

To summarise the ports on a Raspberry Pi 3 / 4 and be crystal clear:

/dev/ttyAMA0 -> Bluetooth /dev/ttyS0 -> GPIO serial port.

If you stick with these as is, your Bluetooth will work as nature intended AND you can use a serial port over the GPIO (there is a way of swapping the serial ports around if you don’t want to use the Bluetooth and I’ll cover that at the end of this post).

Enabling

There is yet another wrinkle in that in the latest Jessie / Stretch / Buster releases (as of August 2019) the GPIO serial port is disabled by default. In order to enable it, edit config.txt:

1

$sudo nano/boot/config.txt

and add the line (at the bottom):

1

enable_uart=1

As of May 2016 this will also lock the cpu core frequency for you so there’s nothing else you need to do (If you aren’t convinced and you really like to belt and braces it the command is: core_freq=250 which you add to the same file aswell).

Reboot for the changes to take effect.

This should get you good serial communications for most uses.

Serial Aliases

On the Raspberry Pi 3 the second serial port is called /dev/ttyS0 and is by default mapped to the GPIO pins 14 and 15. So immediately, if you have code that references /dev/ttyAMA0 you’re going to have problems and things aren’t going to work.

You could go through your code and replace ttyAMA0 with ttyS0 and that should work. However, if you find yourself use the same SD card on a Raspberry Pi other than a rpi3 your code won’t work again.

In order to try and get around this the Foundation have introduced a serial port alias (as of May 2016 – 2016-05-10). Thus you have serial ports: serial0 and serial1 (rpi3). The Raspberry Pi kernel sorts out where these point to depending on which Raspberry Pi you are on. Thus on a Raspberry Pi 3 / 4 serial0 will point to GPIO pins 14 and 15 and use the “mini-uart” aka /dev/ttyS0. On other Raspberry Pi’s it will point to the hardware UART and /dev/ttyAMA0.

To find out where it is pointing you can use the command:

1

$ls-l/dev

Default Raspberry PI 3 / 4 serial port aliases

Default Raspberry PI 2 serial port aliases

So where possible refer to the serial port via it’s alias of “serial0” and your code should work on both Raspberry Pi 3 / 4’s and other Raspberry Pi’s.

Disabling the Console

If you are using the serial port for anything other than the console you need to disable it. This will be slightly different depending on whether you are running a Raspberry Pi 3 / 4 or not.

For non Raspberry Pi 3 / 4 machines, remember it’s /dev/ttyAMA0 that is linked to the getty (console) service. So you need to perform this command from a terminal window:

1

2

$sudo systemctl stop serial-getty@ttyAMA0.service

$sudo systemctl disable serial-getty@ttyAMA0.service

The “disable” will stop it loading in the future.

For Raspberry Pi 3’s the command is similar but referencing /dev/ttyS0:

1

2

$sudo systemctl stop serial-getty@ttyS0.service

$sudo systemctl disable serial-getty@ttyS0.service

You also need to remove the console from the cmdline.txt. If you edit this with:

remove the line: console=serial0,115200 and save and reboot for changes to take effect.

Swapping the Serial Ports on Raspberry Pi 3 / 4

What if you don’t want to use the Bluetooth and you want that high performance /dev/ttyAMA0 back on the GPIO? Well you can do this and the way you do this is via a device overlay called “pi3-miniuart-bt” i.e. use the mini-uart (/dev/ttyS0) for Bluetooth (you may get some loss of performance on your Bluetooth though).

You can also just disable the Bluetooth all together by using another overlay “pi3-disable-bt”. In both cases if you can find out more of what they do here: /boot/overlays/README

Thank you. I’ve been banging my head against the wall for two days trying to figure out why my Pi Zero W wasn’t getting data via UART. I knew data was coming off my GPS and knew was it was getting to the GPIO pins. But I had no data. This article provided the missing information and the key to getting it to work.

Speaking of systemd and serial ports, there is also the damn stupid serial port getty setup that cycles between 115200, 38400, 9600 baud. That is guaranteed to waste a few minutes of your time when you come to log in on a serial port. That is a !@#$ing stupid idea. Once you understand baud rate on serial, you set it correctly, then the RPi does its stupid baud rate dance. The offending code is in /lib/systemd/system/serial-getty@.service . Apart from hacking that file, what is the proper way to tell systemd: halt and desist?

Man, Linux community is really Narcissism. Windows 3.0 already dominated the business, Windows 3.1 was even more success. I downloaded the first Linux from slackware/cdrom.com on a Windows PC. I check out the popular Linux.distro every a few years since then, now running a Debian Stretch on a Rockchip board, the Lixus is still not a usable system for most of people even you consider yourself have good tech savvy.

I’m not too sure about Linux being around longer than Windows Windows 3.0 showed up around 1990. I remember…. I was there. Before that starting around 1985 Windows came in on various versions that weren’t all that hot. Basically a shell over DOS. Linux kernel didn’t show up until 1991.

You’re correct.. UNIX came out during my employment with AT&T (old Western Electric) back in 1979. I was one of the fortunate employees to be allowed to attend the training classes held in Bell Lab at the time.

There was no Windows (or DOS for that matter) until much later. UNIX was booted on old Digital Equipment PDP mainframes with a whopping 4K of non-volatile RAM (magentic cores to retain their states) . We’ve come a long way in such a relatively short length of time. 🙂

hello sir i have Raspberry pi b+ device as well as GSM sim900A modem..
I have to config both divice .. using GPIO pins..
tell me how to config..
and
could u give me Paython code for display receiving sms from mobile on raspberry pi..

If on a Windows box, be sure the Prolific USB-to-Serial device is set to 115200, along with your PUTTY session. I had the garbled text issue on both a Rasp Pi 3 and a Rasp Pi 2. Once I changed my device speed in device manager, I was getting clear text over my serial session.

Thanks, this was a very helpful explanation. Since it was quite a bit of hassle to do this manually each time, I scriptized it. On Raspberry Pi3, the script simply disables Bluetooth. It is available on GitHub: https://github.com/itemir/raspberry_pi_utils

For some reason, the serial ports swapping doesn’t work on my RPI3.
I must have done Something wrong but I can’t figure out what.
When I type ls -l /dev, ttyS0 and ttyAMA remain unchanged.
RPi gurus please advise.
Regards,
Yann

Hi,
I have a problem with serial port /dev/ttyAMA0 on Jessie (Raspberry Pi 2B)
During boot time it sends one byte (0xFF) to serial port which confuses the connected device (DS2480B 1-wire bus-master chip).

Console is disabled in raspi-config, uart is enabled in /boot/config.txt, gpio pins 14,15 in ALT0 mode.

Thanks for this. I had some other problems as well. I also had to issue:
sudo systemctl disable hciuart
to prevent (I am told) the bt modem from trying to keep the port. Also beware that if you use raspi-config to turn off the console AFTER you have edited /boot/config.txt that it seems to also reset enable_uart=0. Too me a while to find that problem 😉

A follow on question. Have moved ttyAMA0 back to gpio14/15, and moved bt to miniuart, and have disabled console on serial as above. It all seems to wrk – except for one very peculiar thing. I am using this as a coprocessor for vision tasks an a flying quad. There is a serial link to the quad’s flight controller that talks to ttyAMA0. The communications works perfectly on pi 0 – just need more computation. When moved to pi 3 – it works occasionally – BUT attaching flight controller TX to gpio15 frequently causes the pi to close all ssh sessions! there is plenty of power (tried separate power supplies, etc), the cable has been replaced and tested on pi 0.

So the question is what the heck could be happening on the uart rx pin that would cause the pi 3 to kick out active ssh connections? I am starting to guess that I just have a bad pi 3 – but thought I would ask first.

IS there a way to ID one of the native (Serial0 / Serial1) as “COMn”? I am wanting to run an application (AdvancedHMI) using mono on an R Pi 3, and the application’s ModbusRTU comm driver is configured to address COM1, 2 or 3………………………..

Running my code on a RPi 3, I had troubles with receiving data on the UART unless I added the line to lock the CPU core frequency (core_freq=250): Adding the line to enable the UART (enable_uart=1) was not enough.