como-usar-puerto-serie-uart-raspberry-pi

How to Use UART Serial Port on Raspberry Pi

  • 4 min

The UART is one of the most used interfaces for communicating Raspberry Pi with other devices, such as sensors, GPS modules, Bluetooth modules, and other microcontrollers.

UART (Universal Asynchronous Receiver-Transmitter) is a serial communication protocol that allows data transmission between devices asynchronously.

Sometimes you’ll hear it called simply “serial port”, especially if you come from the Arduino or similar world.

Let’s see how we can use it with a Raspberry Pi 👇

What is UART?

UART is a serial communication protocol that uses two data lines: TX (Transmit) and RX (Receive). These lines allow bidirectional communication between two devices.

  • TX (Transmit): Sends data from the device.
  • RX (Receive): Receives data into the device.

UART communication is asynchronous, meaning there is no shared clock signal between devices. Instead, both devices must be configured to use the same transmission speed, known as baud rate.

Unlike other protocols like SPI or I2C, UART does not require a shared clock, making it simpler but also slower in comparison.

UART on Raspberry Pi

The Raspberry Pi has a built-in UART port that can be used to communicate with other devices. On most Raspberry Pi models, the UART is available on the GPIO pins:

  • TX (GPIO 14): Pin 8 on the GPIO connector.
  • RX (GPIO 15): Pin 10 on the GPIO connector.

Furthermore, the UART on Raspberry Pi can be used for communication with the serial console, which is useful for system debugging and configuration.

It’s important to note that the UART on Raspberry Pi operates at 3.3V, while many devices, such as Arduinos, operate at 5V.

To avoid damaging the Raspberry Pi, it is necessary to use a logic level converter.

Configuring UART on Raspberry Pi

Enable the Serial Console

By default, the UART on Raspberry Pi may be disabled or used for the serial console. To enable it, we need to modify the system configuration file.

Open the configuration file:

sudo nano /boot/config.txt

Look for the line starting with enable_uart and make sure it is configured as follows:

enable_uart=1

Save the changes and reboot the Raspberry Pi:

sudo reboot

Verify the UART

To verify that the UART is working correctly, you can use the following command:

ls /dev/serial*

If the UART is enabled, you should see something like /dev/serial0 or /dev/ttyAMA0.

Disable the Serial Console

If you don’t need the serial console and want to free the UART for other uses, you can disable it:

Open the configuration file:

sudo nano /boot/cmdline.txt

Remove any reference to console=serial0,115200 or console=ttyAMA0,115200.

Save the changes and reboot the Raspberry Pi:

sudo reboot

Practical Example

Let’s see how it works with a practical example of how to use UART to communicate a Raspberry Pi with an Arduino.

  • Connect the Raspberry Pi’s TX to the Arduino’s RX.
  • Connect the Raspberry Pi’s RX to the Arduino’s TX.
  • Connect the Raspberry Pi’s GND to the Arduino’s GND.

Arduino Code

The following code on Arduino waits to receive a character via UART and then sends it back to the Raspberry Pi.

void setup() {
  Serial.begin(9600); // Initializes UART at 9600 baud
}

void loop() {
  if (Serial.available() > 0) {
    char receivedChar = Serial.read(); // Read a received character
    Serial.print("Received: ");
    Serial.println(receivedChar); // Sends back the received character
  }
}
Copied!

Raspberry Pi Code

On the Raspberry Pi, we will use Python to send and receive data via UART. Make sure you have the pyserial library installed:

sudo apt-get install python3-serial

Then, you can use the following Python code:

import serial
import time

# Set up the serial port
ser = serial.Serial('/dev/serial0', 9600, timeout=1)
ser.flush()

try:
    while True:
        # Send a character to the Arduino
        ser.write(b'A')
        print("Sent: A")
        
        # Wait for a response
        if ser.in_waiting > 0:
            line = ser.readline().decode('utf-8').rstrip()
            print(f"Received: {line}")
        
        time.sleep(1)
except KeyboardInterrupt:
    print("Program terminated")
finally:
    ser.close()
Copied!

Now

  1. Upload the code to the Arduino.
  2. Run the Python script on the Raspberry Pi.
  3. You should see in the Raspberry Pi console that the character A is sent and the response from the Arduino is received.