How do I make serial work on the Raspberry Pi3 (or later model)

  • My Pi3 serial console produces rubbish and fails to respond to the keyboard.

  • Milliways

    Milliways Correct answer

    4 years ago

    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 Select Interfacing 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 SOC 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 the core_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 pi3-miniuart-bt and pi3-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 set `core_freq=250` and then configure my baud rate in ma python script? Also, would be great if you could have a look at: http://raspberrypi.stackexchange.com/questions/47379/uart-problems-on-my-pi3 Thank you!

    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)

    @OE1 If you have a question ask it in a new question, not in Comments.

    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.

    @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.

    I will add that, for users that only have a serial cable and/or can't connect to the new device using the ethernet port, the easy way is to mount the SD card on a regular PC (ignore the please format errors), open the standard MS-DOS partition and notepad \boot\ config.txt adding `enable_uart=1` and `core_freq=250` at the end. Thank @milliways

    Thanks a lot! I was really confused when I used ttyAMA0 and my bluetooth keyboard stopped immediately. After I disabled bluetooth = dtoverlay=pi3-disable-bt in /boot/config.txt and using /dev/serial0 instead of /dev/ttyAMA0. Everything started to work!

    Could you add that USB to UART converter should be 3.3V compatible and not 5V compatible.

  • 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-------------------------------

    On 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?

  • 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.

    How can this work? Bluetooth UART is not even connected to GPIO pins!

    @DmitryGrigoryev It works for me on my PI 3.

    This worked for me as well, I found that this thread helped to clarify a few small issues: https://www.raspberrypi.org/forums/viewtopic.php?f=107&;t=138223&start=50

  • Raspberry Pi4 UART

    The Pi4 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.

    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

    • uart2 uses GPIO0/1 which are reserved 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).

    If additional UARTs are enabled they will appear as /dev/AMAn
    The first will be /dev/AMA1 and increment 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  (I2C)
    uart3   4   5   6   7       7   29
    uart4   8   9   10  11      24  23  (SPI0)
    uart5   12  13  14  15      32  33  (gpio-fan)
    

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM