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

  • Thanks for posting this project Vivek, Very informative. Are there any serial ports other than the port that is configured as the terminal? I wonder If I can do a 9-bit serial stream on the Pi? So many questions!!

    Thanks again,
    Glenn

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

  • 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

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

  • 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

  • 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.

  • 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?


  • >