RPi Serial Connection

From eLinux.org
Revision as of 14:46, 1 April 2013 by IanH (Talk | contribs)

Jump to: navigation, search

Back to RPi Advanced Setup.

The Serial Port is a low-level way to send data between the Raspberry Pi and another computer system. There are two main ways in which it can be used:

  • Connecting to a PC to allow access to the Linux console. This can help to fix problems during boot, or to log in to the Pi if the video and network are not available.
  • Connecting to a microcontroller or other peripheral which has a serial interface. This can be useful if you want the Pi to control another device.

Connection to a PC

Note that the Raspberry Pi cannot be wired directly to an RS232 serial port. To do so, some additional electronics are required!

A UART is available in the Broadcom SoC, and its RxD and TxD lines are available on the GPIO header. However, these use 0 and 3.3V logic levels, not the +/-12V used by RS-232 serial ports. If you wish to connect one of these, you need a board or adapter to convert the signal levels. See this tutorial for one example on how to build a 3.3V to RS-232 level converter with a breadboard, a MAX3232CPE IC and five 0.1 uF capacitors.

If your PC has a USB port, it is possible to connect a USB-to-serial cable which uses 3.3V logic levels (e.g. the Adafruit 954 or the FTDI TTL-232R-RPI cables) directly to the GPIO pins.

If you have a serial connection using 5V logic levels, you should ideally have a circuit to convert between the voltage levels. See this tutorial for an example circuit. For prototyping work, provided the connections are kept short you may connect the GPIO TxD directly to the 5V RxD, and the 5V TxD can be connected via a voltage divider to the GPIO RxD. A 5V to 3V resistor divider can be built from a 2K2 (top) and a 3K3 (bottom) resistor.

When this is done, you will need to set up a terminal emulator program on your PC as described below.

Console serial parameters

The following parameters are needed to connect to the Pi console, and apply on both Linux and Windows.

  • Speed (baud rate): 115200
  • Bits: 8
  • Parity: None
  • Stop Bits: 1
  • Flow Control: None

Linux terminal set up

You will need to know the port name of your PC's serial port. On Linux this will be:

  • Built-in (standard) Serial Port: the Linux standard is /dev/ttyS0, /dev/ttyS1, and so on, but on the raspberry pi, it is /dev/ttyAMA0
  • USB Serial Port Adapter: /dev/ttyUSB0, /dev/ttyUSB1, and so on.
    • Some types of USB serial adapter may appear as /dev/ttyACM0 ...

You will need to be a member of the dialout group to access this port (for later releases the required group is tty). You can check which is needed with:

ls -l /dev/ttyAMA0

and you will see something like "crw-rw----T 1 root dialout ..." which says root can 'create,read,write' and the group xxx can 'read,write' to the port and everyone else cannot access it.

To find out if you, the current user, is in the group dialout, use the command:


If you do not see dialout listed, add yourself with the command

sudo usermod -a -G dialout username

You then have a choice of terminal emulation programs:

  • Super Easy Way Using GNU Screen

Enter the command below into a terminal window

 screen port_name 115200

To exit GNU screen, type Control-A k.

  • Super Easy Way Using Minicom

Run minicom with the following parameters:

minicom -b 115200 -o -D Port_Name

You can exit minicom with Control-A x

  • Tedious Old-Fashioned Way Using Minicom

Another method to setup minicom is described in the Tincantools Minicom Tutorial

  • GUI method with GtkTerm

Start GtkTerm, select Configuration->Port and enter the values above in the labeled fields.

Windows terminal set-up

Users of Windows Vista or later will need to download a terminal program, for instance putty, or TeraTerm. Users of XP and below can choose between using putty and the built-in Hyperterminal.

Boot messages

If your connection is set up correctly, when the Pi is booted you should see many messages as the system comes up:

Uncompressing Linux... done, booting the kernel.
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.2.27+ (dc4@dc4-arm-01) (gcc version 4.7.2 20120731 (prerelease) (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08) ) #250 PREEMPT Thu Oct 18 19:03:02 BST 2012
[    0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
[    0.000000] Machine: BCM2708
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 113792
[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2708.boardrev=0xf bcm2708.serial=0xcc5c4b6d smsc95xx.macaddr=B8:27:EB:5C:4B:6D sdhci-bcm2708.emmc_clock_freq=100000000 vc_mem.mem_base=0x1c000000 vc_mem.mem_size=0x20000000  dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

and so on. Eventually, you should see a login prompt:

Debian GNU/Linux wheezy/sid raspi2 ttyAMA0

raspi2 login:

You can then log in as you would with a keyboard and screen.

Unwanted serial garbage input

Note that on older software by accident the internal pullups of the RxD GPIO pins were not enabled, this could lead to lots of serial garbage being picked up if the GPIO pin was touched, or even if a finger was nearby. In extreme case this could lead to kernel warnings and other problems.

Connection to a microcontroller or other peripheral

The TxD and RxD signals can also be connected directly to similar signals on a microcontroller board like the Arduino provided the signals are all at 3V3 levels. It's still a good idea to put 2K2 series resistors in the lines to prevent damage when two outputs are connected together, which could also happen if a GPIO input pin is accidentally programmed as output.

If your microcontroller uses 5V logic levels, level conversion is usually necessary - see 'Connecting to a PC' for details.

Preventing Linux using the serial port

The Broadcom UART appears as /dev/ttyAMA0 under Linux. There are several minor things in the way if you want to have dedicated control of the serial port on a Raspberry Pi.

  • Firstly, the kernel will use the port as controlled by kernel command line contained in /boot/cmdline.txt. The file will look something like this:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

The console keyword outputs messages during boot, and the kgdboc keyword enables kernel debugging. You will need to remove all references to ttyAMA0. So, for the example above /boot/cmdline.txt, should contain:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

You must be root to edit this (e.g. use sudo nano /boot/cmdline.txt). Be careful doing this, as a faulty command line can prevent the system booting.

  • Secondly, after booting, a login prompt appears on the serial port. This is controlled by the following lines in /etc/inittab:
#Spawn a getty on Raspberry Pi serial line
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

You will need to edit this file to comment out the second line, i.e.

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Finally you will need to reboot the Pi for the new settings to take effect. Once this is done, you can use /dev/ttyAMA0 like any normal linux serial port, and you wont get any unwanted traffic confusing the attached devices.

The above instructions have been verified on Raspbian 'wheezy'; other distributions may be set up differently. To double-check, use

cat /proc/cmdline

to show the current kernel command line, and

ps aux | grep ttyAMA0

to search for getty processes using the serial port.

Handshaking lines

You can have the RTS0 signal on GPIO 17 (P1-11) if you set it to ALT function 3. Likewise, the CTS0 is available on GPIO 30 (P5-05), if it is set to ALT function 3. You can control the settings of IOs with gpio_setfunc