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:

−

The communication depends on byte wise data transmission and is generally available even before boot time.

+

* Connecting to a PC to allow access to the Linux [http://en.wikipedia.org/wiki/System_console 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 [http://en.wikipedia.org/wiki/Serial_port serial] interface. This can be useful if you want the Pi to control another device.

−

==First interaction with the board==

+

== Connections and signal levels ==

−

'''Note that the Raspberry has no PREBUILT COM port. Soldering/protoyping and technical knowledge is required ! '''

A UART is available in the Broadcom SoC, and its RxD and TxD lines are available on the [[RPi_Low-level_peripherals|GPIOs]] but you need a board or cable to convert 3.3V UART signals to RS-232. They do however connect directly to a USB serial legacy device, provided it uses 3V3 levels. If the device uses 5V levels you will need level converters (which could be simple resistor dividers, or could be specific IC's) See [http://codeandlife.com/2012/07/01/raspberry-pi-serial-console-with-max3232cpe/ this tutorial] for one example on how to build 3.3V to RS-232 levels converter with a breadboard, MAX3232CPE and five 0.1 uF capacitors. A 5V to 3V resistor divider can be built from a 2K2 (top) and a 3K3 (bottom) resistor.

+

The Raspberry Pi serial port consists of two signals (a 'transmit' signal, TxD and a 'receive' signal RxD) made available on the [[RPi_Low-level_peripherals|GPIO header]]. To connect to another serial device, you connect the 'transmit' of one to the 'receive' of the other, and vice versa. You will also need to connect the Ground pins of the two devices together.

−

Connect the serial cable to your COM circuitry , and connect the other end to the COM port or USB Serial Adapter in the computer.

+

The Broadcom chip at the heart of the Pi uses 0 and 3.3V logic levels, not the +/-12V used by [http://en.wikipedia.org/wiki/RS-232 RS-232] serial ports found on some older PCs. If you wish to connect one of these, you need a board or adapter to convert the signal levels. See [http://codeandlife.com/2012/07/01/raspberry-pi-serial-console-with-max3232cpe/ 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 you wish to connect your Pi to a PC with a USB port, the simplest option is to use a USB-to-serial cable which uses 3.3V logic levels (e.g. the [http://www.adafruit.com/products/954 Adafruit 954] or the [http://www.ftdichip.com/Products/Cables/RPi.htm FTDI TTL-232R-RPI] cables). These can be simply plugged in directly to the GPIO header (see illustration).

The TxD and TXD 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 you wish to connect to a peripheral which has 0/5V signals, you should ideally have a circuit to convert between the voltage levels. See [http://www.element14.com/community/groups/raspberry-pi/blog/2012/07/18/look-ma-no-display-using-the-raspberry-pi-serial-console 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 [http://en.wikipedia.org/wiki/Voltage_divider voltage divider] to the GPIO RxD. A 5V to 3V resistor divider can be built from a 2K2 (top) and a 3K3 (bottom) resistor.

−

==Serial Parameters==

+

−

The following parameters are needed to connect to the Raspberry. All parameters except '''Port_Name''' and '''Speed''' are default values and may not need to be set.

You can connect the Pi to a PC using a USB-serial cable, or (if it has an RS232 port) a level-converter circuit - see above for details. When this is done, you will need to set up a [http://en.wikipedia.org/wiki/Terminal_emulator terminal emulator] program on your PC as described below.

−

** USB Serial Port Adapter: ttyUSB0 ... ttyUSBn

+

−

* '''Speed''': 115200

+

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

* Bits: 8

* Parity: None

* Parity: None

Line 29:

Line 31:

* Flow Control: None

* Flow Control: None

−

The Serial Port is generally usable by the users in the group '''dialout'''. To add oneself to the group '''dialout''' the the following command needs to be executed with '''root''' privileges:

+

===Linux terminal set up===

−

$useradd -G {dialout} your_name

+

+

If your PC is running Linux, you will need to know the ''port name'' of its serial port:

+

+

* Built-in (standard) Serial Port: the Linux standard is '''/dev/ttyS0''', '''/dev/ttyS1''', 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/ttyUSB0

+

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:

+

+

id

+

+

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

* '''Super Easy Way Using GNU Screen'''

Enter the command below into a terminal window

Enter the command below into a terminal window

−

screen Port_Name 115200

+

screen ''port_name'' 115200

+

+

To exit GNU screen, type Control-A k.

* '''Super Easy Way Using Minicom'''

* '''Super Easy Way Using Minicom'''

Line 42:

Line 64:

minicom -b 115200 -o -D Port_Name

minicom -b 115200 -o -D Port_Name

+

+

You can exit minicom with Control-A x

* '''Tedious Old-Fashioned Way Using Minicom'''

* '''Tedious Old-Fashioned Way Using Minicom'''

Line 52:

Line 76:

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

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

+

===Windows terminal set-up===

−

* '''Windows Users'''

+

Users of Windows Vista or later will need to download a terminal program, for instance [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html PuTTY], or [http://en.wikipedia.org/wiki/Tera_Term TeraTerm]. Users of XP and below can choose between using ''PuTTY'' and the built-in ''Hyperterminal.''

PuTTY users simply need to choose 'serial', select the correct COM port and set the speed, as shown in the dialog below.

−

Users of XP and below can choose between using ''putty'' and ''Hyperterminal.''

+

−

==First Dialog==

+

[[File:Putty-settings.png]]

−

If you get the prompt below, you are connected to the Raspberry Pi shell!

+

−

prompt> #

+

−

First command you might want try is "help":

+

If you are unsure of the COM port, run [[https://en.wikipedia.org/wiki/Device_manager Device Manager]] and look under 'Ports'. USB-attached serial adapters should have the name of the adapter shown (the Adafruit cable comes up as 'Prolific USB-to_Serial Comm Port'.

−

prompt> # help

+

−

If you get some output, you are correctly connected to the Raspberry Pi! Congratulations!

+

===Boot messages===

−

==Unwanted serial garbage input==

+

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

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.

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.

−

==Dedicated control of serial port==

+

==Connection to a microcontroller or other peripheral==

−

there are several minor things in the way if you want to have dedicated control of the serial port on a raspberry pi

+

+

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. Some Arduino variants use 5V levels, and should have level conversion (see 'Connections and Signal levels' above). Even 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 <code>/dev/ttyAMA0</code> 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 <code>/boot/cmdline.txt</code>. The file will look something like this:

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 <code>/boot/cmdline.txt</code>, should contain:

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

−

the first is kernel boot messages at startup, those are caused by the kernel parameter console=ttyAMA0,115200 set in /boot/cmdline.txt

+

* Secondly, after booting, a login prompt appears on the serial port. This is controlled by the following lines in <code>/etc/inittab</code>:

+

#Spawn a getty on Raspberry Pi serial line

+

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

−

next is the login prompt, set in /etc/inittab with the 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

−

kernel debuging is also on the serial port and i dont know what effect it may have on the end-use, so you may want to turn that off also in cmdline.txt, just remove kgdboc=ttyAMA0,115200

+

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

−

once all of those are off, 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 <code>getty</code> processes using the serial port.

−

some distro's may be setup differently, just double check 'ps aux|grep ttyAMA0' and 'cat /proc/cmdline' to verify ttyAMA0 is not in use anywhere

+

A tutorial on accessing the Pi's serial port from Python is available at [[Serial_port_programming]].

+

===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 [https://github.com/rewolff/bw_rpi_tools/tree/master/gpio gpio_setfunc]

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 [https://github.com/rewolff/bw_rpi_tools/tree/master/gpio gpio_setfunc]

Connections and signal levels

Adafruit serial cable connected to Pi

The Raspberry Pi serial port consists of two signals (a 'transmit' signal, TxD and a 'receive' signal RxD) made available on the GPIO header. To connect to another serial device, you connect the 'transmit' of one to the 'receive' of the other, and vice versa. You will also need to connect the Ground pins of the two devices together.

The Broadcom chip at the heart of the Pi uses 0 and 3.3V logic levels, not the +/-12V used by RS-232 serial ports found on some older PCs. 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 you wish to connect your Pi to a PC with a USB port, the simplest option is to use a USB-to-serial cable which uses 3.3V logic levels (e.g. the Adafruit 954 or the FTDI TTL-232R-RPI cables). These can be simply plugged in directly to the GPIO header (see illustration).

If you wish to connect to a peripheral which has 0/5V signals, 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.

Connection to a PC

You can connect the Pi to a PC using a USB-serial cable, or (if it has an RS232 port) a level-converter circuit - see above for details. 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

If your PC is running Linux, you will need to know the port name of its serial port:

Built-in (standard) Serial Port: the Linux standard is /dev/ttyS0, /dev/ttyS1, and so on

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/ttyUSB0

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:

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.

PuTTY users simply need to choose 'serial', select the correct COM port and set the speed, as shown in the dialog below.

If you are unsure of the COM port, run [Device Manager] and look under 'Ports'. USB-attached serial adapters should have the name of the adapter shown (the Adafruit cable comes up as 'Prolific USB-to_Serial Comm Port'.

Boot messages

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

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. Some Arduino variants use 5V levels, and should have level conversion (see 'Connections and Signal levels' above). Even 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:

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:

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

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