loading

In this tutorial we will see how to use the serial port on Raspberry Pi. We will use the serial port available on Raspberry with a RS232/TTL 3-5,5V adapter and a USB-serial adapter. By default the Raspberry Pi’s serial port is configured to be used for console input/output. This can help to fix problems during boot, or to log in to the Pi if the video and network are not available.

To be able to use the serial port to connect and talk to other devices (e.g. a modem a printer.. ), the serial port console login needs to be disabled.

Here we use Raspberry Pi 2, and we connect a RS232/TTL 3-5,5V adapter to pins 4 (5V), 6 (GND) ,8 (TX),10 (RX) of Raspberry, obviously connect tx with rx and vice versa.

Emmeshop tutorial Raspberry serial connection

To search for available serial ports we use the command

dmesg | grep tty

The output is something like this

pi@raspberrypi ~ $ dmesg | grep tty
  [    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
  [    0.001774] console [tty1] enabled
  [    0.749509] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
  [    1.268971] console [ttyAMA0] enabled
  pi@raspberrypi ~ $

Last line indicates that the console is enabled on the serial port ttyAMA0, so we disable it

Run the configuration command and follow the instructions below

sudo raspi-config

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Reboot and try with

dmesg | grep tty

output now is

pi@raspberrypi ~ $ dmesg | grep tty
  [    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
  [    0.001769] console [tty1] enabled
  [    0.749438] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
  pi@raspberrypi ~ $

Now we can use the serial ttyAMA0. We connect an adapter usb / serial, then we will try to establish a communication between the two serial ports; obviously in a practical application to every serial we could connect a device, for example a modem, a printer a RFID reader etc.

Emmeshop tutorial Raspberry two serial port communication

After connecting the second serial port we launch the command to find the name that Raspberry gives him

dmesg | grep tty

The output is something like this

pi@raspberrypi ~ $ dmesg | grep tty
  [    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
  [    0.001769] console [tty1] enabled
  [    0.749438] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
  [  971.919417] usb 1-1.2: pl2303 converter now attached to ttyUSB0
  pi@raspberrypi ~ $

Ok, now we create two files, one who writes something on the ttyAMA0 port and the other that reads on the ttyUSB0 port.

serial_write.py

    #!/usr/bin/env python
          
      
           import time
           import serial
          
      
           ser = serial.Serial(
              
               port='/dev/ttyAMA0',
               baudrate = 9600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
           counter=0
          
      
           while 1:
               ser.write('Write counter: %d \n'%(counter))
               time.sleep(1)
               counter += 1

serial_read.py

    #!/usr/bin/env python
          
      
           import time
           import serial
          
      
           ser = serial.Serial(
              
               port='/dev/ttyUSB0',
               baudrate = 9600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
           counter=0
          
      
           while 1:
               x=ser.readline()
               print x

If we run both files, serial_read.py will read what serial_write.py writes

Emmeshop tutorial Raspberry serial write and read

This is just a small example but it can serve as a starting point to send a print to an old printer or read data from a router or a gps.

Follow us on social to stay informed.
http://www.emmeshop.eu

Emmeshop tutorial Raspberry serial connection

<p>I using the raspi 3. and the writing seems not working initially.</p><p>Base on this disccussion <a href="http://raspberrypi.stackexchange.com/questions/45570/how-do-i-make-serial-work-on-the-raspberry-pi3/45571#45571" rel="nofollow">http://raspberrypi.stackexchange.com/questions/455...</a></p><p>change 'ttyAMA0' to 'ttyS0' in serial_write.py and it's </p><p>working fine!</p><p>***Thanks for sharing!</p>
nice work
<p>I'm working on this to try and get it to work, but ttyAMA0 disappeared after disabling serial. </p>
<p>I'm not sure if it's a problem with raspi-config GUI or not, I ended up undoing that and doing the following, though I'm not entirely sure if step 2 is necessary: </p><p>1. Changed code to use /dev/tty1</p><p>2. (from stack exchange)</p><p>&quot; step 5 -</p><p>a)to disable the Serial Console edit the file using</p><pre>sudo nano /boot/cmdline.txt<br></pre><p>remove the word phase &quot;console=serial0,115200&quot; or &quot;console=ttyAMA0,115200&quot;</p><p>Exit and save your changes &quot;</p><p>Then 'Sudo Reboot'</p>
<p>I'm having the same problem, I disabled and now ttyAMA0 is gone. Did you figure out what was wrong?</p>
<p>btw, uname -a returns </p><p>Linux raspberrypi 4.4.26+ #915 Thu Oct 20 17:02:14 BST 2016 armv6l GNU/Linux</p>
<p>cat /etc/os-release</p><p>PRETTY_NAME=&quot;Raspbian GNU/Linux 8 (jessie)&quot;</p>
<p>I ran the code from here: <a href="https://github.com/lurch/rpi-serial-console" rel="nofollow">https://github.com/lurch/rpi-serial-console</a></p><p>Then edited /boot/config.tx and set enable_uart=1; then rebooted</p><p>See <a href="https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=148515" rel="nofollow">https://www.raspberrypi.org/forums/viewtopic.php?f...</a> </p><p>for more details.</p>
<p>Works great with a GPS didn't even have to connect the receive line from the GPS.<br><br>I created a little shell script to read the raw data.</p><p><br>while true; do</p><p> read -n 200 LINE &lt; /dev/ttyAMA0</p><p>echo $LINE </p><p>done</p>
<p>It looks like you are using one of the UBLOX GPS receivers... have you written over the UART to change configurations on it? I'm trying to figure out how to construct a message in readable form based on the ublox manual... I'll start shotgunning some approaches soon, but if you have any input that would be great.</p>
<p>did you connect the gps directly to pi or ardupilot?</p>
<p>To the Pi's GPIO pins. I think you only need 1(Ground), 2 (5V power) and 4(TX/Transmit)</p>
<p>i use jessie raspbian a the write program dont work ...</p><p>it is blocked at ser.write call </p>
<p>Hi, Thank you, but in my case, it doesn't work.</p><p>I use RFID Reader and I can't print data from the RFID Tags(</p>
<p>I'm just now learning how to connect the Pi3 to a serial device (other microcontroller) and want to pass bytes of data back and forth. Most of the examples I see look like they pass ascii data. In my case that is not the desired method because I may possibly send a zero byte which would then be considered a null and terminate the string. I saw your post and was wondering if you are faced with the same situation when reading the RFID tag and if you found a solution.</p>
<p>I'm trying this on a raspberry pi and I get this error:</p><p><br>Traceback (most recent call last):<br>File &quot;serial_read.py&quot;, line 5, in &lt;module&gt;<br>import serial<br>ImportError: No module named serial</p><p><br>I tried &quot;$pip install serial&quot; and that failed as well. I'm a bit new to python. Could someone guide me to installing this python module? Thanks!</p>
<p>You should try &quot;$ pip install pyserial&quot; instead. Then use import serial on your python script.</p>
<p>Did anyone one else have issues with the serial_read file not reading? I have tried editing the program with different rates but not even the original works. PLEASE help</p>
<p>TU THX and THANKS. I have been trying to find out how to write Python code for TX RX (RS232) for AGES with no light at the end of the tunnel. Thank you so much for educating me so clearly and thoroughly. ian</p>
<p>ian - any links where I can check any more helpful info on TX RX (RS232) ?</p>
Hi J,<br>I found this useful from a connectivity point of view<br>http://codeandlife.com/2012/07/01/raspberry-pi-serial-console-with-max3232cpe/<br>hope it is of some help? ian
<p>ian - any links where I can check any more helpful info on TX RX (RS232) ?</p>
Thank u so much... :)
<p>This is so cool! The possiblilities of the Pi are generally limited to ones imagination. </p>

About This Instructable

217,642views

250favorites

License:

More by emmeshop:Roast Coffee With Artisan and Phidgets Raspberry datalogger with Mysql Highcharts  Remote control with Raspberry Pi and Phidget WebService 
Add instructable to: