---
title: "How to Use UART "
date: "2024-12-12"
categories: ["Advanced Guides"]
tags: ["Advanced", "Python"]
excerpt: "UART (universal asynchronous receiver transmitter) is a hardware communication protocol between two devices. The Micromelon Rover features an expansion header that can be used to connect external devices, such as an Arduino or an IR sensor. This guid"
featuredImage: "/images/resources/getting-started-the-micromelon-rover-uart.png"
---

UART (universal asynchronous receiver transmitter) is a hardware communication protocol between two devices. The Micromelon Rover features an expansion header that can be used to connect external devices, such as an Arduino or an IR sensor.

This guide will outline how the UART protocol works, how it is implemented in the Rover and how to connect a device to the Rover using UART.

![](/images/content/2d6b14-expansion.jpg)

## What is UART?

The UART communication protocol is used to transmit data packets over two wires. It is useful as it allows serial data to be transmitted from one device to another. The fact that UART is asynchronous means that no clock is needed to send data. All that is required is for the transmit pin of one device to be connected to the receive pin of another.

Since there is no clock, the transmitter and receiver must agree on a predetermined transmission rate called a Baud Rate. UART also has a standard packet structure, so the receiving device knows when to read the bits. This structure is as per the packet structure table below:

  Table: UART packet strcture

    Start Bit
    Data Frame
    Parity Bit
    Stop Bits

    1 bit
    5 to 9 data bits
    0 to 1 partiy bits
    1 to 2 stop bits

The **start bit** is held high until data is ready to be sent and then set low. This then tells the receiving UART to begin reading the transmission line. The ** data frame** contains the actual data and can be anywhere from 5 to 9 bits longer, depending on whether the parity bit is used. The ** parity bit** is used to verify the data. It does so by counting the number of bits with a 1 and setting the parity bit to a 0 if the total is odd or 1 if it is even. This ensures that no bits have been changed in the data transfer. The ** stop bit** signals the end of a package by pulling the data transmutation line to a high voltage. The receiver then stops reading the transmission line.

## Hardware Setup

![](/images/content/56892c-image-from-ios.jpg)

Connecting two devices through the UART protocol requires two wires. This allows data to both be received and transmitted by both devices. Each device's RX (receive) pin must be connected to the other's TX (transmit) pin. Additionally, a shared ground is needed between both devices.

The UART pins can be found in the expansion header that sits below the LCD screen on the Rover. The 3.3V and GND pins will also be required to power the device communicating with the Rover. Jumper wires can be used to achieve this. Example guides on how this can be done can be found in the software interfacing section.

It is important to note that the rover power switch should be in the on position when the other device is powered on. Not doing so may cause the Rover to be back powered, even if the 3.3V and GND pins are not connected.

## Rover UART Packets

The Micromelon Rover follows a particular format for the UART data. This format allows commands to be sent to the Rover from an external device over UART. Most languages will have a function that will handle the sending of the start bits, partly bits and stop bits, so all that needs to be given is the data frame. This data structure is shown in the table below:

  Table: UART packet data structure for the Micromelon Rover

    Start Byte
    Operation Byte (Read/Write)
    Register Byte
    Data Length Byte
    Data Byte

    Always 0x05
    0x00 to read, 0x01 to write
    An enum that corresponds to a command
     The number of bytes of data
     Data related to the command

The **start byte** is always 0x05, which tells the Rover's UART to begin reading the transmission line. The ** operation byte**(either 0x00 or 0x01) is the set based on whether the external device is going to be reading data (0x00) from the Rover or sending commands (0x01) by writing data. The** register byte ** is a value that corresponds to the command that is being sent to the Rover. The information associated with this command is stored in the** data byte**, with the number of bytes in the data stored in the** data length byte**.

## Software Interfacing

The final step in sending UART packets to the Rover is to write a program to initialise the connection, create packets and then send them. How to implement this will depend on what secondary device is being used. Still, generally, this implementation will be in either Python or C++.

For a Python implementation, refer to the Open MV UART guide.

For a C++ and Arduinio implementation, refer to the Arduinio UART guide.

[Getting Started with Open MV and the Micromelon Rover](getting-started-with-open-mv-and-the-micromelon-rover)

[Getting Started with Arduino and the Micromelon Rover](/resources/getting-started-with-arduino)
