How to Set Up UART Communication on the Arduino (2024)

In this tutorial, we will discuss what UART communication is and how it works. We will also write a simple sketch to show how to use the Arduino Uno’s UART interface. Then we will demonstrate with a project that uses UART to communicate between two Arduinos.

How to Set Up UART Communication on the Arduino (1)

What is UART?

UART stands for Universal Asynchronous Receiver/Transmitter. It is a hardware device (or circuit) used for serial communication between two devices.

How are UART Devices Connected?

Connecting two UART devices together is simple and straightforward. Figure 1 shows a basic UART connection diagram.

How to Set Up UART Communication on the Arduino (2)

One wire is for transmitting data (called the TX pin) and the other is for receiving data (called the RX pin). We can only connect two UART devices together.

How does UART Work?

UART works by converting data into packets for sending or rebuilding data from packets received.

Sending Serial Data

Before the UART device can send data, the transmitting device converts the data bytes to bits. After converting the data into bits, the UART device then splits them into packets for transmission. Each packet contains a start bit, a data frame, parity bit, and the stop bits. Figure 2 shows a sample data packet.

How to Set Up UART Communication on the Arduino (3)

After preparing the packet, the UART circuit then sends it out via the TX pin.

Receiving Serial data

The receiving UART device checks the received packet (via RX pin) for errors by calculating the number of 1’s and comparing it with the value of the parity bit contained in the packet. If there are no errors in transmission, it will then proceed to strip the start bit, stop bits, and parity bit to get the data frame. It may need to receive several packets before it can rebuild the whole data byte from the data frames. After rebuilding the byte, it is stored in the UART buffer.

The receiving UART device uses the parity bit to determine if there was a data loss during transmission. Data loss in transmission happens when a bit changed its state while being transmitted. Bits can change because of the transmission distance, magnetic radiation, and mismatch baud rates, among other things.

UART Parameters

UART has settings that need to be the same on both devices to have proper communication. These UART settings are the baud rate, data length, parity bit, number of stop bits, and flow control.

Baud Rate

Baud rate is the number of bits per second (bps) a UART device can transmit/receive. We need to set both UART devices with the same baud rate to have the proper transmission of data. Common values for baud rate are 9600, 1200, 2400, 4800 , 19200, 38400, 57600, and 115200 bps.

Data Length

Data length refers to the number of bits per byte of data.

Parity Bit

The parity bit is a bit added to the transmitted data and tells the receiver if the number of 1’s in the data transmitted is odd or even. The possible setting for Parity Bit is Odd or Even.

  • ODD – the parity bit is ‘1’ if there is an odd number of 1’s in the data frame
  • EVEN – the parity bit is ‘0’ if there is an even number of 1’s in the data frame

Number of Stop bits

UART devices can use none, one or two stop bits to mark the end of a set of bits (called packets) transmitted.

Flow Control

Flow Control is the method to avoid the risk of losing data when transmitting data over UART. The UART device uses special characters as flow control to start/stop transmission.

Arduino UART Interface

Arduino has one or more UART pins depending on the board. For our project, we will use an Arduino Uno which has only one UART interface found on pin 0 (RX0) and pin 1 (TX0). The Arduino pins 0 and 1 are also used for communicating with the Arduino IDE via the USB. So if you will upload sketches to your UNO, be sure to first disconnect any wires on pins 0 and 1. Figure 3 shows the location of the UART TX and RX pins.

How to Set Up UART Communication on the Arduino (4)

UART Logic Level

The UART logic levels may differ between manufacturers. For example, an Arduino Uno has a 5-V logic level but a computer’s RS232 port has a +/-12-V logic level. Connecting an Arduino Uno directly to an RS232 port will damage the Arduino. If both UART devices don’t have the same logic levels, a suitable logic level converter circuit is needed to connect the devices.

For further reading about UART, please check out our article on the Basics of UART Communication.

A Simple UART Project

After learning how the UART works, let us now build a simple sketch demonstrating how to use UART communication using Arduino Uno.

Our project is about controlling the built-in LED of an Arduino remotely via UART. A push-button wired to the first Uno board will control the built-in LED of the second Uno board and vice versa.

Components Required

To build our project, we need the following components:

Connection Diagram

Figure 4 shows how to connect the components used in our project.

How to Set Up UART Communication on the Arduino (5)

If you want to learn more about the Arduino, check out our Ultimate Guide to the Arduino video course. You’ll learn basic to advanced Arduino programming and circuit building techniques that will prepare you to build any project.

Arduino Sketch

After gathering and assembling the hardware, we are now ready to program our boards. For this project, both boards will have identical sketches. First, we set pin 8 (push button) pin mode to INPUT_PULLUP, set pin 13 (LED) pin mode to OUTPUT and set the initial state of pin 13 to LOW (LED off).

void setup() { pinMode(8, INPUT_PULLUP); // set push button pin as input pinMode(13, OUTPUT); // set LED pin as output digitalWrite(13, LOW); // switch off LED pin}void loop() {}

Serial Object

As always, the Arduino makes it easy for us to use the built-in UART hardware by using the serial object. The serial object has the necessary functions for an easy use of the Arduino’s UART interface.

Serial.begin()

To communicate via the UART interface, we need to configure it first. The easiest way to configure the Arduino’s UART is by using the function Serial.begin(speed). The speed parameter is the baud rate that we want the UART to run. Using this function will set the remaining UART parameters to default values (Data length=8, Parity bit=1, Number of Stop Bits=None).

If the default settings don’t work for you, use the function Serial.begin(speed,config) instead of Serial.begin(speed). The additional parameter config is used to change the settings for data length, parity bit, number of stop bits. Defined values for the parameter config can be found here.

The code below adds Serial.begin(9600); inside setup() to initialize the Arduino Uno UART with a baud rate of 9600 bps and other parameters set to default values.

void setup() { pinMode(8, INPUT_PULLUP); // set push button pin as input pinMode(13, OUTPUT); // set LED pin as output digitalWrite(13, LOW); // switch off LED pin Serial.begin(9600); // initialize UART with baud rate of 9600 bps}void loop() {}

The next part to code is to read and save a value received from the serial. To do this, we will use the function Serial.available() together with an If statement to check if there is data received. We will then call Serial.read() to get one byte of received data and save the value to the variable data_rcvd. The value of data_rcvd controls the On/Off of the built-in LED.

Serial.available()

To check if there is data waiting to be read in the UART (or serial) buffer, we will use the function Serial.available(). Serial.available() returns the number of bytes waiting in the buffer.

Serial.read()

To read the data waiting in the serial buffer, we will use the function Serial.read(). This function returns one byte of data read from the buffer.

void setup() { pinMode(8, INPUT_PULLUP); // set push button pin as input pinMode(13, OUTPUT); // set LED pin as output digitalWrite(13, LOW); // switch off LED pin Serial.begin(9600); // initialize UART with baud rate of 9600 bps}void loop() { if(Serial.available()) { char data_rcvd = Serial.read(); // read one byte from serial buffer and save to data_rcvd if(data_rcvd == '1') digitalWrite(13, HIGH); // switch LED On if(data_rcvd == '0') digitalWrite(13, LOW); // switch LED Off }}

Serial.write()

For us to send data via Arduino’s TX0 pins, we will use the function Serial.write(val). The val parameter is the byte (or series of bytes) to be sent.

In our sketch, we will send a char value depending on the state of pin 8. We will send the char value of '1' if pin 8 is HIGH or a char value of '0' if the pin 8 is LOW.

void setup() { pinMode(8, INPUT_PULLUP); // set push button pin as input pinMode(13, OUTPUT); // set LED pin as output digitalWrite(13, LOW); // switch off LED pin Serial.begin(9600); // initialize UART with baud rate of 9600 bps}void loop() { if (Serial.available()) { char data_rcvd = Serial.read(); // read one byte from serial buffer and save to data_rcvd if (data_rcvd == '1') digitalWrite(13, HIGH); // switch LED On if (data_rcvd == '0') digitalWrite(13, LOW); // switch LED Off } if (digitalRead(8) == HIGH) Serial.write('0'); // send the char '0' to serial if button is not pressed. else Serial.write('1'); // send the char '1' to serial if button is pressed.}

Sketch Upload and Testing

Save the sketch as arduino_uart_tutorial.ino. The remaining step is to upload the sketch to both Arduino Uno boards. Please remember to disconnect the wires connected to TX0 and RX0 pins before uploading the sketch. After uploading successfully, reconnect the wires on TX0 and RX0 pins.

After uploading, we can control the built-in LED of one of the boards by pressing the push button connected to the other board. Our sketch checks the button state and sends a corresponding char value of'0' or '1' to the other board to control the built-in LED.

Be sure to leave a comment if you have any questions or have trouble setting this up!


How to Set Up UART Communication on the Arduino (2024)

FAQs

How to Set Up UART Communication on the Arduino? ›

The easiest way to configure the Arduino's UART is by using the function Serial. begin(speed) . The speed parameter is the baud rate that we want the UART to run. Using this function will set the remaining UART parameters to default values (Data length=8, Parity bit=1, Number of Stop Bits=None).

Can you use UART with Arduino? ›

UART is one of the most used device-to-device (serial) communication protocols. It's the protocol used by Arduino boards to communicate with the computer. It allows an asynchronous serial communication in which the data format and transmission speed are configurable.

What are the pins for UART in Arduino Uno? ›

Pins for UART – Serial

You can also find the 2 required pins for UART directly on the Arduino Uno board, on pins 0 and 1: RX and TX. R stands for “reception” and T for “transmission”. This is a bidirectional communication. Note that the Serial used by USB is the same as the one used with pins 0 and 1.

How to add serial communication to Arduino? ›

You can use the Arduino environment's built-in serial monitor to communicate with an Arduino board. Click the serial monitor button in the toolbar and select the same baud rate used in the call to begin() . Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board).

How to configure UART? ›

Configure the UART

We will start with the USART Control B Register. The Bits 6 and 7 of this register are used to enable/disable the Transmitter and Receiver for the UART. We will enable these bits so to enable the Transmitter and Receiver. Also the bits [2:1] sets the Receiver in the different available modes.

How to communicate with UART? ›

Communication in UART can be simplex (data is sent in one direction only), half-duplex (each side speaks but only one at a time), or full-duplex (both sides can transmit simultaneously). Data in UART is transmitted in the form of frames. The format and content of these frames is briefly described and explained.

How many pins are required for UART communication? ›

Required Pins

The UART interface consists of two pins: the Rx and Tx pin. The Rx pin is used to receive data. The Tx pin is used to transmit data. When two devices are connected using a UART, the Rx pin of one device is connected to the Tx pin of the second device.

How to connect 2 UART? ›

Connecting UART Devices
  1. Connect device A's TX line to device B's RX line.
  2. Connect device A's RX line to device B's TX line.
  3. Connect the two devices' GND lines together.

What is the best way to communicate between two Arduinos? ›

Inter-Integrated Circuit or I2C (pronounced I squared C) is the best solution. I2C is an interesting protocol. It's usually used to communicate between components on motherboards in cameras and in any embedded electronic system. Here, we will make an I2C bus using two Arduinos.

What is the most common UART communication mode? ›

Asynchronous mode is the most common mode and is used for standard UART communication. Synchronous mode is used for higher-speed communication and requires a clock signal. ISO7816 mode is used for communication with smart cards and supports features such as parity checking and error detection.

What is the difference between UART and serial? ›

The UART takes bytes of data and transmits the individual bits in a sequential fashion. At the destination, a second UART re-assembles the bits into complete bytes. Serial transmission is commonly used with modems and for non-networked communication between computers, terminals and other devices.

How to check UART communication? ›

You can use a terminal program to test your UART communication and see if the data is correct and consistent. Some examples of terminal programs are PuTTY, Tera Term, or Arduino Serial Monitor. To use a terminal program, you need a USB-to-serial converter that connects your microcontroller to your computer's USB port.

How do you send serial messages on Arduino? ›

Upload the sketch and send messages using the Serial Monitor. Open the Serial Monitor by clicking the Monitor icon (see Recipe 4.1) and type a digit in the text box at the top of the Serial Monitor window. Clicking the Send button will send the character typed into the text box; you should see the blink rate change.

What are the two types of serial communication in Arduino? ›

Types of Serial Communications
  • Synchronous − Devices that are synchronized use the same clock and their timing is in synchronization with each other.
  • Asynchronous − Devices that are asynchronous have their own clocks and are triggered by the output of the previous state.

What are the TX and RX pins in Arduino? ›

The data comes or goes out in a byte at a time. The reference for Serial has some information and example code. The signals on the RX and TX pins are digital - they are either on or off (high or low). The TX pin sends out (transmits) the digital signal, and the RX pin listens (receives) for a digital signal.

Can UART be used as GPIO? ›

Most devices have a header containing GPIO pins for flashing and UART debugging ( RX & TX ). With some caution, these can also be used as GPIO pins. In general I wouldn't prefer to use these pins but in some cases this is the only way of extending devices with extra sensors.

What is the main drawback of UART? ›

Disadvantages of Using UART

The data frame size is limited to 9 bits. Multiple master and slave systems are not possible. To avoid data loss, each UART's Baud rate must be within 10% of one another. Data transfer speeds are slow.

Can UART work on RS232? ›

UART is the peripheral that can output asynchronous serial data frames like for RS232.

Does Arduino Nano support UART? ›

Learn how to send data from a Nano Every board to another board via UART. In this tutorial we will control the built-in LED on the Arduino Nano Every from another Arduino Nano Every. To do so, we will connect both boards using a wired communication protocol called UART.

Top Articles
The Best PUBG Settings for PC
NHS Wales: Discharge delays see thousands stuck in hospitals
Giant Key Osrs
Arcanis Secret Santa
Ap Psychology Unit 8 Vocab
8776685260
Best Taq 56 Loadout Mw2 Ranked
Pooch Parlor Covington Tn
Brazos County Jail Times Newspaper
Noah Schnapp Lpsg
Busted Newspaper Randolph County
National Weather Service Monterey
J. Foster Phillips Funeral Home Obituaries
โลโก้โภชนาการที่ดีที่สุด: สัญลักษณ์แห่งความเป็นเลิศ
Espn Masters Leaderboard
Family Guy Wiki Peter
How To Get Father, Son or Grandmother Tokens in Warframe?
Asoiaf Spacebattles
Jennette Mccurdy Tmz Hawaii
Bleach Tybw Part 2 Gogoanime
Ktbs Payroll Login
Kaelis Dahlias
Test Nvidia GeForce GTX 1660 Ti, la carte graphique parfaite pour le jeu en 1080p
Crazy 8S Cool Math
Papa's Games Unblocked Games
Karz Insurance Quote
Truecarcin
Water Leaks in Your Car When It Rains? Common Causes & Fixes
Walmart Com Careers Jobs
Telegram Voyeur
Hartford Healthcare Employee Tools
Ohio State Football Wiki
Cavender's Boot City Killeen Photos
Sold 4 U Hallie North
Goodwill Winter Springs 434
Gabrielle Enright Weight Loss
Couches To Curios Photos
Geritol Complete - Gebrauchsanweisung, Dosierung, Zusammensetzung, Analoga, Nebenwirkungen / Pillintrip
T&J Agnes Theaters
Game Akin To Bingo Nyt
Helixnet Rfums
Busty Bruce Lee
CareCredit Lawsuit - Illegal Credit Card Charges And Fees
The Whale Showtimes Near Cinépolis Vista
Nz Herald Obituary Notices
Oriellys Tooele
Research Tome Neltharus
Ttw Cut Content
13364 Nw 42Nd Street
Gary Zerola Net Worth
CareLink™ Personal Software | Medtronic
8X10 Meters To Square Meters
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5542

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.