Serial (UART) communication on Raspberry Pi 2

UART – Universal Asynchronous Receiver/Transmitter is one of the earliest mode of communication applied to computers. Asynchronous means that data is transferred without support from an external clock signal and universal because it’s parameters: speed, frame size and number of parity and stop bits are not fixed and can be configured to meet the needs of a given communication requirement.

The asynchronous serial protocol has four built-in-rules, mechanisms, which must be the same on both sides: Baud rate, Data bits, Parity bits and Stop bits.

Both devices on a serial bus must use exactly the same configuration parameters. 

 

UART parameters are set as: 9600 8N1 which means:

  • BaudRate = 9600
  • Data bits = 8
  • No parity bit
  • Stop bits = 1

 

Disabling console on ttyAMA0

Type dmesg | grep tty into terminal to check if Raspberry is using serial port for console login. If output is like picture below you will need to disable login shell over serial port.

Raspberry is using ttyAMA0 for console login
Raspberry is using ttyAMA0 for console login

Type in terminal:

Config screen should appear as on picture below. Select Advanced Options.

Main configuration tool screen
Main configuration tool screen

Next, select Serial and disable it.

Peripheral configuration tool
Peripheral configuration tool

Raspberry Pi will be rebooted. Again type dmesg | grep tty into terminal and watch output which should be just like picture below.

ttyAMA0 port is disabled
ttyAMA0 port is disabled

Check uart using pyserial

Install pySerial by typing in terminal.

After installation is complete connect Tx (GPIO14) and Rx (GPIO15) together and write

into terminal.

Output screen
Output screen

Start typing and see if your letters double. If they do, then it means that UART is working as each letter we send we also receive. It’s also called Loop-Back test.

Below is example how you can also check UART with simple python script.

Output from above code
Output from above code


Related posts