https://elinux.org/api.php?action=feedcontributions&user=Ndemou&feedformat=atomeLinux.org - User contributions [en]2024-03-28T11:01:07ZUser contributionsMediaWiki 1.31.0https://elinux.org/index.php?title=RPi_Serial_Connection&diff=410826RPi Serial Connection2016-06-03T13:15:05Z<p>Ndemou: moved sentence from other section</p>
<hr />
<div>Back to [[RPi Advanced Setup]].<br />
<br />
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:<br />
* 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 Raspberry Pi if the video and network are not available.<br />
* 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 Raspberry Pi to control another device.<br />
<br />
== Connections and signal levels ==<br />
<br />
[[File:Adafruit-connection.jpg|300px|thumb|right|Adafruit serial cable connected to Pi]]<br />
<br />
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.<br />
<br />
The Broadcom chip at the heart of the Raspberry Pi uses 0 and 3.3&nbsp;V logic levels, not the +/-12&nbsp;V 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.3&nbsp;V to RS-232 level converter with a breadboard, a MAX3232CPE IC and five 0.1&nbsp;µF capacitors. <br />
<br />
If you wish to connect your Raspberry Pi to a PC with a USB port, the simplest option is to use a USB-to-serial cable which uses 3.3&nbsp;V logic levels (e.g. the [http://www.adafruit.com/products/954 Adafruit 954] cable, the [http://www.ftdichip.com/Products/Cables/RPi.htm FTDI TTL-232R-RPI] cable, or the [http://www.mysticengineering.com/debug.buddy/pi.usage.html Debug Buddy] ultimate serial port). These can be simply plugged in directly to the GPIO header (see illustration).<br />
<br />
*For using the ''Adafruit 954'' cable on [[Windows]], see [[Adafruit 954 USB serial cable]].<br />
<br />
If you wish to connect to a peripheral which has 0/5&nbsp;V 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 using a ready-made level shifter module. Other circuits for level shifting are shown at [[RPi_GPIO_Interface_Circuits#Level_Shifters]]. The [http://www.mysticengineering.com/debug.buddy Debug Buddy] ultimate serial port can also be configured for 0/5&nbsp;V signals.<br />
<br />
'''NOTE FOR RASPBERRY PI 3:''' The Raspberry pi 3 has changed things a bit and you might need to add the option <code>enable_uart=1</code> at the end of <code>/boot/config.txt</code> (see this [https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=141195 post by a Pi Engineer])<br />
<br />
== Connection to a PC ==<br />
<br />
You can connect the Raspberry Pi to a PC using a USB-serial cable, or (if it has an RS-232 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.<br />
<br />
===Console serial parameters===<br />
The following parameters are needed to connect to the Raspberry Pi console, and apply on both Linux and Windows.<br />
<br />
* Speed (baud rate): 115200<br />
* Bits: 8<br />
* Parity: None<br />
* Stop Bits: 1<br />
* Flow Control: None<br />
<br />
===Linux terminal set up===<br />
<br />
If your PC is running Linux, you will need to know the ''port name'' of its serial port:<br />
<br />
* Built-in (standard) Serial Port: the Linux standard is '''/dev/ttyS0''', '''/dev/ttyS1''', and so on <br />
* USB Serial Port Adapter: '''/dev/ttyUSB0''', '''/dev/ttyUSB1''', and so on.<br />
** Some types of USB serial adapter may appear as '''/dev/ttyACM0''' ... <br />
<br />
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:<br />
ls -l /dev/ttyUSB0<br />
and you will see something like "crw-rw----T 1 root dialout ...", ''c'' means character device, and root can 'read,write' and the group ''dialout'' can 'read,write' to the port and everyone else cannot access it.<br />
<br />
To find out if you, the current user, is in the group dialout, use the command:<br />
<br />
id<br />
<br />
If you do not see ''dialout'' listed, add yourself with the command<br />
sudo usermod -a -G dialout ''username''<br />
<br />
You then have a choice of terminal emulation programs:<br />
<br />
* '''Super Easy Way Using GNU Screen'''<br />
<br />
Enter the command below into a terminal window<br />
screen ''port_name'' 115200<br />
<br />
To exit GNU screen, type Control-A k.<br />
<br />
* '''Super Easy Way Using Minicom'''<br />
<br />
Run Minicom with the following parameters:<br />
<br />
minicom -b 115200 -o -D Port_Name<br />
<br />
You can exit Minicom with Control-A x<br />
<br />
* '''Tedious Old-Fashioned Way Using Minicom'''<br />
<br />
Another method to setup ''minicom'' is described in the [http://www.tincantools.com/wiki/Minicom Tincantools Minicom Tutorial]<br />
<br />
<br />
* '''GUI method with GtkTerm'''<br />
<br />
Start ''GtkTerm'', select Configuration->Port and enter the values above in the labeled fields.<br />
<br />
=== Network connection with the point-to-point protocol (ppp)===<br />
<br />
The easiest way to set up a network connection between your Raspberry Pi and another computer is with an Ethernet cable. If this is not possible, as is the case for the Raspberry Pi Model A, you can set up a connection over the serial cable. This uses the Point-to-point Protocol (PPP). A network connection running over a serial cable can be very useful for copying files onto the Raspberry Pi.<br />
<br />
Step 1: Log in to the Raspberry Pi over the serial cable and run the Point-to-Point Protocol Daemon:<br />
sudo pppd noauth<br />
<br />
Some garbage will start appearing in the terminal. This is the cue to quit your terminal program and proceed to step two.<br />
<br />
Step 2: On your local computer, start the Point-to-Point protocol. On a Linux or Mac computer you can do this by typing:<br />
<br />
sudo pppd noauth proxyarp /dev/tty.usbserial-FTGCC2MV 115200 10.0.0.1:10.0.0.2 passive local maxfail 0 nocrtscts xonxoff<br />
<br />
replacing /dev/tty.usbserial-FTGCC2MV with the name of your serial port. In the above line, 115200 is the baud rate of the connection, 10.0.0.1 is the local internet protocol (IP) address, the address you want your computer to have. 10.0.0.2 is the remote IP address, it is the address that the Raspberry Pi will have.<br />
<br />
Test the connection:<br />
<br />
ping 10.0.0.2<br />
<br />
===Virtual connection to the LAN===<br />
Instead of 10.0.0.0/8 you could as well use normal 192.168.0.0/16 addresses;<br />
the first address must be the real address of the local (serving) system.<br />
You can chose the second address; it must not yet be assigned on the LAN (and be outside the DHCP range).<br />
The advantage is that the system connected to the serial line will appear as if it is directly connected to the LAN (arp protocol).<br />
<br />
You must enable routing on the system directly connected to the LAN for other systems to access the system connected to the serial line:<br />
sudo sysctl -w net.ipv4.ip_forward=1<br />
<br />
On the guest system connected via the serial cable you must set the default route pointing to the serving system, e.g.<br />
sudo route add default gateway 192.168.1.21<br />
You should also configure /etc/resolv.conf if you want to use DNS.<br />
<br />
===Windows terminal set-up===<br />
<br />
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 Windows&nbsp;XP and below can choose between using ''PuTTY'' and the built-in ''Hyperterminal.''<br />
<br />
PuTTY users simply need to choose 'serial', select the correct COM port and set the speed, as shown in the dialog below.<br />
<br />
[[File:Putty-settings.png]]<br />
<br />
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'.<br />
<br />
===Boot messages===<br />
<br />
If your connection is set up correctly, when the Raspberry Pi is booted you should see many messages as the system comes up:<br />
Uncompressing Linux... done, booting the kernel.<br />
[ 0.000000] Initializing cgroup subsys cpu<br />
[ 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<br />
[ 0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d<br />
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache<br />
[ 0.000000] Machine: BCM2708<br />
[ 0.000000] Memory policy: ECC disabled, Data cache writeback<br />
[ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 113792<br />
[ 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<br />
<br />
and so on. Eventually, you should see a login prompt:<br />
<br />
Debian GNU/Linux Wheezy/sid raspi2 ttyAMA0<br />
<br />
raspi2 login:<br />
<br />
You can then log in as you would with a keyboard and screen.<br />
<br />
=== Unwanted serial garbage input ===<br />
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.<br />
<br />
==Connection to a microcontroller or other peripheral==<br />
<br />
===H/W considerations===<br />
If your microcontroller or peripheral works with '''5V logic levels''', level conversion is necessary - see 'Connecting to a PC' for details.<br />
<br />
If your microcontroller or peripheral works with '''3.3V logic levels''' then you can connect its TxD, RxD signals directly to the RxD and TxD pins of the Raspberry. However it's probably a good idea to connect the signals with a 2.2 kΩ resistors in series. This will prevent damage if two outputs are accidentally connected together (e.g. if you connect TxD with TxD or if a GPIO input pin is accidentally programmed as output).<br />
<br />
===S/W: Preventing Linux from using the serial port===<br />
<br />
By default Linux will grab the serial port and use it as a terminal. If you want to use it for other purposes you must prevent this. Here are the methods you can use:<br />
<br />
====Method 1, raspi-config (easiest, try this first)====<br />
<br />
Run <code>sudo raspi-config</code> and check if it has the option <code>advanced options</code> -> <code>serial</code>. If it has, set it to disabled and you're done.<br />
<br />
====Method 2, using an existing script (easy)====<br />
<br />
There's a [https://github.com/lurch/rpi-serial-console nice little script] to automate the steps bellow. <br />
<br />
====Method 3, manual configuration (complex)====<br />
<br />
If neither raspi-config nor the script works for you then follow the hard way.<br />
<br />
'''NOTE FOR RASPBERRY PI 3:''' The Raspberry pi 3 has changed things around a bit: ttyAMA0 now refers to the serial port that is connected to the bluetooth. The old serial port is now called ttyS0. So if you have an RPI3, everywhere you see "ttyAMA0" below, you should read "ttyS0".<br />
<br />
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.<br />
<br />
* 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:<br />
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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:<br />
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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.<br />
<br />
* Secondly, after booting, a login prompt appears on the serial port. This is controlled by the following lines in <code>/etc/inittab</code>:<br />
#Spawn a getty on Raspberry Pi serial line<br />
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
You will need to edit this file to comment out the second line, i.e.<br />
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
Finally you will need to reboot the Raspberry 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 won't get any unwanted traffic confusing the attached devices. <br />
<br />
The above instructions have been verified on Raspbian 'Wheezy'; other distributions may be set up differently. To double-check, use<br />
cat /proc/cmdline<br />
to show the current kernel command line, and <br />
ps aux | grep ttyAMA0<br />
to search for <code>getty</code> processes using the serial port.<br />
<br />
A tutorial on accessing the Raspberry Pi's serial port from Python is available at [[Serial_port_programming]].<br />
<br />
===Handshaking lines===<br />
You can have the [[RPi_BCM2835_GPIOs#RTS0|RTS0]] signal on [[RPi_BCM2835_GPIOs#GPIO17|GPIO 17]] (P1-11) or [[RPi_BCM2835_GPIOs#GPIO31|GPIO 31]] (P5-06) if you set them to ALT function 3. Likewise, the [[RPi_BCM2835_GPIOs#CTS0|CTS0]] is available on [[RPi_BCM2835_GPIOs#GPIO30|GPIO 30]] (P5-05), if it is set to ALT function 3. You can control the settings of I/O pins with [https://github.com/rewolff/bw_rpi_tools/tree/master/gpio gpio_setfunc].<br />
<br />
===Glitch when opening serial port===<br />
When the serial port is opened the voltage on TXD pulses negative for approximately 32 µs (regardless of the baud rate). This pulse may be interpreted as a transmission by a device connected to the TXD pin, which could have unintended effects. An error tolerant communication protocol should be used to avoid problems this glitch could cause. Another method for avoiding problems is to use a GPIO pin to implement the RTS signal, and to have the connected device ignore all data on TXD until RTS is asserted. If the connected device is susceptible to the glitch and cannot be modified, it is sometimes possible to obtain correct operation by opening the serial port in advance of initiating transmission. This can be done in the shell with the sleep program:<br />
sleep infinity >/dev/ttyAMA0 &<br />
In a shell script, the following commands may be used to kill the sleep process once serial transmission is complete.<br />
sleep infinity >/dev/ttyAMA0 &<br />
sleep_pid=$!<br />
disown $sleep_pid # this is required to prevent an error message when sleep is terminated<br />
# Use serial port here<br />
kill $sleep_pid<br />
<br />
{{Template:Raspberry Pi}}</div>Ndemouhttps://elinux.org/index.php?title=RPi_Serial_Connection&diff=410816RPi Serial Connection2016-06-03T13:13:16Z<p>Ndemou: Moving sentence to other section</p>
<hr />
<div>Back to [[RPi Advanced Setup]].<br />
<br />
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:<br />
* 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 Raspberry Pi if the video and network are not available.<br />
* 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 Raspberry Pi to control another device.<br />
<br />
== Connections and signal levels ==<br />
<br />
[[File:Adafruit-connection.jpg|300px|thumb|right|Adafruit serial cable connected to Pi]]<br />
<br />
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.<br />
<br />
The Broadcom chip at the heart of the Raspberry Pi uses 0 and 3.3&nbsp;V logic levels, not the +/-12&nbsp;V 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.3&nbsp;V to RS-232 level converter with a breadboard, a MAX3232CPE IC and five 0.1&nbsp;µF capacitors. <br />
<br />
If you wish to connect your Raspberry Pi to a PC with a USB port, the simplest option is to use a USB-to-serial cable which uses 3.3&nbsp;V logic levels (e.g. the [http://www.adafruit.com/products/954 Adafruit 954] cable, the [http://www.ftdichip.com/Products/Cables/RPi.htm FTDI TTL-232R-RPI] cable, or the [http://www.mysticengineering.com/debug.buddy/pi.usage.html Debug Buddy] ultimate serial port). These can be simply plugged in directly to the GPIO header (see illustration).<br />
<br />
*For using the ''Adafruit 954'' cable on [[Windows]], see [[Adafruit 954 USB serial cable]].<br />
<br />
If you wish to connect to a peripheral which has 0/5&nbsp;V 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 using a ready-made level shifter module. Other circuits for level shifting are shown at [[RPi_GPIO_Interface_Circuits#Level_Shifters]]. The [http://www.mysticengineering.com/debug.buddy Debug Buddy] ultimate serial port can also be configured for 0/5&nbsp;V signals.<br />
<br />
== Connection to a PC ==<br />
<br />
You can connect the Raspberry Pi to a PC using a USB-serial cable, or (if it has an RS-232 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.<br />
<br />
===Console serial parameters===<br />
The following parameters are needed to connect to the Raspberry Pi console, and apply on both Linux and Windows.<br />
<br />
* Speed (baud rate): 115200<br />
* Bits: 8<br />
* Parity: None<br />
* Stop Bits: 1<br />
* Flow Control: None<br />
<br />
===Linux terminal set up===<br />
<br />
If your PC is running Linux, you will need to know the ''port name'' of its serial port:<br />
<br />
* Built-in (standard) Serial Port: the Linux standard is '''/dev/ttyS0''', '''/dev/ttyS1''', and so on <br />
* USB Serial Port Adapter: '''/dev/ttyUSB0''', '''/dev/ttyUSB1''', and so on.<br />
** Some types of USB serial adapter may appear as '''/dev/ttyACM0''' ... <br />
<br />
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:<br />
ls -l /dev/ttyUSB0<br />
and you will see something like "crw-rw----T 1 root dialout ...", ''c'' means character device, and root can 'read,write' and the group ''dialout'' can 'read,write' to the port and everyone else cannot access it.<br />
<br />
To find out if you, the current user, is in the group dialout, use the command:<br />
<br />
id<br />
<br />
If you do not see ''dialout'' listed, add yourself with the command<br />
sudo usermod -a -G dialout ''username''<br />
<br />
You then have a choice of terminal emulation programs:<br />
<br />
* '''Super Easy Way Using GNU Screen'''<br />
<br />
Enter the command below into a terminal window<br />
screen ''port_name'' 115200<br />
<br />
To exit GNU screen, type Control-A k.<br />
<br />
* '''Super Easy Way Using Minicom'''<br />
<br />
Run Minicom with the following parameters:<br />
<br />
minicom -b 115200 -o -D Port_Name<br />
<br />
You can exit Minicom with Control-A x<br />
<br />
* '''Tedious Old-Fashioned Way Using Minicom'''<br />
<br />
Another method to setup ''minicom'' is described in the [http://www.tincantools.com/wiki/Minicom Tincantools Minicom Tutorial]<br />
<br />
<br />
* '''GUI method with GtkTerm'''<br />
<br />
Start ''GtkTerm'', select Configuration->Port and enter the values above in the labeled fields.<br />
<br />
=== Network connection with the point-to-point protocol (ppp)===<br />
<br />
The easiest way to set up a network connection between your Raspberry Pi and another computer is with an Ethernet cable. If this is not possible, as is the case for the Raspberry Pi Model A, you can set up a connection over the serial cable. This uses the Point-to-point Protocol (PPP). A network connection running over a serial cable can be very useful for copying files onto the Raspberry Pi.<br />
<br />
Step 1: Log in to the Raspberry Pi over the serial cable and run the Point-to-Point Protocol Daemon:<br />
sudo pppd noauth<br />
<br />
Some garbage will start appearing in the terminal. This is the cue to quit your terminal program and proceed to step two.<br />
<br />
Step 2: On your local computer, start the Point-to-Point protocol. On a Linux or Mac computer you can do this by typing:<br />
<br />
sudo pppd noauth proxyarp /dev/tty.usbserial-FTGCC2MV 115200 10.0.0.1:10.0.0.2 passive local maxfail 0 nocrtscts xonxoff<br />
<br />
replacing /dev/tty.usbserial-FTGCC2MV with the name of your serial port. In the above line, 115200 is the baud rate of the connection, 10.0.0.1 is the local internet protocol (IP) address, the address you want your computer to have. 10.0.0.2 is the remote IP address, it is the address that the Raspberry Pi will have.<br />
<br />
Test the connection:<br />
<br />
ping 10.0.0.2<br />
<br />
===Virtual connection to the LAN===<br />
Instead of 10.0.0.0/8 you could as well use normal 192.168.0.0/16 addresses;<br />
the first address must be the real address of the local (serving) system.<br />
You can chose the second address; it must not yet be assigned on the LAN (and be outside the DHCP range).<br />
The advantage is that the system connected to the serial line will appear as if it is directly connected to the LAN (arp protocol).<br />
<br />
You must enable routing on the system directly connected to the LAN for other systems to access the system connected to the serial line:<br />
sudo sysctl -w net.ipv4.ip_forward=1<br />
<br />
On the guest system connected via the serial cable you must set the default route pointing to the serving system, e.g.<br />
sudo route add default gateway 192.168.1.21<br />
You should also configure /etc/resolv.conf if you want to use DNS.<br />
<br />
===Windows terminal set-up===<br />
<br />
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 Windows&nbsp;XP and below can choose between using ''PuTTY'' and the built-in ''Hyperterminal.''<br />
<br />
PuTTY users simply need to choose 'serial', select the correct COM port and set the speed, as shown in the dialog below.<br />
<br />
[[File:Putty-settings.png]]<br />
<br />
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'.<br />
<br />
===Boot messages===<br />
<br />
If your connection is set up correctly, when the Raspberry Pi is booted you should see many messages as the system comes up:<br />
Uncompressing Linux... done, booting the kernel.<br />
[ 0.000000] Initializing cgroup subsys cpu<br />
[ 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<br />
[ 0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d<br />
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache<br />
[ 0.000000] Machine: BCM2708<br />
[ 0.000000] Memory policy: ECC disabled, Data cache writeback<br />
[ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 113792<br />
[ 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<br />
<br />
and so on. Eventually, you should see a login prompt:<br />
<br />
Debian GNU/Linux Wheezy/sid raspi2 ttyAMA0<br />
<br />
raspi2 login:<br />
<br />
You can then log in as you would with a keyboard and screen.<br />
<br />
=== Unwanted serial garbage input ===<br />
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.<br />
<br />
==Connection to a microcontroller or other peripheral==<br />
<br />
===H/W considerations===<br />
If your microcontroller or peripheral works with '''5V logic levels''', level conversion is necessary - see 'Connecting to a PC' for details.<br />
<br />
If your microcontroller or peripheral works with '''3.3V logic levels''' then you can connect its TxD, RxD signals directly to the RxD and TxD pins of the Raspberry. However it's probably a good idea to connect the signals with a 2.2 kΩ resistors in series. This will prevent damage if two outputs are accidentally connected together (e.g. if you connect TxD with TxD or if a GPIO input pin is accidentally programmed as output).<br />
<br />
===S/W: Preventing Linux from using the serial port===<br />
<br />
By default Linux will grab the serial port and use it as a terminal. If you want to use it for other purposes you must prevent this. Here are the methods you can use:<br />
<br />
====Method 1, raspi-config (easiest, try this first)====<br />
<br />
Run <code>sudo raspi-config</code> and check if it has the option <code>advanced options</code> -> <code>serial</code>. If it has, set it to disabled and you're done.<br />
<br />
====Method 2, using an existing script (easy)====<br />
<br />
There's a [https://github.com/lurch/rpi-serial-console nice little script] to automate the steps bellow. <br />
<br />
====Method 3, manual configuration (complex)====<br />
<br />
If neither raspi-config nor the script works for you then follow the hard way.<br />
<br />
'''NOTE FOR RASPBERRY PI 3:''' The Raspberry pi 3 has changed things around a bit: ttyAMA0 now refers to the serial port that is connected to the bluetooth. The old serial port is now called ttyS0. So if you have an RPI3, everywhere you see "ttyAMA0" below, you should read "ttyS0".<br />
<br />
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.<br />
<br />
* 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:<br />
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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:<br />
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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.<br />
<br />
* Secondly, after booting, a login prompt appears on the serial port. This is controlled by the following lines in <code>/etc/inittab</code>:<br />
#Spawn a getty on Raspberry Pi serial line<br />
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
You will need to edit this file to comment out the second line, i.e.<br />
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
Finally you will need to reboot the Raspberry 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 won't get any unwanted traffic confusing the attached devices. <br />
<br />
The above instructions have been verified on Raspbian 'Wheezy'; other distributions may be set up differently. To double-check, use<br />
cat /proc/cmdline<br />
to show the current kernel command line, and <br />
ps aux | grep ttyAMA0<br />
to search for <code>getty</code> processes using the serial port.<br />
<br />
A tutorial on accessing the Raspberry Pi's serial port from Python is available at [[Serial_port_programming]].<br />
<br />
===Handshaking lines===<br />
You can have the [[RPi_BCM2835_GPIOs#RTS0|RTS0]] signal on [[RPi_BCM2835_GPIOs#GPIO17|GPIO 17]] (P1-11) or [[RPi_BCM2835_GPIOs#GPIO31|GPIO 31]] (P5-06) if you set them to ALT function 3. Likewise, the [[RPi_BCM2835_GPIOs#CTS0|CTS0]] is available on [[RPi_BCM2835_GPIOs#GPIO30|GPIO 30]] (P5-05), if it is set to ALT function 3. You can control the settings of I/O pins with [https://github.com/rewolff/bw_rpi_tools/tree/master/gpio gpio_setfunc].<br />
<br />
===Glitch when opening serial port===<br />
When the serial port is opened the voltage on TXD pulses negative for approximately 32 µs (regardless of the baud rate). This pulse may be interpreted as a transmission by a device connected to the TXD pin, which could have unintended effects. An error tolerant communication protocol should be used to avoid problems this glitch could cause. Another method for avoiding problems is to use a GPIO pin to implement the RTS signal, and to have the connected device ignore all data on TXD until RTS is asserted. If the connected device is susceptible to the glitch and cannot be modified, it is sometimes possible to obtain correct operation by opening the serial port in advance of initiating transmission. This can be done in the shell with the sleep program:<br />
sleep infinity >/dev/ttyAMA0 &<br />
In a shell script, the following commands may be used to kill the sleep process once serial transmission is complete.<br />
sleep infinity >/dev/ttyAMA0 &<br />
sleep_pid=$!<br />
disown $sleep_pid # this is required to prevent an error message when sleep is terminated<br />
# Use serial port here<br />
kill $sleep_pid<br />
<br />
{{Template:Raspberry Pi}}</div>Ndemouhttps://elinux.org/index.php?title=RPi_Serial_Connection&diff=410811RPi Serial Connection2016-06-03T13:11:32Z<p>Ndemou: formating</p>
<hr />
<div>Back to [[RPi Advanced Setup]].<br />
<br />
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:<br />
* 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 Raspberry Pi if the video and network are not available.<br />
* 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 Raspberry Pi to control another device.<br />
<br />
== Connections and signal levels ==<br />
<br />
[[File:Adafruit-connection.jpg|300px|thumb|right|Adafruit serial cable connected to Pi]]<br />
<br />
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.<br />
<br />
The Broadcom chip at the heart of the Raspberry Pi uses 0 and 3.3&nbsp;V logic levels, not the +/-12&nbsp;V 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.3&nbsp;V to RS-232 level converter with a breadboard, a MAX3232CPE IC and five 0.1&nbsp;µF capacitors. <br />
<br />
If you wish to connect your Raspberry Pi to a PC with a USB port, the simplest option is to use a USB-to-serial cable which uses 3.3&nbsp;V logic levels (e.g. the [http://www.adafruit.com/products/954 Adafruit 954] cable, the [http://www.ftdichip.com/Products/Cables/RPi.htm FTDI TTL-232R-RPI] cable, or the [http://www.mysticengineering.com/debug.buddy/pi.usage.html Debug Buddy] ultimate serial port). These can be simply plugged in directly to the GPIO header (see illustration).<br />
<br />
*For using the ''Adafruit 954'' cable on [[Windows]], see [[Adafruit 954 USB serial cable]].<br />
<br />
If you wish to connect to a peripheral which has 0/5&nbsp;V 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 using a ready-made level shifter module. Other circuits for level shifting are shown at [[RPi_GPIO_Interface_Circuits#Level_Shifters]]. The [http://www.mysticengineering.com/debug.buddy Debug Buddy] ultimate serial port can also be configured for 0/5&nbsp;V signals.<br />
<br />
== Connection to a PC ==<br />
<br />
You can connect the Raspberry Pi to a PC using a USB-serial cable, or (if it has an RS-232 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.<br />
<br />
===Console serial parameters===<br />
The following parameters are needed to connect to the Raspberry Pi console, and apply on both Linux and Windows.<br />
<br />
* Speed (baud rate): 115200<br />
* Bits: 8<br />
* Parity: None<br />
* Stop Bits: 1<br />
* Flow Control: None<br />
<br />
===Linux terminal set up===<br />
<br />
If your PC is running Linux, you will need to know the ''port name'' of its serial port:<br />
<br />
* Built-in (standard) Serial Port: the Linux standard is '''/dev/ttyS0''', '''/dev/ttyS1''', and so on <br />
* USB Serial Port Adapter: '''/dev/ttyUSB0''', '''/dev/ttyUSB1''', and so on.<br />
** Some types of USB serial adapter may appear as '''/dev/ttyACM0''' ... <br />
<br />
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:<br />
ls -l /dev/ttyUSB0<br />
and you will see something like "crw-rw----T 1 root dialout ...", ''c'' means character device, and root can 'read,write' and the group ''dialout'' can 'read,write' to the port and everyone else cannot access it.<br />
<br />
To find out if you, the current user, is in the group dialout, use the command:<br />
<br />
id<br />
<br />
If you do not see ''dialout'' listed, add yourself with the command<br />
sudo usermod -a -G dialout ''username''<br />
<br />
You then have a choice of terminal emulation programs:<br />
<br />
* '''Super Easy Way Using GNU Screen'''<br />
<br />
Enter the command below into a terminal window<br />
screen ''port_name'' 115200<br />
<br />
To exit GNU screen, type Control-A k.<br />
<br />
* '''Super Easy Way Using Minicom'''<br />
<br />
Run Minicom with the following parameters:<br />
<br />
minicom -b 115200 -o -D Port_Name<br />
<br />
You can exit Minicom with Control-A x<br />
<br />
* '''Tedious Old-Fashioned Way Using Minicom'''<br />
<br />
Another method to setup ''minicom'' is described in the [http://www.tincantools.com/wiki/Minicom Tincantools Minicom Tutorial]<br />
<br />
<br />
* '''GUI method with GtkTerm'''<br />
<br />
Start ''GtkTerm'', select Configuration->Port and enter the values above in the labeled fields.<br />
<br />
=== Network connection with the point-to-point protocol (ppp)===<br />
<br />
The easiest way to set up a network connection between your Raspberry Pi and another computer is with an Ethernet cable. If this is not possible, as is the case for the Raspberry Pi Model A, you can set up a connection over the serial cable. This uses the Point-to-point Protocol (PPP). A network connection running over a serial cable can be very useful for copying files onto the Raspberry Pi.<br />
<br />
Step 1: Log in to the Raspberry Pi over the serial cable and run the Point-to-Point Protocol Daemon:<br />
sudo pppd noauth<br />
<br />
Some garbage will start appearing in the terminal. This is the cue to quit your terminal program and proceed to step two.<br />
<br />
Step 2: On your local computer, start the Point-to-Point protocol. On a Linux or Mac computer you can do this by typing:<br />
<br />
sudo pppd noauth proxyarp /dev/tty.usbserial-FTGCC2MV 115200 10.0.0.1:10.0.0.2 passive local maxfail 0 nocrtscts xonxoff<br />
<br />
replacing /dev/tty.usbserial-FTGCC2MV with the name of your serial port. In the above line, 115200 is the baud rate of the connection, 10.0.0.1 is the local internet protocol (IP) address, the address you want your computer to have. 10.0.0.2 is the remote IP address, it is the address that the Raspberry Pi will have.<br />
<br />
Test the connection:<br />
<br />
ping 10.0.0.2<br />
<br />
===Virtual connection to the LAN===<br />
Instead of 10.0.0.0/8 you could as well use normal 192.168.0.0/16 addresses;<br />
the first address must be the real address of the local (serving) system.<br />
You can chose the second address; it must not yet be assigned on the LAN (and be outside the DHCP range).<br />
The advantage is that the system connected to the serial line will appear as if it is directly connected to the LAN (arp protocol).<br />
<br />
You must enable routing on the system directly connected to the LAN for other systems to access the system connected to the serial line:<br />
sudo sysctl -w net.ipv4.ip_forward=1<br />
<br />
On the guest system connected via the serial cable you must set the default route pointing to the serving system, e.g.<br />
sudo route add default gateway 192.168.1.21<br />
You should also configure /etc/resolv.conf if you want to use DNS.<br />
<br />
===Windows terminal set-up===<br />
<br />
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 Windows&nbsp;XP and below can choose between using ''PuTTY'' and the built-in ''Hyperterminal.''<br />
<br />
PuTTY users simply need to choose 'serial', select the correct COM port and set the speed, as shown in the dialog below.<br />
<br />
[[File:Putty-settings.png]]<br />
<br />
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'.<br />
<br />
===Boot messages===<br />
<br />
If your connection is set up correctly, when the Raspberry Pi is booted you should see many messages as the system comes up:<br />
Uncompressing Linux... done, booting the kernel.<br />
[ 0.000000] Initializing cgroup subsys cpu<br />
[ 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<br />
[ 0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d<br />
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache<br />
[ 0.000000] Machine: BCM2708<br />
[ 0.000000] Memory policy: ECC disabled, Data cache writeback<br />
[ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 113792<br />
[ 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<br />
<br />
and so on. Eventually, you should see a login prompt:<br />
<br />
Debian GNU/Linux Wheezy/sid raspi2 ttyAMA0<br />
<br />
raspi2 login:<br />
<br />
You can then log in as you would with a keyboard and screen.<br />
<br />
=== Unwanted serial garbage input ===<br />
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.<br />
<br />
==Connection to a microcontroller or other peripheral==<br />
<br />
===H/W considerations===<br />
If your microcontroller or peripheral works with '''5V logic levels''', level conversion is necessary - see 'Connecting to a PC' for details.<br />
<br />
If your microcontroller or peripheral works with '''3.3V logic levels''' then you can connect its TxD, RxD signals directly to the RxD and TxD pins of the Raspberry. However it's probably a good idea to connect the signals with a 2.2 kΩ resistors in series. This will prevent damage if two outputs are accidentally connected together (e.g. if you connect TxD with TxD or if a GPIO input pin is accidentally programmed as output).<br />
<br />
===S/W: Preventing Linux from using the serial port===<br />
<br />
By default Linux will grab the serial port and use it as a terminal. If you want to use it for other purposes you must prevent this. Here are the methods you can use:<br />
<br />
====Method 1, raspi-config (easiest, try this first)====<br />
<br />
Run <code>sudo raspi-config</code> and check if it has the option <code>advanced options</code> -> <code>serial</code>. If it has, set it to disabled and you're done.<br />
<br />
====Method 2, using an existing script (easy)====<br />
<br />
There's a [https://github.com/lurch/rpi-serial-console nice little script] to automate the steps bellow. <br />
<br />
====Method 3, manual configuration (complex)====<br />
<br />
If neither raspi-config nor the script works for you then follow the hard way.<br />
<br />
'''NOTE FOR RASPBERRY PI 3:''' The Raspberry pi 3 has changed things around a bit: ttyAMA0 now refers to the serial port that is connected to the bluetooth. The old serial port is now called ttyS0. So if you have an RPI3, everywhere you see "ttyAMA0" below, you should read "ttyS0". Also for Raspberry pi3 you might need to add the option <code>enable_uart=1</code> at the end of <code>/boot/config.txt</code> (see this [https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=141195 post by a Pi Engineer])<br />
<br />
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.<br />
<br />
* 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:<br />
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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:<br />
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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.<br />
<br />
* Secondly, after booting, a login prompt appears on the serial port. This is controlled by the following lines in <code>/etc/inittab</code>:<br />
#Spawn a getty on Raspberry Pi serial line<br />
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
You will need to edit this file to comment out the second line, i.e.<br />
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
Finally you will need to reboot the Raspberry 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 won't get any unwanted traffic confusing the attached devices. <br />
<br />
The above instructions have been verified on Raspbian 'Wheezy'; other distributions may be set up differently. To double-check, use<br />
cat /proc/cmdline<br />
to show the current kernel command line, and <br />
ps aux | grep ttyAMA0<br />
to search for <code>getty</code> processes using the serial port.<br />
<br />
A tutorial on accessing the Raspberry Pi's serial port from Python is available at [[Serial_port_programming]].<br />
<br />
===Handshaking lines===<br />
You can have the [[RPi_BCM2835_GPIOs#RTS0|RTS0]] signal on [[RPi_BCM2835_GPIOs#GPIO17|GPIO 17]] (P1-11) or [[RPi_BCM2835_GPIOs#GPIO31|GPIO 31]] (P5-06) if you set them to ALT function 3. Likewise, the [[RPi_BCM2835_GPIOs#CTS0|CTS0]] is available on [[RPi_BCM2835_GPIOs#GPIO30|GPIO 30]] (P5-05), if it is set to ALT function 3. You can control the settings of I/O pins with [https://github.com/rewolff/bw_rpi_tools/tree/master/gpio gpio_setfunc].<br />
<br />
===Glitch when opening serial port===<br />
When the serial port is opened the voltage on TXD pulses negative for approximately 32 µs (regardless of the baud rate). This pulse may be interpreted as a transmission by a device connected to the TXD pin, which could have unintended effects. An error tolerant communication protocol should be used to avoid problems this glitch could cause. Another method for avoiding problems is to use a GPIO pin to implement the RTS signal, and to have the connected device ignore all data on TXD until RTS is asserted. If the connected device is susceptible to the glitch and cannot be modified, it is sometimes possible to obtain correct operation by opening the serial port in advance of initiating transmission. This can be done in the shell with the sleep program:<br />
sleep infinity >/dev/ttyAMA0 &<br />
In a shell script, the following commands may be used to kill the sleep process once serial transmission is complete.<br />
sleep infinity >/dev/ttyAMA0 &<br />
sleep_pid=$!<br />
disown $sleep_pid # this is required to prevent an error message when sleep is terminated<br />
# Use serial port here<br />
kill $sleep_pid<br />
<br />
{{Template:Raspberry Pi}}</div>Ndemouhttps://elinux.org/index.php?title=RPi_Serial_Connection&diff=410791RPi Serial Connection2016-06-03T12:21:03Z<p>Ndemou: </p>
<hr />
<div>Back to [[RPi Advanced Setup]].<br />
<br />
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:<br />
* 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 Raspberry Pi if the video and network are not available.<br />
* 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 Raspberry Pi to control another device.<br />
<br />
== Connections and signal levels ==<br />
<br />
[[File:Adafruit-connection.jpg|300px|thumb|right|Adafruit serial cable connected to Pi]]<br />
<br />
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.<br />
<br />
The Broadcom chip at the heart of the Raspberry Pi uses 0 and 3.3&nbsp;V logic levels, not the +/-12&nbsp;V 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.3&nbsp;V to RS-232 level converter with a breadboard, a MAX3232CPE IC and five 0.1&nbsp;µF capacitors. <br />
<br />
If you wish to connect your Raspberry Pi to a PC with a USB port, the simplest option is to use a USB-to-serial cable which uses 3.3&nbsp;V logic levels (e.g. the [http://www.adafruit.com/products/954 Adafruit 954] cable, the [http://www.ftdichip.com/Products/Cables/RPi.htm FTDI TTL-232R-RPI] cable, or the [http://www.mysticengineering.com/debug.buddy/pi.usage.html Debug Buddy] ultimate serial port). These can be simply plugged in directly to the GPIO header (see illustration).<br />
<br />
*For using the ''Adafruit 954'' cable on [[Windows]], see [[Adafruit 954 USB serial cable]].<br />
<br />
If you wish to connect to a peripheral which has 0/5&nbsp;V 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 using a ready-made level shifter module. Other circuits for level shifting are shown at [[RPi_GPIO_Interface_Circuits#Level_Shifters]]. The [http://www.mysticengineering.com/debug.buddy Debug Buddy] ultimate serial port can also be configured for 0/5&nbsp;V signals.<br />
<br />
== Connection to a PC ==<br />
<br />
You can connect the Raspberry Pi to a PC using a USB-serial cable, or (if it has an RS-232 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.<br />
<br />
===Console serial parameters===<br />
The following parameters are needed to connect to the Raspberry Pi console, and apply on both Linux and Windows.<br />
<br />
* Speed (baud rate): 115200<br />
* Bits: 8<br />
* Parity: None<br />
* Stop Bits: 1<br />
* Flow Control: None<br />
<br />
===Linux terminal set up===<br />
<br />
If your PC is running Linux, you will need to know the ''port name'' of its serial port:<br />
<br />
* Built-in (standard) Serial Port: the Linux standard is '''/dev/ttyS0''', '''/dev/ttyS1''', and so on <br />
* USB Serial Port Adapter: '''/dev/ttyUSB0''', '''/dev/ttyUSB1''', and so on.<br />
** Some types of USB serial adapter may appear as '''/dev/ttyACM0''' ... <br />
<br />
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:<br />
ls -l /dev/ttyUSB0<br />
and you will see something like "crw-rw----T 1 root dialout ...", ''c'' means character device, and root can 'read,write' and the group ''dialout'' can 'read,write' to the port and everyone else cannot access it.<br />
<br />
To find out if you, the current user, is in the group dialout, use the command:<br />
<br />
id<br />
<br />
If you do not see ''dialout'' listed, add yourself with the command<br />
sudo usermod -a -G dialout ''username''<br />
<br />
You then have a choice of terminal emulation programs:<br />
<br />
* '''Super Easy Way Using GNU Screen'''<br />
<br />
Enter the command below into a terminal window<br />
screen ''port_name'' 115200<br />
<br />
To exit GNU screen, type Control-A k.<br />
<br />
* '''Super Easy Way Using Minicom'''<br />
<br />
Run Minicom with the following parameters:<br />
<br />
minicom -b 115200 -o -D Port_Name<br />
<br />
You can exit Minicom with Control-A x<br />
<br />
* '''Tedious Old-Fashioned Way Using Minicom'''<br />
<br />
Another method to setup ''minicom'' is described in the [http://www.tincantools.com/wiki/Minicom Tincantools Minicom Tutorial]<br />
<br />
<br />
* '''GUI method with GtkTerm'''<br />
<br />
Start ''GtkTerm'', select Configuration->Port and enter the values above in the labeled fields.<br />
<br />
=== Network connection with the point-to-point protocol (ppp)===<br />
<br />
The easiest way to set up a network connection between your Raspberry Pi and another computer is with an Ethernet cable. If this is not possible, as is the case for the Raspberry Pi Model A, you can set up a connection over the serial cable. This uses the Point-to-point Protocol (PPP). A network connection running over a serial cable can be very useful for copying files onto the Raspberry Pi.<br />
<br />
Step 1: Log in to the Raspberry Pi over the serial cable and run the Point-to-Point Protocol Daemon:<br />
sudo pppd noauth<br />
<br />
Some garbage will start appearing in the terminal. This is the cue to quit your terminal program and proceed to step two.<br />
<br />
Step 2: On your local computer, start the Point-to-Point protocol. On a Linux or Mac computer you can do this by typing:<br />
<br />
sudo pppd noauth proxyarp /dev/tty.usbserial-FTGCC2MV 115200 10.0.0.1:10.0.0.2 passive local maxfail 0 nocrtscts xonxoff<br />
<br />
replacing /dev/tty.usbserial-FTGCC2MV with the name of your serial port. In the above line, 115200 is the baud rate of the connection, 10.0.0.1 is the local internet protocol (IP) address, the address you want your computer to have. 10.0.0.2 is the remote IP address, it is the address that the Raspberry Pi will have.<br />
<br />
Test the connection:<br />
<br />
ping 10.0.0.2<br />
<br />
===Virtual connection to the LAN===<br />
Instead of 10.0.0.0/8 you could as well use normal 192.168.0.0/16 addresses;<br />
the first address must be the real address of the local (serving) system.<br />
You can chose the second address; it must not yet be assigned on the LAN (and be outside the DHCP range).<br />
The advantage is that the system connected to the serial line will appear as if it is directly connected to the LAN (arp protocol).<br />
<br />
You must enable routing on the system directly connected to the LAN for other systems to access the system connected to the serial line:<br />
sudo sysctl -w net.ipv4.ip_forward=1<br />
<br />
On the guest system connected via the serial cable you must set the default route pointing to the serving system, e.g.<br />
sudo route add default gateway 192.168.1.21<br />
You should also configure /etc/resolv.conf if you want to use DNS.<br />
<br />
===Windows terminal set-up===<br />
<br />
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 Windows&nbsp;XP and below can choose between using ''PuTTY'' and the built-in ''Hyperterminal.''<br />
<br />
PuTTY users simply need to choose 'serial', select the correct COM port and set the speed, as shown in the dialog below.<br />
<br />
[[File:Putty-settings.png]]<br />
<br />
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'.<br />
<br />
===Boot messages===<br />
<br />
If your connection is set up correctly, when the Raspberry Pi is booted you should see many messages as the system comes up:<br />
Uncompressing Linux... done, booting the kernel.<br />
[ 0.000000] Initializing cgroup subsys cpu<br />
[ 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<br />
[ 0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d<br />
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache<br />
[ 0.000000] Machine: BCM2708<br />
[ 0.000000] Memory policy: ECC disabled, Data cache writeback<br />
[ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 113792<br />
[ 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<br />
<br />
and so on. Eventually, you should see a login prompt:<br />
<br />
Debian GNU/Linux Wheezy/sid raspi2 ttyAMA0<br />
<br />
raspi2 login:<br />
<br />
You can then log in as you would with a keyboard and screen.<br />
<br />
=== Unwanted serial garbage input ===<br />
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.<br />
<br />
==Connection to a microcontroller or other peripheral==<br />
<br />
===H/W considerations===<br />
If your microcontroller or peripheral works with '''5V logic levels''', level conversion is necessary - see 'Connecting to a PC' for details.<br />
<br />
If your microcontroller or peripheral works with '''3.3V logic levels''' then you can connect its TxD, RxD signals directly to the RxD and TxD pins of the Raspberry. However it's probably a good idea to connect the signals with a 2.2 kΩ resistors in series. This will prevent damage if two outputs are accidentally connected together (e.g. if you connect TxD with TxD or if a GPIO input pin is accidentally programmed as output).<br />
<br />
===S/W: Preventing Linux from using the serial port===<br />
<br />
By default Linux will grab the serial port and use it as a terminal. If you want to use it for other purposes you must prevent this. Here are the methods you can use:<br />
<br />
====Method 1, raspi-config (easiest, try this first)====<br />
<br />
Run <code>sudo raspi-config</code> and check if it has the option <code>advanced options</code> -> <code>serial</code>. If it has, set it to disabled and you're done.<br />
<br />
====Method 2, using an existing script (easy)====<br />
<br />
There's a [https://github.com/lurch/rpi-serial-console nice little script] to automate the steps bellow. <br />
<br />
====Method 3, manual configuration (complex)====<br />
<br />
If neither raspi-config nor the script works for you then follow the hard way.<br />
<br />
'''NOTE FOR RASPBERRY PI 3:''' The Raspberry pi 3 has changed things around a bit: ttyAMA0 now refers to the serial port that is connected to the bluetooth. The old serial port is now called ttyS0. So if you have an RPI3, everywhere you see "ttyAMA0" below, you should read "ttyS0". Also for Raspberry pi3 you might need to add the option enable_uart=1 at the end of /boot/config.txt (see this post https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=141195)<br />
<br />
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.<br />
<br />
* 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:<br />
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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:<br />
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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.<br />
<br />
* Secondly, after booting, a login prompt appears on the serial port. This is controlled by the following lines in <code>/etc/inittab</code>:<br />
#Spawn a getty on Raspberry Pi serial line<br />
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
You will need to edit this file to comment out the second line, i.e.<br />
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
Finally you will need to reboot the Raspberry 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 won't get any unwanted traffic confusing the attached devices. <br />
<br />
The above instructions have been verified on Raspbian 'Wheezy'; other distributions may be set up differently. To double-check, use<br />
cat /proc/cmdline<br />
to show the current kernel command line, and <br />
ps aux | grep ttyAMA0<br />
to search for <code>getty</code> processes using the serial port.<br />
<br />
A tutorial on accessing the Raspberry Pi's serial port from Python is available at [[Serial_port_programming]].<br />
<br />
===Handshaking lines===<br />
You can have the [[RPi_BCM2835_GPIOs#RTS0|RTS0]] signal on [[RPi_BCM2835_GPIOs#GPIO17|GPIO 17]] (P1-11) or [[RPi_BCM2835_GPIOs#GPIO31|GPIO 31]] (P5-06) if you set them to ALT function 3. Likewise, the [[RPi_BCM2835_GPIOs#CTS0|CTS0]] is available on [[RPi_BCM2835_GPIOs#GPIO30|GPIO 30]] (P5-05), if it is set to ALT function 3. You can control the settings of I/O pins with [https://github.com/rewolff/bw_rpi_tools/tree/master/gpio gpio_setfunc].<br />
<br />
===Glitch when opening serial port===<br />
When the serial port is opened the voltage on TXD pulses negative for approximately 32 µs (regardless of the baud rate). This pulse may be interpreted as a transmission by a device connected to the TXD pin, which could have unintended effects. An error tolerant communication protocol should be used to avoid problems this glitch could cause. Another method for avoiding problems is to use a GPIO pin to implement the RTS signal, and to have the connected device ignore all data on TXD until RTS is asserted. If the connected device is susceptible to the glitch and cannot be modified, it is sometimes possible to obtain correct operation by opening the serial port in advance of initiating transmission. This can be done in the shell with the sleep program:<br />
sleep infinity >/dev/ttyAMA0 &<br />
In a shell script, the following commands may be used to kill the sleep process once serial transmission is complete.<br />
sleep infinity >/dev/ttyAMA0 &<br />
sleep_pid=$!<br />
disown $sleep_pid # this is required to prevent an error message when sleep is terminated<br />
# Use serial port here<br />
kill $sleep_pid<br />
<br />
{{Template:Raspberry Pi}}</div>Ndemouhttps://elinux.org/index.php?title=RPi_GPIO_Interface_Circuits&diff=409816RPi GPIO Interface Circuits2016-05-20T10:44:34Z<p>Ndemou: added a few math and a paragraph with drawbacks</p>
<hr />
<div>This page shows a number of common circuits used for interfacing the Raspberry Pi's GPIO pins to various other electronic circuits. <br />
<br />
<br />
=Input circuits=<br />
<br />
==Buttons and switches==<br />
<br />
[[File:Input_button.png]]<br />
<br />
A simple pushbutton or switch is very easy to connect to a GPIO, as shown above. When the button is pressed (or the switch is closed) the GPIO input will be '0'. When it is released (or the switch is opened) the GPIO will be '1'. R1 is a ''pull-up'' resistor, which "pulls" the GPIO input to 3.3V when the switch is open.<br />
<br />
==Voltage divider==<br />
<br />
[[File:Input_divider.png]]<br />
<br />
This circuit may be used to connect a digital signal from a 5V device to a GPIO pin on the Pi, which requires a 3.3V input level. The '''Ov''' point in the schematic should be connected to one of the Ground lines on the Pi's GPIO connector, and also to the 0v (or Ground) signal on the input device.<br />
<br />
The important thing here is the ''ratio'' of R1 to (R1+R2); the ration should be such that 5V is divided down to 3.3V. In this example the ratio is 0.353 (18/(18+33) so 5V is divided to 3.24V. The values shown here should be suitable for most uses but please check the drawbacks.<br />
<br />
'''Drawbacks'''<br />
<br />
This circuit adds a resistance to the output equal to (R1+R2)/R1*R2 so you have less current to drive a difficult load. In most cases that's not a problem but you may need something better if you want to drive a relatively high load or to get fast switching of a capacitive load. An other detail is that when the output is high you get a very light current leaking to the ground (5V/(R1+R2)~=0.1mA).<br />
<br />
(For more information, see the [http://en.wikipedia.org/wiki/Voltage_divider Wikipedia article] on voltage dividers).<br />
<br />
=Output circuits=<br />
<br />
The GPIO pins are connected ''directly'' to the BCM2835 chip at the heart of the Raspberry Pi. These provide only a 3.3V output level, and are not capable of supplying much power. More importantly, if they are damaged through misuse the Pi itself will need to be replaced.<br />
<br />
So, if you are connecting anything more than a small LED to the GPIO output, you should use an additional circuit to boost the voltage and/or current. <br />
<br />
==Using an NPN transistor==<br />
<br />
[[File:Npn_switch.png]]<br />
<br />
This shows a cheap NPN transistor being used to switch a load on and off. When the GPIO is 'high' (logic 1) the load will be turned on; when it is 'low' the load will be off. The circuit shows a 5V power rail, but with the 2N3904 transistor shown, a supply of up to 40V may be used (this is the V<sub>CEO</sub> value given in the transistor's datasheet). It should be suitable for a load current of up to 100mA.<br />
<br />
For bigger loads, you will need a bigger transistor to switch higher voltages (the V<sub>CEO</sub> value) or currents (the I<sub>C</sub> value), but note that you will need a transistor with a higher ''gain'' (the h<sub>FE</sub> value) as well. The gain is the ratio of the current in R1 (in this case, about 2.5mA), to the current in the load: a 1A load will need a gain of 400. So-called ''Darlington'' transistors, such as the TIP112 (1A, 100V) provide high gain at high currents.<br />
<br />
{| class="wikitable"<br />
! Note! Transistors designed for high power operation generally need a heat sink. The current ratings quoted in the data sheet assume ideal cooling is provided: you will not get anywhere close to these without adequate cooling.<br />
|}<br />
<br />
==Using a FET==<br />
<br />
[[File:Mosfet_switch.png]]<br />
<br />
A ''field-effect transistor'' is another alternative to the ('bipolar') transistor shown above. Again, when the GPIO is 'high' (or 1), the load will be switched on, and 'low' or 0 switches the load off. Resistor R1 is provided for safety; it ensures that the load is switched off if the GPIO is set to be an input.<br />
<br />
As with the bipolar transistor circuit, supply voltages higher than 5V may be used for the load. The 2N7000 device has a maximum (V<sub>DS</sub>) voltage of 60V; it is suitable for load currents of about 100mA.<br />
<br />
For bigger loads, you can choose a bigger FET: the current rating (I<sub>D</sub>) and voltage rating (V<sub>DS</sub>) are given in the data sheet. Unlike bipolar transistors, FETs do not have a 'gain' to worry about. Instead you should look for a 'threshold' voltage (the input voltage at which the FET begins to turn on) of 3V or less, so that it is properly 'on' when driven from the GPIO's 3.3 volts. Some datasheets don't give a threshold voltage - instead they give a value R<sub>DS</sub>(on) measured at 2.5 or 2.7 volts. If the FET isn't suitable for 3.3V logic input, this value won't be given. Fairchild's FQP30N06L has a low threshold voltage (2.5V max) and can switch 32 amps at 60 volts.<br />
<br />
As with bipolar transistors, a power FET will need a good heatsink if you are using it at anywhere near its rated current.<br />
<br />
==Driving a relay==<br />
<br />
[[File:relay_drive.png]]<br />
<br />
If you are connecting an ''inductive'' load (essentially this means anything with windings, such as a relay, a motor, or a solenoid) it is advisable to fit a diode D1 as shown above (note that the ''cathode'' is connected to the positive supply rail). When an inductive load is switched off, a voltage spike is produced which can exceed the maximum rating of transistor Q1; the diode 'clamps' this voltage to prevent damage.<br />
<br />
=Level Shifters=<br />
<br />
Many digital logic circuits (particularly older ones) use a 5 volt power supply. These cannot be connected to the GPIO signals on the Pi, which uses a 3.3V power supply. There are two cases: a) a Pi output driving a peripherals' input b) a peripherals' output driving a Pi input. In case a) the Pi GPIO will output +3.3V for a logic 'high', which is less than the 3.5V (= 0.7 * V<sub>DD</sub>) required by most 5V CMOS logic <ref>See e.g. http://www.allaboutcircuits.com/vol_4/chpt_3/10.html</ref>. In case b) a 5V logic output will send an excessively high voltage into a GPIO input of the Pi, which may damage either or both of them. This problem however can easily be solved with a resistor divider (see above).<br />
<br />
The solution is a ''level shifter'' circuit. Level shifters are available in IC form or as pre-built modules from many suppliers (e.g. Adafruit, Sparkfun, Cool Components), but it's quite easy to make your own.<br />
<br />
== Classic MOSFET level shifter ==<br />
<br />
[[File:Mosfet_level_shift.png]]<br />
<br />
This uses a single MOSFET; when the GPIO is 'low', the MOSFET is turned on (because the gate-source voltage is 3.3V), so the 5V side is connected to the GPIO pin - it is therefore also low. When the GPIO is 'high' the MOSFET is off (the gate-source voltage is near zero), and R2 pulls the 5V side high.<br />
<br />
The circuit also works in the ''other'' direction, and lets you safely drive a 3.3V GPIO input from a 5V source. Q1 has a (hidden) diode between the source and drain terminals - if the 5V logic side is driven low, this will pull the GPIO low through the diode. When the 5V logic side is high, everything is off and the GPIO will be pulled to 3.3V via R1.<br />
<br />
For this reason, the above circuit is often used to convert levels on '''bidirectional buses such as I<sup>2</sup>C'''. Note that for an I<sup>2</sup>C bus, you will need two copies of the circuit, one for the SDA line, and one for SCL. <br />
<br />
== Simple diode circuit ==<br />
<br />
[[File:Diode_level_shift.png]]<br />
<br />
This quick-and-dirty circuit has been used successfully to connect the serial port of a 5V ATMega328 processor (as used on many Arduinos) to the Pi's [[RPi Serial Connection|serial port]]. The 0 and 3.3V logic levels on the Pi's TxD pin are shifted (via D1 and R1) to approximately 0.7V and 4.0V. This is within specification for the ATMega328's RxD pin, so the logic levels are recognised correctly. Note that it is ''only just'' within specification; this means electrical noise (interference) can cause errors more easily than when using a 'proper' level shifter.<br />
<br />
There is no level conversion between the ATMega328's TxD and the Pi's RxD. Instead, resistor R2 limits the current flowing from one device to the other to a safe value (under 0.5mA).<br />
<br />
== SN7407 IC ==<br />
<br />
[[File:7407_shifter.png]]<br />
<br />
The SN7407 open-collector buffer IC is useful for a variety of level shifting and output-driver tasks. When the GPIO output is '0' (low), the 7407 output will be low. When the GPIO is '1', the 7407 output is 'not driven', and R1 will pull the circuit output high. The output-high voltage can be as high as 30V. <br />
<br />
The 7407 output can sink up to 30mA when the output is low, so low-current loads (e.g. LEDs) can be connected directly in place of R1. The IC has six independent circuits in a 14-pin DIP package, and is quite cheap (roughly £0.50).<br />
<br />
-----<br />
<br />
<references /><br />
<br />
'''Note:''' If you wish to produce new or modified schematics matching the ones above, you can use [http://www.inkscape.org Inkscape] using symbols from [http://www.elinux.org/File:Circuit_Symbols.svg Circuit_Symbols.svg].</div>Ndemouhttps://elinux.org/index.php?title=RPi_GPIO_Interface_Circuits&diff=409811RPi GPIO Interface Circuits2016-05-20T10:13:18Z<p>Ndemou: Mostly clarifications of the introduction</p>
<hr />
<div>This page shows a number of common circuits used for interfacing the Raspberry Pi's GPIO pins to various other electronic circuits. <br />
<br />
<br />
=Input circuits=<br />
<br />
==Buttons and switches==<br />
<br />
[[File:Input_button.png]]<br />
<br />
A simple pushbutton or switch is very easy to connect to a GPIO, as shown above. When the button is pressed (or the switch is closed) the GPIO input will be '0'. When it is released (or the switch is opened) the GPIO will be '1'. R1 is a ''pull-up'' resistor, which "pulls" the GPIO input to 3.3V when the switch is open.<br />
<br />
==Voltage divider==<br />
<br />
[[File:Input_divider.png]]<br />
<br />
This circuit may be used to connect a digital signal from a 5V device to a GPIO pin on the Pi, which requires a 3.3V input level. The '''Ov''' point in the schematic should be connected to one of the Ground lines on the Pi's GPIO connector, and also to the 0v (or Ground) signal on the input device.<br />
<br />
The important thing here is the ''ratio'' of R1 to R2; R1 should be just over half R2's value to ensure 5V is divided down to 3.3V. The values shown here should be suitable for most uses.<br />
<br />
(For more information, see the [http://en.wikipedia.org/wiki/Voltage_divider Wikipedia article] on voltage dividers).<br />
<br />
=Output circuits=<br />
<br />
The GPIO pins are connected ''directly'' to the BCM2835 chip at the heart of the Raspberry Pi. These provide only a 3.3V output level, and are not capable of supplying much power. More importantly, if they are damaged through misuse the Pi itself will need to be replaced.<br />
<br />
So, if you are connecting anything more than a small LED to the GPIO output, you should use an additional circuit to boost the voltage and/or current. <br />
<br />
==Using an NPN transistor==<br />
<br />
[[File:Npn_switch.png]]<br />
<br />
This shows a cheap NPN transistor being used to switch a load on and off. When the GPIO is 'high' (logic 1) the load will be turned on; when it is 'low' the load will be off. The circuit shows a 5V power rail, but with the 2N3904 transistor shown, a supply of up to 40V may be used (this is the V<sub>CEO</sub> value given in the transistor's datasheet). It should be suitable for a load current of up to 100mA.<br />
<br />
For bigger loads, you will need a bigger transistor to switch higher voltages (the V<sub>CEO</sub> value) or currents (the I<sub>C</sub> value), but note that you will need a transistor with a higher ''gain'' (the h<sub>FE</sub> value) as well. The gain is the ratio of the current in R1 (in this case, about 2.5mA), to the current in the load: a 1A load will need a gain of 400. So-called ''Darlington'' transistors, such as the TIP112 (1A, 100V) provide high gain at high currents.<br />
<br />
{| class="wikitable"<br />
! Note! Transistors designed for high power operation generally need a heat sink. The current ratings quoted in the data sheet assume ideal cooling is provided: you will not get anywhere close to these without adequate cooling.<br />
|}<br />
<br />
==Using a FET==<br />
<br />
[[File:Mosfet_switch.png]]<br />
<br />
A ''field-effect transistor'' is another alternative to the ('bipolar') transistor shown above. Again, when the GPIO is 'high' (or 1), the load will be switched on, and 'low' or 0 switches the load off. Resistor R1 is provided for safety; it ensures that the load is switched off if the GPIO is set to be an input.<br />
<br />
As with the bipolar transistor circuit, supply voltages higher than 5V may be used for the load. The 2N7000 device has a maximum (V<sub>DS</sub>) voltage of 60V; it is suitable for load currents of about 100mA.<br />
<br />
For bigger loads, you can choose a bigger FET: the current rating (I<sub>D</sub>) and voltage rating (V<sub>DS</sub>) are given in the data sheet. Unlike bipolar transistors, FETs do not have a 'gain' to worry about. Instead you should look for a 'threshold' voltage (the input voltage at which the FET begins to turn on) of 3V or less, so that it is properly 'on' when driven from the GPIO's 3.3 volts. Some datasheets don't give a threshold voltage - instead they give a value R<sub>DS</sub>(on) measured at 2.5 or 2.7 volts. If the FET isn't suitable for 3.3V logic input, this value won't be given. Fairchild's FQP30N06L has a low threshold voltage (2.5V max) and can switch 32 amps at 60 volts.<br />
<br />
As with bipolar transistors, a power FET will need a good heatsink if you are using it at anywhere near its rated current.<br />
<br />
==Driving a relay==<br />
<br />
[[File:relay_drive.png]]<br />
<br />
If you are connecting an ''inductive'' load (essentially this means anything with windings, such as a relay, a motor, or a solenoid) it is advisable to fit a diode D1 as shown above (note that the ''cathode'' is connected to the positive supply rail). When an inductive load is switched off, a voltage spike is produced which can exceed the maximum rating of transistor Q1; the diode 'clamps' this voltage to prevent damage.<br />
<br />
=Level Shifters=<br />
<br />
Many digital logic circuits (particularly older ones) use a 5 volt power supply. These cannot be connected to the GPIO signals on the Pi, which uses a 3.3V power supply. There are two cases: a) a Pi output driving a peripherals' input b) a peripherals' output driving a Pi input. In case a) the Pi GPIO will output +3.3V for a logic 'high', which is less than the 3.5V (= 0.7 * V<sub>DD</sub>) required by most 5V CMOS logic <ref>See e.g. http://www.allaboutcircuits.com/vol_4/chpt_3/10.html</ref>. In case b) a 5V logic output will send an excessively high voltage into a GPIO input of the Pi, which may damage either or both of them. This problem however can easily be solved with a resistor divider (see above).<br />
<br />
The solution is a ''level shifter'' circuit. Level shifters are available in IC form or as pre-built modules from many suppliers (e.g. Adafruit, Sparkfun, Cool Components), but it's quite easy to make your own.<br />
<br />
== Classic MOSFET level shifter ==<br />
<br />
[[File:Mosfet_level_shift.png]]<br />
<br />
This uses a single MOSFET; when the GPIO is 'low', the MOSFET is turned on (because the gate-source voltage is 3.3V), so the 5V side is connected to the GPIO pin - it is therefore also low. When the GPIO is 'high' the MOSFET is off (the gate-source voltage is near zero), and R2 pulls the 5V side high.<br />
<br />
The circuit also works in the ''other'' direction, and lets you safely drive a 3.3V GPIO input from a 5V source. Q1 has a (hidden) diode between the source and drain terminals - if the 5V logic side is driven low, this will pull the GPIO low through the diode. When the 5V logic side is high, everything is off and the GPIO will be pulled to 3.3V via R1.<br />
<br />
For this reason, the above circuit is often used to convert levels on '''bidirectional buses such as I<sup>2</sup>C'''. Note that for an I<sup>2</sup>C bus, you will need two copies of the circuit, one for the SDA line, and one for SCL. <br />
<br />
== Simple diode circuit ==<br />
<br />
[[File:Diode_level_shift.png]]<br />
<br />
This quick-and-dirty circuit has been used successfully to connect the serial port of a 5V ATMega328 processor (as used on many Arduinos) to the Pi's [[RPi Serial Connection|serial port]]. The 0 and 3.3V logic levels on the Pi's TxD pin are shifted (via D1 and R1) to approximately 0.7V and 4.0V. This is within specification for the ATMega328's RxD pin, so the logic levels are recognised correctly. Note that it is ''only just'' within specification; this means electrical noise (interference) can cause errors more easily than when using a 'proper' level shifter.<br />
<br />
There is no level conversion between the ATMega328's TxD and the Pi's RxD. Instead, resistor R2 limits the current flowing from one device to the other to a safe value (under 0.5mA).<br />
<br />
== SN7407 IC ==<br />
<br />
[[File:7407_shifter.png]]<br />
<br />
The SN7407 open-collector buffer IC is useful for a variety of level shifting and output-driver tasks. When the GPIO output is '0' (low), the 7407 output will be low. When the GPIO is '1', the 7407 output is 'not driven', and R1 will pull the circuit output high. The output-high voltage can be as high as 30V. <br />
<br />
The 7407 output can sink up to 30mA when the output is low, so low-current loads (e.g. LEDs) can be connected directly in place of R1. The IC has six independent circuits in a 14-pin DIP package, and is quite cheap (roughly £0.50).<br />
<br />
-----<br />
<br />
<references /><br />
<br />
'''Note:''' If you wish to produce new or modified schematics matching the ones above, you can use [http://www.inkscape.org Inkscape] using symbols from [http://www.elinux.org/File:Circuit_Symbols.svg Circuit_Symbols.svg].</div>Ndemouhttps://elinux.org/index.php?title=Thread:Talk:RPi_Serial_Connection/Create_a_new_page_re:_the_manual_configuration_needed_to_prevent_Linux_from_using_the_serial_port&diff=409806Thread:Talk:RPi Serial Connection/Create a new page re: the manual configuration needed to prevent Linux from using the serial port2016-05-20T08:48:23Z<p>Ndemou: New thread: Create a new page re: the manual configuration needed to prevent Linux from using the serial port</p>
<hr />
<div>I suggest to create a new page regarding the manual configuration needed to prevent Linux from using the serial port and place a link here because a) the text of that section is by far much longer than the other 2 methods b) almost everybody will use the other 2 methods and very-very few the manual one</div>Ndemouhttps://elinux.org/index.php?title=RPi_Serial_Connection&diff=409801RPi Serial Connection2016-05-20T08:41:23Z<p>Ndemou: Cleaned up first paragraph and added heading</p>
<hr />
<div>Back to [[RPi Advanced Setup]].<br />
<br />
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:<br />
* 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 Raspberry Pi if the video and network are not available.<br />
* 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 Raspberry Pi to control another device.<br />
<br />
== Connections and signal levels ==<br />
<br />
[[File:Adafruit-connection.jpg|300px|thumb|right|Adafruit serial cable connected to Pi]]<br />
<br />
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.<br />
<br />
The Broadcom chip at the heart of the Raspberry Pi uses 0 and 3.3&nbsp;V logic levels, not the +/-12&nbsp;V 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.3&nbsp;V to RS-232 level converter with a breadboard, a MAX3232CPE IC and five 0.1&nbsp;µF capacitors. <br />
<br />
If you wish to connect your Raspberry Pi to a PC with a USB port, the simplest option is to use a USB-to-serial cable which uses 3.3&nbsp;V logic levels (e.g. the [http://www.adafruit.com/products/954 Adafruit 954] cable, the [http://www.ftdichip.com/Products/Cables/RPi.htm FTDI TTL-232R-RPI] cable, or the [http://www.mysticengineering.com/debug.buddy/pi.usage.html Debug Buddy] ultimate serial port). These can be simply plugged in directly to the GPIO header (see illustration).<br />
<br />
*For using the ''Adafruit 954'' cable on [[Windows]], see [[Adafruit 954 USB serial cable]].<br />
<br />
If you wish to connect to a peripheral which has 0/5&nbsp;V 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 using a ready-made level shifter module. Other circuits for level shifting are shown at [[RPi_GPIO_Interface_Circuits#Level_Shifters]]. The [http://www.mysticengineering.com/debug.buddy Debug Buddy] ultimate serial port can also be configured for 0/5&nbsp;V signals.<br />
<br />
== Connection to a PC ==<br />
<br />
You can connect the Raspberry Pi to a PC using a USB-serial cable, or (if it has an RS-232 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.<br />
<br />
===Console serial parameters===<br />
The following parameters are needed to connect to the Raspberry Pi console, and apply on both Linux and Windows.<br />
<br />
* Speed (baud rate): 115200<br />
* Bits: 8<br />
* Parity: None<br />
* Stop Bits: 1<br />
* Flow Control: None<br />
<br />
===Linux terminal set up===<br />
<br />
If your PC is running Linux, you will need to know the ''port name'' of its serial port:<br />
<br />
* Built-in (standard) Serial Port: the Linux standard is '''/dev/ttyS0''', '''/dev/ttyS1''', and so on <br />
* USB Serial Port Adapter: '''/dev/ttyUSB0''', '''/dev/ttyUSB1''', and so on.<br />
** Some types of USB serial adapter may appear as '''/dev/ttyACM0''' ... <br />
<br />
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:<br />
ls -l /dev/ttyUSB0<br />
and you will see something like "crw-rw----T 1 root dialout ...", ''c'' means character device, and root can 'read,write' and the group ''dialout'' can 'read,write' to the port and everyone else cannot access it.<br />
<br />
To find out if you, the current user, is in the group dialout, use the command:<br />
<br />
id<br />
<br />
If you do not see ''dialout'' listed, add yourself with the command<br />
sudo usermod -a -G dialout ''username''<br />
<br />
You then have a choice of terminal emulation programs:<br />
<br />
* '''Super Easy Way Using GNU Screen'''<br />
<br />
Enter the command below into a terminal window<br />
screen ''port_name'' 115200<br />
<br />
To exit GNU screen, type Control-A k.<br />
<br />
* '''Super Easy Way Using Minicom'''<br />
<br />
Run Minicom with the following parameters:<br />
<br />
minicom -b 115200 -o -D Port_Name<br />
<br />
You can exit Minicom with Control-A x<br />
<br />
* '''Tedious Old-Fashioned Way Using Minicom'''<br />
<br />
Another method to setup ''minicom'' is described in the [http://www.tincantools.com/wiki/Minicom Tincantools Minicom Tutorial]<br />
<br />
<br />
* '''GUI method with GtkTerm'''<br />
<br />
Start ''GtkTerm'', select Configuration->Port and enter the values above in the labeled fields.<br />
<br />
=== Network connection with the point-to-point protocol (ppp)===<br />
<br />
The easiest way to set up a network connection between your Raspberry Pi and another computer is with an Ethernet cable. If this is not possible, as is the case for the Raspberry Pi Model A, you can set up a connection over the serial cable. This uses the Point-to-point Protocol (PPP). A network connection running over a serial cable can be very useful for copying files onto the Raspberry Pi.<br />
<br />
Step 1: Log in to the Raspberry Pi over the serial cable and run the Point-to-Point Protocol Daemon:<br />
sudo pppd noauth<br />
<br />
Some garbage will start appearing in the terminal. This is the cue to quit your terminal program and proceed to step two.<br />
<br />
Step 2: On your local computer, start the Point-to-Point protocol. On a Linux or Mac computer you can do this by typing:<br />
<br />
sudo pppd noauth proxyarp /dev/tty.usbserial-FTGCC2MV 115200 10.0.0.1:10.0.0.2 passive local maxfail 0 nocrtscts xonxoff<br />
<br />
replacing /dev/tty.usbserial-FTGCC2MV with the name of your serial port. In the above line, 115200 is the baud rate of the connection, 10.0.0.1 is the local internet protocol (IP) address, the address you want your computer to have. 10.0.0.2 is the remote IP address, it is the address that the Raspberry Pi will have.<br />
<br />
Test the connection:<br />
<br />
ping 10.0.0.2<br />
<br />
===Virtual connection to the LAN===<br />
Instead of 10.0.0.0/8 you could as well use normal 192.168.0.0/16 addresses;<br />
the first address must be the real address of the local (serving) system.<br />
You can chose the second address; it must not yet be assigned on the LAN (and be outside the DHCP range).<br />
The advantage is that the system connected to the serial line will appear as if it is directly connected to the LAN (arp protocol).<br />
<br />
You must enable routing on the system directly connected to the LAN for other systems to access the system connected to the serial line:<br />
sudo sysctl -w net.ipv4.ip_forward=1<br />
<br />
On the guest system connected via the serial cable you must set the default route pointing to the serving system, e.g.<br />
sudo route add default gateway 192.168.1.21<br />
You should also configure /etc/resolv.conf if you want to use DNS.<br />
<br />
===Windows terminal set-up===<br />
<br />
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 Windows&nbsp;XP and below can choose between using ''PuTTY'' and the built-in ''Hyperterminal.''<br />
<br />
PuTTY users simply need to choose 'serial', select the correct COM port and set the speed, as shown in the dialog below.<br />
<br />
[[File:Putty-settings.png]]<br />
<br />
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'.<br />
<br />
===Boot messages===<br />
<br />
If your connection is set up correctly, when the Raspberry Pi is booted you should see many messages as the system comes up:<br />
Uncompressing Linux... done, booting the kernel.<br />
[ 0.000000] Initializing cgroup subsys cpu<br />
[ 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<br />
[ 0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d<br />
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache<br />
[ 0.000000] Machine: BCM2708<br />
[ 0.000000] Memory policy: ECC disabled, Data cache writeback<br />
[ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 113792<br />
[ 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<br />
<br />
and so on. Eventually, you should see a login prompt:<br />
<br />
Debian GNU/Linux Wheezy/sid raspi2 ttyAMA0<br />
<br />
raspi2 login:<br />
<br />
You can then log in as you would with a keyboard and screen.<br />
<br />
=== Unwanted serial garbage input ===<br />
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.<br />
<br />
==Connection to a microcontroller or other peripheral==<br />
<br />
===H/W considerations===<br />
If your microcontroller or peripheral works with '''5V logic levels''', level conversion is necessary - see 'Connecting to a PC' for details.<br />
<br />
If your microcontroller or peripheral works with '''3.3V logic levels''' then you can connect its TxD, RxD signals directly to the RxD and TxD pins of the Raspberry. However it's probably a good idea to connect the signals with a 2.2 kΩ resistors in series. This will prevent damage if two outputs are accidentally connected together (e.g. if you connect TxD with TxD or if a GPIO input pin is accidentally programmed as output).<br />
<br />
===S/W: Preventing Linux from using the serial port===<br />
<br />
By default Linux will grab the serial port and use it as a terminal. If you want to use it for other purposes you must prevent this. Here are the methods you can use:<br />
<br />
====Method 1, raspi-config (easiest, try this first)====<br />
<br />
Run <code>sudo raspi-config</code> and check if it has the option <code>advanced options</code> -> <code>serial</code>. If it has, set it to disabled and you're done.<br />
<br />
====Method 2, using an existing script (easy)====<br />
<br />
There's a [https://github.com/lurch/rpi-serial-console nice little script] to automate the steps bellow. <br />
<br />
====Method 3, manual configuration (complex)====<br />
<br />
If neither raspi-config nor the script works for you then follow the hard way.<br />
<br />
'''NOTE FOR RASPBERRY PI 3:''' The raspberry pi 3 has changed things around a bit: ttyAMA0 now refers to the serial port that is connected to the bluetooth. The old serial port is now called ttyS0. So if you have an RPI3, everywhere you see "ttyAMA0" below, you should read "ttyS0". <br />
<br />
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.<br />
<br />
* 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:<br />
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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:<br />
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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.<br />
<br />
* Secondly, after booting, a login prompt appears on the serial port. This is controlled by the following lines in <code>/etc/inittab</code>:<br />
#Spawn a getty on Raspberry Pi serial line<br />
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
You will need to edit this file to comment out the second line, i.e.<br />
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
Finally you will need to reboot the Raspberry 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 won't get any unwanted traffic confusing the attached devices. <br />
<br />
The above instructions have been verified on Raspbian 'Wheezy'; other distributions may be set up differently. To double-check, use<br />
cat /proc/cmdline<br />
to show the current kernel command line, and <br />
ps aux | grep ttyAMA0<br />
to search for <code>getty</code> processes using the serial port.<br />
<br />
A tutorial on accessing the Raspberry Pi's serial port from Python is available at [[Serial_port_programming]].<br />
<br />
===Handshaking lines===<br />
You can have the [[RPi_BCM2835_GPIOs#RTS0|RTS0]] signal on [[RPi_BCM2835_GPIOs#GPIO17|GPIO 17]] (P1-11) or [[RPi_BCM2835_GPIOs#GPIO31|GPIO 31]] (P5-06) if you set them to ALT function 3. Likewise, the [[RPi_BCM2835_GPIOs#CTS0|CTS0]] is available on [[RPi_BCM2835_GPIOs#GPIO30|GPIO 30]] (P5-05), if it is set to ALT function 3. You can control the settings of I/O pins with [https://github.com/rewolff/bw_rpi_tools/tree/master/gpio gpio_setfunc].<br />
<br />
===Glitch when opening serial port===<br />
When the serial port is opened the voltage on TXD pulses negative for approximately 32 µs (regardless of the baud rate). This pulse may be interpreted as a transmission by a device connected to the TXD pin, which could have unintended effects. An error tolerant communication protocol should be used to avoid problems this glitch could cause. Another method for avoiding problems is to use a GPIO pin to implement the RTS signal, and to have the connected device ignore all data on TXD until RTS is asserted. If the connected device is susceptible to the glitch and cannot be modified, it is sometimes possible to obtain correct operation by opening the serial port in advance of initiating transmission. This can be done in the shell with the sleep program:<br />
sleep infinity >/dev/ttyAMA0 &<br />
In a shell script, the following commands may be used to kill the sleep process once serial transmission is complete.<br />
sleep infinity >/dev/ttyAMA0 &<br />
sleep_pid=$!<br />
disown $sleep_pid # this is required to prevent an error message when sleep is terminated<br />
# Use serial port here<br />
kill $sleep_pid<br />
<br />
{{Template:Raspberry Pi}}</div>Ndemouhttps://elinux.org/index.php?title=RPi_Serial_Connection&diff=409796RPi Serial Connection2016-05-20T08:28:14Z<p>Ndemou: Added method using raspi-config and a few minor changes to make the text easy to follow</p>
<hr />
<div>Back to [[RPi Advanced Setup]].<br />
<br />
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:<br />
* 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 Raspberry Pi if the video and network are not available.<br />
* 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 Raspberry Pi to control another device.<br />
<br />
== Connections and signal levels ==<br />
<br />
[[File:Adafruit-connection.jpg|300px|thumb|right|Adafruit serial cable connected to Pi]]<br />
<br />
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.<br />
<br />
The Broadcom chip at the heart of the Raspberry Pi uses 0 and 3.3&nbsp;V logic levels, not the +/-12&nbsp;V 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.3&nbsp;V to RS-232 level converter with a breadboard, a MAX3232CPE IC and five 0.1&nbsp;µF capacitors. <br />
<br />
If you wish to connect your Raspberry Pi to a PC with a USB port, the simplest option is to use a USB-to-serial cable which uses 3.3&nbsp;V logic levels (e.g. the [http://www.adafruit.com/products/954 Adafruit 954] cable, the [http://www.ftdichip.com/Products/Cables/RPi.htm FTDI TTL-232R-RPI] cable, or the [http://www.mysticengineering.com/debug.buddy/pi.usage.html Debug Buddy] ultimate serial port). These can be simply plugged in directly to the GPIO header (see illustration).<br />
<br />
*For using the ''Adafruit 954'' cable on [[Windows]], see [[Adafruit 954 USB serial cable]].<br />
<br />
If you wish to connect to a peripheral which has 0/5&nbsp;V 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 using a ready-made level shifter module. Other circuits for level shifting are shown at [[RPi_GPIO_Interface_Circuits#Level_Shifters]]. The [http://www.mysticengineering.com/debug.buddy Debug Buddy] ultimate serial port can also be configured for 0/5&nbsp;V signals.<br />
<br />
== Connection to a PC ==<br />
<br />
You can connect the Raspberry Pi to a PC using a USB-serial cable, or (if it has an RS-232 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.<br />
<br />
===Console serial parameters===<br />
The following parameters are needed to connect to the Raspberry Pi console, and apply on both Linux and Windows.<br />
<br />
* Speed (baud rate): 115200<br />
* Bits: 8<br />
* Parity: None<br />
* Stop Bits: 1<br />
* Flow Control: None<br />
<br />
===Linux terminal set up===<br />
<br />
If your PC is running Linux, you will need to know the ''port name'' of its serial port:<br />
<br />
* Built-in (standard) Serial Port: the Linux standard is '''/dev/ttyS0''', '''/dev/ttyS1''', and so on <br />
* USB Serial Port Adapter: '''/dev/ttyUSB0''', '''/dev/ttyUSB1''', and so on.<br />
** Some types of USB serial adapter may appear as '''/dev/ttyACM0''' ... <br />
<br />
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:<br />
ls -l /dev/ttyUSB0<br />
and you will see something like "crw-rw----T 1 root dialout ...", ''c'' means character device, and root can 'read,write' and the group ''dialout'' can 'read,write' to the port and everyone else cannot access it.<br />
<br />
To find out if you, the current user, is in the group dialout, use the command:<br />
<br />
id<br />
<br />
If you do not see ''dialout'' listed, add yourself with the command<br />
sudo usermod -a -G dialout ''username''<br />
<br />
You then have a choice of terminal emulation programs:<br />
<br />
* '''Super Easy Way Using GNU Screen'''<br />
<br />
Enter the command below into a terminal window<br />
screen ''port_name'' 115200<br />
<br />
To exit GNU screen, type Control-A k.<br />
<br />
* '''Super Easy Way Using Minicom'''<br />
<br />
Run Minicom with the following parameters:<br />
<br />
minicom -b 115200 -o -D Port_Name<br />
<br />
You can exit Minicom with Control-A x<br />
<br />
* '''Tedious Old-Fashioned Way Using Minicom'''<br />
<br />
Another method to setup ''minicom'' is described in the [http://www.tincantools.com/wiki/Minicom Tincantools Minicom Tutorial]<br />
<br />
<br />
* '''GUI method with GtkTerm'''<br />
<br />
Start ''GtkTerm'', select Configuration->Port and enter the values above in the labeled fields.<br />
<br />
=== Network connection with the point-to-point protocol (ppp)===<br />
<br />
The easiest way to set up a network connection between your Raspberry Pi and another computer is with an Ethernet cable. If this is not possible, as is the case for the Raspberry Pi Model A, you can set up a connection over the serial cable. This uses the Point-to-point Protocol (PPP). A network connection running over a serial cable can be very useful for copying files onto the Raspberry Pi.<br />
<br />
Step 1: Log in to the Raspberry Pi over the serial cable and run the Point-to-Point Protocol Daemon:<br />
sudo pppd noauth<br />
<br />
Some garbage will start appearing in the terminal. This is the cue to quit your terminal program and proceed to step two.<br />
<br />
Step 2: On your local computer, start the Point-to-Point protocol. On a Linux or Mac computer you can do this by typing:<br />
<br />
sudo pppd noauth proxyarp /dev/tty.usbserial-FTGCC2MV 115200 10.0.0.1:10.0.0.2 passive local maxfail 0 nocrtscts xonxoff<br />
<br />
replacing /dev/tty.usbserial-FTGCC2MV with the name of your serial port. In the above line, 115200 is the baud rate of the connection, 10.0.0.1 is the local internet protocol (IP) address, the address you want your computer to have. 10.0.0.2 is the remote IP address, it is the address that the Raspberry Pi will have.<br />
<br />
Test the connection:<br />
<br />
ping 10.0.0.2<br />
<br />
===Virtual connection to the LAN===<br />
Instead of 10.0.0.0/8 you could as well use normal 192.168.0.0/16 addresses;<br />
the first address must be the real address of the local (serving) system.<br />
You can chose the second address; it must not yet be assigned on the LAN (and be outside the DHCP range).<br />
The advantage is that the system connected to the serial line will appear as if it is directly connected to the LAN (arp protocol).<br />
<br />
You must enable routing on the system directly connected to the LAN for other systems to access the system connected to the serial line:<br />
sudo sysctl -w net.ipv4.ip_forward=1<br />
<br />
On the guest system connected via the serial cable you must set the default route pointing to the serving system, e.g.<br />
sudo route add default gateway 192.168.1.21<br />
You should also configure /etc/resolv.conf if you want to use DNS.<br />
<br />
===Windows terminal set-up===<br />
<br />
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 Windows&nbsp;XP and below can choose between using ''PuTTY'' and the built-in ''Hyperterminal.''<br />
<br />
PuTTY users simply need to choose 'serial', select the correct COM port and set the speed, as shown in the dialog below.<br />
<br />
[[File:Putty-settings.png]]<br />
<br />
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'.<br />
<br />
===Boot messages===<br />
<br />
If your connection is set up correctly, when the Raspberry Pi is booted you should see many messages as the system comes up:<br />
Uncompressing Linux... done, booting the kernel.<br />
[ 0.000000] Initializing cgroup subsys cpu<br />
[ 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<br />
[ 0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d<br />
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache<br />
[ 0.000000] Machine: BCM2708<br />
[ 0.000000] Memory policy: ECC disabled, Data cache writeback<br />
[ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 113792<br />
[ 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<br />
<br />
and so on. Eventually, you should see a login prompt:<br />
<br />
Debian GNU/Linux Wheezy/sid raspi2 ttyAMA0<br />
<br />
raspi2 login:<br />
<br />
You can then log in as you would with a keyboard and screen.<br />
<br />
=== Unwanted serial garbage input ===<br />
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.<br />
<br />
==Connection to a microcontroller or other peripheral==<br />
<br />
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 3.3&nbsp;V levels. Some Arduino variants use 5&nbsp;V levels, and should have level conversion (see 'Connections and Signal levels' above). Even at 3.3&nbsp;V levels, it's still a good idea to put 2.2 kΩ 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.<br />
<br />
If your microcontroller uses 5&nbsp;V logic levels, level conversion is usually necessary - see 'Connecting to a PC' for details.<br />
<br />
===Preventing Linux from using the serial port===<br />
<br />
By default Linux will grab the serial port and use it as a terminal. If you want to use it for other purposes you must prevent this. Here are the methods you can use:<br />
<br />
====Method 1, raspi-config (easiest, try this first)====<br />
<br />
Run <code>sudo raspi-config</code> and check if it has the option <code>advanced options</code> -> <code>serial</code>. If it has, set it to disabled and you're done.<br />
<br />
====Method 2, using an existing script (easy)====<br />
<br />
There's a [https://github.com/lurch/rpi-serial-console nice little script] to automate the steps bellow. <br />
<br />
====Method 3, manual configuration (complex)====<br />
<br />
If neither raspi-config nor the script works for you then follow the hard way.<br />
<br />
'''NOTE FOR RASPBERRY PI 3:''' The raspberry pi 3 has changed things around a bit: ttyAMA0 now refers to the serial port that is connected to the bluetooth. The old serial port is now called ttyS0. So if you have an RPI3, everywhere you see "ttyAMA0" below, you should read "ttyS0". <br />
<br />
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.<br />
<br />
* 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:<br />
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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:<br />
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait<br />
<br />
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.<br />
<br />
* Secondly, after booting, a login prompt appears on the serial port. This is controlled by the following lines in <code>/etc/inittab</code>:<br />
#Spawn a getty on Raspberry Pi serial line<br />
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
You will need to edit this file to comment out the second line, i.e.<br />
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100<br />
<br />
Finally you will need to reboot the Raspberry 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 won't get any unwanted traffic confusing the attached devices. <br />
<br />
The above instructions have been verified on Raspbian 'Wheezy'; other distributions may be set up differently. To double-check, use<br />
cat /proc/cmdline<br />
to show the current kernel command line, and <br />
ps aux | grep ttyAMA0<br />
to search for <code>getty</code> processes using the serial port.<br />
<br />
A tutorial on accessing the Raspberry Pi's serial port from Python is available at [[Serial_port_programming]].<br />
<br />
===Handshaking lines===<br />
You can have the [[RPi_BCM2835_GPIOs#RTS0|RTS0]] signal on [[RPi_BCM2835_GPIOs#GPIO17|GPIO 17]] (P1-11) or [[RPi_BCM2835_GPIOs#GPIO31|GPIO 31]] (P5-06) if you set them to ALT function 3. Likewise, the [[RPi_BCM2835_GPIOs#CTS0|CTS0]] is available on [[RPi_BCM2835_GPIOs#GPIO30|GPIO 30]] (P5-05), if it is set to ALT function 3. You can control the settings of I/O pins with [https://github.com/rewolff/bw_rpi_tools/tree/master/gpio gpio_setfunc].<br />
<br />
===Glitch when opening serial port===<br />
When the serial port is opened the voltage on TXD pulses negative for approximately 32 µs (regardless of the baud rate). This pulse may be interpreted as a transmission by a device connected to the TXD pin, which could have unintended effects. An error tolerant communication protocol should be used to avoid problems this glitch could cause. Another method for avoiding problems is to use a GPIO pin to implement the RTS signal, and to have the connected device ignore all data on TXD until RTS is asserted. If the connected device is susceptible to the glitch and cannot be modified, it is sometimes possible to obtain correct operation by opening the serial port in advance of initiating transmission. This can be done in the shell with the sleep program:<br />
sleep infinity >/dev/ttyAMA0 &<br />
In a shell script, the following commands may be used to kill the sleep process once serial transmission is complete.<br />
sleep infinity >/dev/ttyAMA0 &<br />
sleep_pid=$!<br />
disown $sleep_pid # this is required to prevent an error message when sleep is terminated<br />
# Use serial port here<br />
kill $sleep_pid<br />
<br />
{{Template:Raspberry Pi}}</div>Ndemou