My Pi3 serial console produces rubbish and fails to respond to the keyboard.
4 Answers
This answer is still correct, and explains in more detail the nature of the changes, but most users of current Raspbian should just run
sudo raspi-config
SelectInterfacing Options / Serial
then specify if you want a Serial console (probably no) then if you want the Serial Port hardware enabled (probably yes). Then use/dev/serial0
in any code which accesses the Serial Port.
The BCM2837 on the Raspberry Pi3 , Pi3B+, Pi3A+, PiZeroW has 2 UARTs (as did its predecessors), however to support the Bluetooth functionality the fully featured PL011 UART was moved from the header pins to the Bluetooth chip and the mini UART made available on header pins 8 & 10.
(The BCM2711 on the Pi4 has additional UARTs, but the same 2 UARTs as BCM2837 are used for default serial on pins 8 & 10 and Bluetooth.)
This has a number of consequences for users of the serial interface.
The /dev/ttyAMA0
previously used to access the UART now connects to Bluetooth.
The miniUART is now available on /dev/ttyS0
.
In the latest operating system software there is a /dev/serial0
which selects the appropriate device so you can replace /dev/ttyAMA0
with /dev/serial0
and use the same software on the Pi3 and earlier models.
Unfortunately there are a number of other consequences:-
The mini UART is a secondary low throughput UART
intended to be used as a console.
The mini Uart has the following features:
• 7 or 8 bit operation.
• 1 start and 1 stop bit.
• No parities.
• Break generation.
• 8 symbols deep FIFOs for receive and transmit.
• SW controlled RTS, SW readable CTS.
• Auto flow control with programmable FIFO level.
• 16550 like registers.
• Baudrate derived from system clock.
There is no support for parity and the throughput is limited, but the latter should not affect most uses.
There is one killer feature "Baudrate derived from system clock" which makes the miniUART useless as the this clock can change dynamically e.g. if the system goes into reduced power or in low power mode.
Modifying the /boot/config.txt
removes this dependency by adding the following line at the end:-
core_freq=250
This fixes the problem and appears to have little impact. The SPI clock frequency
and ARM Timer
are also dependent on the system clock.
For some bizarre reason the default for Pi3 using the latest 4.4.9 kernel is to DISABLE UART. To enable it you need to change
enable_uart=1
in/boot/config.txt
. (This also fixes thecore_freq
so this is no longer necessary.)
Finally if you don't use Bluetooth (or have undemanding uses) it is possible to swap the ports back in Device Tree. There is a miniuart-bt
and disable-bt
module which are described in /boot/overlays/README
.
-
Thank you @Milliways for the great explanation. What I haven't go though... I have a Radio-Module that connects to my raspberry using UART and it prefers a baud-rate of
9600
. So that should work if I setcore_freq=250
and then configure my baud rate in ma python script? Also, would be great if you could have a look at: raspberrypi.stackexchange.com/questions/47379/… Thank you!– GeorgCommented May 15, 2016 at 18:30 -
I have tried /dev/serial0, and enable_uart=1 in /boot/config.txt, with a wiringPi code that used to work with raspberry pi 2, yet it still fails to initiate serial communication (return condition is -1). I also tried /dev/S0 to no avail. What might I be missing? (also freed serial port from raspi-config). (everything including raspbian is up to date) Commented May 25, 2016 at 7:32
-
I am little confused. How do you mean it doesn't support parity? I have a device my pi3 talks to and it has to have EVEN parity or my system won't talk to it. After following your answer and naseer answer, I was able to get my pi3 running my program talk SUCCESSFULLY to this device which is set for EVEN parity.– ThNCommented Sep 16, 2016 at 13:51
-
@ThN The Broadcom datasheet states "No parities". If you send serial with parity the Pi will ignore the parity. The Pi will send with no parity - what your device will do with the 50% of characters with wrong parity depends on the device. If you have questions you should ask as a question, not in comments. Commented Sep 17, 2016 at 1:10
-
1Could you add that USB to UART converter should be 3.3V compatible and not 5V compatible.– MaNyYaCkCommented Jul 17, 2018 at 8:35
finally this got work for my pi3 (os: debian jessie)
please follow these 6 steps carefully.
Step 1 - Install Raspbian Jessie onto a SD card and boot the Pi when connected to a network Login via terminal or desktop and shell Configure the system with:
sudo raspi-config
Expand filesystem and enable serial on advanced page, exit and reboot.
Step 2 -this won't necessary if you have jessie new release Update the system with:
sudo apt-get update
sudo apt-get upgrade
Step 3 - Device Tree settings as below:
Add device tree to /boot/config.txt to disable the Raspberry Pi 3 bluetooth.
sudo nano /boot/config.txt
Add at the end of the file
*if you want to change the blutooth to miniuart port(bad)
dtoverlay=pi3-miniuart-bt
*if you want to disable the blutooth(good)
dtoverlay=pi3-disable-bt
Exit the editor saving your changes.
Step 4 - reboot the pi
sudo reboot
step 5 -
a)to disable the Serial Console edit the file using
sudo nano /boot/cmdline.txt
remove the word phase "console=serial0,115200
" or "console=ttyAMA0,115200
"
Exit and save your changes
b)to Enable the Serial Console edit the file using
sudo nano /boot/cmdline.txt
Change the file to the following:
dwc_otg.lpm_enable=0 console=tty1 console=serial0(or ttyAMA0),115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Exit and save your changes
Step 6 - reboot the pi
sudo reboot
-----------------that's all,have fun-------------------------------
-
2On rpi2 this works and I have it connected to a bluetooth relay board and send it messages via AT commands. But can I receive data from an arduino this way? Commented Jun 16, 2017 at 0:23
Raspberry Pi4 UART
The BCM2711 used in the Raspberry Pi 4 Model B, the Raspberry Pi 400, and the Raspberry Pi Compute Module 4 has 4 additional UART (uart2-uart5) in addition to uart0/1 on the older Pi (only one of which can be used as they share GPIO).
Functionally these are equivalent to the fully featured PL011 UART on uart0 and can optionally be configured with CTS/RTS.
See UART configuration for Raspberry Pi Ltd. documentation.
These can be enabled (by editing /boot/config.txt
), but this requires careful consideration of the impact on GPIO functionality.
Activation of CTS/RTS functionality has additional impact.
NOTE you can enable overlays on the fly e.g.sudo dtoverlay uart4
Manually loaded overlays can be removed e.gsudo dtoverlay -r uart4
Changes to the OS mean that enabling some overlays on the fly is not currently working, although formerly it worked.
- uart2 uses GPIO0/1 which are reserved1 and possibly impact on normal Raspbian functionality.
- uart3 uses GPIO4/5 which is OK, although GPIO4 is commonly used for other purposes.
- uart4 uses GPIO8/9 which are used for SPI0.
- uart5 uses GPIO12/13 which conflict with the default pin allocation
of gpio-fan (although this can be changed).
1 These pins are documented as "RESERVED". It is possible to configure uart2 inconfig.txt
(at least if no HATs are present). Impact on functionality is unknown.
If additional UARTs are enabled they will appear as /dev/ttyAMAn
.
In current kernel /dev/ttyAMAn
corresponds to UARTn.
In older OS/kernel they were numbered sequentially starting with /dev/ttyAMA1
and incrementing if additional UART are enabled.
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
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
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
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
Load: dtoverlay=uart5,<param>
Params: ctsrts Enable CTS/RTS on GPIOs 14-15 (default off)
The following summarises the pin usage:-
TXD RXD CTS RTS Board Pins
uart0 14 15 8 10
uart1 14 15 8 10
uart2 0 1 2 3 27 28 (I2C0)
uart3 4 5 6 7 7 29
uart4 8 9 10 11 24 21 (SPI0)
uart5 12 13 14 15 32 33 (gpio-fan)
You CAN use uart2 on Pi4 but need to disable other uses of GPIO0/1 with force_eeprom_read=0
& disable_poe_fan=1
Naseer's answer is correct but a bit elaborate if you just flashed the latest rasbian. All I needed to do, is just to add the following line to my config.txt (from e.g. windows where you plug in the SD card on the fat32 partition):
dtoverlay=pi3-disable-bt
Then plug it in, and the pi will directly print stuff on the console pins.
-
2How can this work? Bluetooth UART is not even connected to GPIO pins! Commented Nov 15, 2016 at 15:09
-
5
-
3This worked for me as well, I found that this thread helped to clarify a few small issues: raspberrypi.org/forums/… Commented Oct 21, 2017 at 7:31