EducationUART serial communication interface

UART serial communication interface

Category articles

Communication interface UART (universal asynchronous receiver-transmitter) communication protocol, whose task is to receive and send information via the serial port. The UART enabled chip is equipped with a parallel-serial converter, which is designed to convert data from one device to another. It also has a serial-parallel system that works in the reverse direction, it performs conversion when receiving information.

Principle of the UART interface

The basis of the UART interface is based on the transmission of bits in the form of a data frame.

uart data frame
UART data frame

LSB – least significant bit,

MSB – most significant bit.

To start broadcasts, you should send Start Bit. It occurs in the form of a high state change (1) to a low state (0). After sending the Start Bit, data are sent in sizes 7, 8 or 9 bit, starting with LSB. Before Stop Bit you can optionally set Parity bit informing about errors in data transfer. The stop bit transmitted at the end of the data frame occurs in the form of a high state change (1).

UART connection settings

We need two devices for a proper UART interface connection. This interface works on the principle of direct transmission between two devices. This is not a data bus where many devices are connected. The main principle of the correct transmission setting is setting the Baud Rate (Bit Rate) on both devices. This value must be the same on both devices connected to each other. The most popular transmission speeds are 9600 bit/s and 115200 bit/s.

UART connection
UART connection

Tx – port designated as the data sending port,

Rx – port designated as a port for receiving data,

GND – ground.

The advantage of the UART interface is the ability to send and receive data simultaneously.

The advantages of the UART interface

  • Easy to use interface, well documented,
  • Simple construction of the transmission frame,
  • Possibility to attach a bit informing about errors.

Disadvantages of the UART Interface

  • Free data transfer up to 1 Mbit / s,
  • Asynchronous communication.

UART bluetooth connection by HC06 on Arduino Uno

The method of connection between a Bluetooth device such as an Android phone and Arduino requires the Bluetooth Low Energy standard.

UART bluetooth HC06
UART bluetooth HC06

To make this device you will need: Arduino Uno, USB connection cable, male-female cables and a device with Android system (tablet or smartphone).

Connection schema between the Arduino Uno and Bluetooth HC06:

Arduino 5V — Vcc HC06

Arduino D10 — TXD HC06

Arduino 11 — RX HC06

Arduino GND — GND HC06

After correct connection, the LED on the HC06 module will start blinking. This means no connection to the any Bluetooth device.

Creation of the Arduino Sketch function

If all Bluetooth pins for Arduino are connected, the bluetooth library code should be downloaded via Arduino Sketch. The program is simple and has clear comments to the function. In the program you can see the start of Bluetooth UART transmission under the BT.begin function.

The program is built to wait for the device to receive the value from the connected Bluetooth device with the loaded terminal. On the Android device, the Bluetooth Terminal by qwerty program was installed, which served as a tool for sending values to Arduino.

Bluetooth Terminal
Bluetooth Terminal

After the correct installation of the Bluetooth terminal on the Android device, pair both devices by searching for the HC06 module in the Android device. To be paired enter the PIN code: 1234.

Arduino Sketch source code:

#include "<softwareserial.h>" // remove the inverted commas after you copy the code to the IDE
SoftwareSerial BT(10, 11); 

// creates a "virtual" serial port/UART

// connect BT module TX to D10

// connect BT module RX to D11

// connect BT Vcc to 5V, GND to GND

void setup()  

{

  // set digital pin to control as an output

  pinMode(13, OUTPUT);

  // set the data rate for the SoftwareSerial port

  BT.begin(9600);

  // Send test message to other device

  BT.println("Hello from Arduino");

}

char a; // stores incoming character from other device

void loop() 

{

  if (BT.available())

  // if text arrived in from BT serial...

  {

    a=(BT.read());

    if (a=='1')

    {

      digitalWrite(13, HIGH);

      BT.println("LED on");

    }

    if (a=='2')

    {

      digitalWrite(13, LOW);

      BT.println("LED off");

    }

    if (a=='?')

    {

      BT.println("Send '1' to turn LED on");

      BT.println("Send '2' to turn LED on");

    }   

      }
}

After correct pairing, open the Bluetooth terminal application and connect to the HC06 module. If the connection is made correctly, the LED on the HC06 module should be on all the time.

After completing all these steps, the Android device sends the values you enter from the keyboard correctly. In this program 1 means to turn on the LED on Arduino and 2 it turns off. This is just the simplest use example to check the idea of a Bluetooth UART connection. Now if we can connect and receive assigned values from an Android device, you can create an application that controls the work of devices connected to the embedded platforms.

 

Michal Pukala
Electronics and Telecommunications engineer with Electro-energetics Master degree graduation. Lightning designer experienced engineer. Currently working in IT industry.

News