Using UART on Raspberry Pi – Python

Using UART on Raspberry Pi – Python

UART stands for Universal Asynchronous Transmitter / Receiver, a popular serial communication interface which provides full duplex communication between two devices. The term universal means that transmission speed and data format are configurable. As it is asynchronous it doesn’t need to send clock signal along with the data signals. UART uses two data lines for sending (Tx) and receiving (Rx) data. The ground of both devices should be made common.

UART Communication

UART Communication

 

Freeing up UART pins on Raspberry Pi GPIO

By default Raspberry Pi’s UART pins (GPIO 14 and 15) are configured as a serial console. It outputs all the kernel data during boot. We need to free up these pins for our use. For this launch terminal,

  • First make a backup of the file containing kernel parameters cmdline.txt as cmdline_bp.txt
sudo cp /boot/cmdline.txt /boot/cmdline_bp.txt
  • Edit the file cmdline.txt by removing the parameters containing ‘ttyAMA0‘. ie. ‘console=ttyAMA0,115200’ and ‘kgdboc=ttyAMA0,115200’.
sudo nano /boot/cmdline.txt
  • The remaining file looks like,
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p6 rootfstype=ext4 elevator=deadline rootwait
Raspberry Pi Kernal files

Raspberry Pi Kernal files

  • Save the file, Ctrl + O
  • Close the editor, Ctrl + X
  • Now edit inittab, file containing serial console data
sudo nano /etc/inittab
  • Comment out the line  ‘2:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100’
#2:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Raspberry Pi Serial console

Raspberry Pi Serial console

  • Save the file, Ctrl + O
  • Close the editor, Ctrl + X
  • Reboot Raspberry Pi by using the command, sudo reboot.

Now you have freed the UART pins.


You can verify whether the Pi is sending and receiving UART data by installing the tool Minicom.

  • Short the Rx and Tx pins on Pi (GPIO 14 and 15), such that it will receive the same data as it transmits.
  • Install minicom,
sudo apt-get install minicom
Raspberry Pi Install Minicom

Raspberry Pi Install Minicom

  • And launching it,
minicom -b 115200 -o -D /dev/ttyAMA0

Where,

  • 115200 is the baud rate
  • ttyAMA0 is the port
Raspberry Pi Minicom terminal

Raspberry Pi Minicom terminal

  • Verify whether the pi is receiving the same data as it transmits.

Using Python

In this section we will see, How to access UART using Python.

Installing pySerial

Pyserial provides backend for serial communication using python. The module named ‘serial’ selects appropriate backend automatically.  To install pySerial, by using following command.

sudo apt-get install python-serial

Now the module serial can be imported to python by using ‘import serial‘.

Example Program

The following python program reads 10 characters from the serial port and sends back it.

import serial
ser = serial.Serial ("/dev/ttyAMA0")    #Open named port 
ser.baudrate = 9600                     #Set baud rate to 9600
data = ser.read(10)                     #Read ten characters from serial port to data
ser.write(data)                         #Send back the received data
ser.close()        

For More Information : pySerial API

Any doubts or suggestions? Comment below.

Share this post

Comments (8)

  • Mayke Santos

    Hi.
    How do you log into the Pi through UART communication? I managed to connect my Raspberry Pi to my Laptop using the method you described above; but I still don’t know how to control my Pi. Minicom does not give you signing in capabilities; does it?

    November 21, 2015 at 7:37 pm
  • Ligo George

    Hi,
    Above tutorial not explaining to control your Pi through UART. It just shows how to use UART. We need UART communication for a lot of applications.

    November 22, 2015 at 9:24 am
  • Misha

    Hi

    I am planning on sending integer data over a 433Mhz RF kit using 2 RPi’s. Now I have searched for a very long time and cannot find valid information. Please can you help.

    I want to send numerical data(integers or floats perhaps) over the 433Mhz RF link kit using the UART pins. Now the data sent is constantly changing in an infinite loop.

    1. I assume if I change the parameters defining what “data” is (referencing above code) then I could transmit anything?
    2. What is the code for the receiver? what code can I use to read the numbers that I sent through?

    All my code is in python like above and would like to keep it such
    Any help would be appreciated.

    Misha

    April 26, 2016 at 7:53 pm
  • Ligo George

    Yes, you can use UART communication, but you should use lowest baud rate.

    June 2, 2016 at 6:17 pm
  • Pierre Marconi

    HI
    I always get a
    AttributeError: ‘module’ object has no attribute ‘Serial’
    any suggestions?
    thanks

    December 1, 2017 at 6:48 pm
  • aman surya

    ttyAMA0 is not at all there
    instead its serial0
    it’s like console=serial0,15200 etc
    i am not able to do this so please help me

    May 22, 2018 at 11:31 am
  • Rendon Greeff

    Hi, why does minicom not accept my keystrokes after I launch it?

    July 29, 2019 at 12:37 am
  • Rendon Greeff

    Also, if I run the program in python, it gets stuck when I read from or write to the uart.

    July 29, 2019 at 12:37 am

Leave a Reply

Your email address will not be published. Required fields are marked *