Language: EN

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 commonly used interfaces for communicating Raspberry Pi with other devices, such as sensors, GPS modules, Bluetooth modules, and other microcontrollers.

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

Sometimes you will hear it simply referred to as “serial port,” especially if you come from the Arduino world or similar.

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 (Transmission) and RX (Reception). These lines enable bidirectional communication between two devices.

  • TX (Transmission): Sends data from the device.
  • RX (Reception): Receives data on the device.

UART communication is asynchronous, which means there is no shared clock signal between the 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 an integrated 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.

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

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

To avoid damage to the Raspberry Pi, it is necessary to use a level shifter.

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 that starts with enable_uart and make sure it is set as follows:

enable_uart=1

Save the changes and reboot the Raspberry Pi:

sudo reboot

Verify UART

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

ls /dev/serial*

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

Disable the Serial Console

If you do not need the serial console and want to free up 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 TX of the Raspberry Pi to the RX of the Arduino.
  • Connect the RX of the Raspberry Pi to the TX of the Arduino.
  • Connect the GND of the Raspberry Pi to the GND of the Arduino.

Arduino Code

The following code on Arduino waits to receive a character over 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
  }
}

Raspberry Pi Code

On the Raspberry Pi, we will use Python to send and receive data through UART. Make sure to 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()

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.