Skip to content

Commit 923e661

Browse files
committed
Update uart.md
1 parent 7028661 commit 923e661

File tree

1 file changed

+106
-13
lines changed
  • content/learn/05.communication/09.uart

1 file changed

+106
-13
lines changed

content/learn/05.communication/09.uart/uart.md

Lines changed: 106 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,70 @@
11
---
2-
title: "Universal Asynchronous Receiver-Transmitter (UART)"
3-
description: "A serial communication protocol used to send data using two wires. "
4-
author: "Hannes Siebeneicher"
2+
title: 'Universal Asynchronous Receiver-Transmitter (UART)'
3+
description: 'A serial communication protocol for sending serial data over USB or via TX/RX pins.'
4+
author: 'Hannes Siebeneicher'
5+
tags: [UART, Serial Communication]
56
---
67

7-
## Introduction
8-
9-
This article dives into the basics of Universal Asynchronous Receiver-Transmitter (UART), a serial communication protocol that can be used to send data between two Arduino boards using only two wires.
8+
In this article, you will learn the basics of Universal Asynchronous Receiver-Transmitter (UART), a serial communication protocol that can be used to send data between an Arduino board and other devices. This is the protocol used when you send data from an Arduino to your computer, using the classic `Serial.print()` method.
109

1110
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. It's among the earliest serial protocols and even though it has in many places been replaced by [SPI](/learn/communication/spi) and [I2C](/learn/communication/wire) it's still widely used for lower-speed and lower-throughput applications because it is very simple, low-cost and easy to implement.
1211

13-
When sending UART messages between two Arduino boards we use `Serial1`. That is because `Serial` is used to communicate between the Arduino and the computer to, e.g. display messages inside the serial monitor.
12+
Communication via UART is enabled by the [Serial](https://www.arduino.cc/reference/en/language/functions/communication/serial/) class, which has a number of methods available, including reading & writing data.
1413

1514
***If you want to jump straight to the examples click [here](#examples) or go to the end of this article.***
1615

1716
## Overview
1817

19-
- [How UART Works](#how-uart-works)
20-
- [Timing and Synchronization](#timing-and-synchronization)
21-
- [UART Messages](#uart-messages)
22-
- [Examples](#examples)
18+
- [Overview](#overview)
19+
- [Serial Class](#serial-class)
20+
- [Arduino UART Pins](#arduino-uart-pins)
21+
- [Technical Specifications](#technical-specifications)
22+
- [How UART Works](#how-uart-works)
23+
- [Timing and Synchronization](#timing-and-synchronization)
24+
- [UART Messages](#uart-messages)
25+
- [Serial USB Examples](#serial-usb-examples)
26+
- [Basic Print Example](#basic-print-example)
27+
- [Read](#read)
28+
- [RX/TX Pin Examples](#rxtx-pin-examples)
29+
- [Transmit / Receive Messages](#transmit--receive-messages)
30+
- [Control Built-in LED](#control-built-in-led)
31+
32+
## Serial Class
33+
34+
With the [Serial](https://www.arduino.cc/reference/en/language/functions/communication/serial/) class, you can send / receive data to and from your computer over USB, or to a device connected via the Arduino's RX/TX pins.
35+
36+
- When sending data over USB, we use `Serial`. This data can be viewed in the Serial Monitor in the Arduino IDE.
37+
- When sending data over RX/TX pins, we use `Serial1`.
38+
- The [GIGA R1 WiFi](https://store.arduino.cc/products/giga-r1-wifi), [Mega 2560](https://store.arduino.cc/products/arduino-mega-2560-rev3) and [Due](https://store.arduino.cc/products/arduino-due) boards also have `Serial2` and `Serial3`
39+
40+
The [Serial](https://www.arduino.cc/reference/en/language/functions/communication/serial/) class have several methods with some of the essentials being:
41+
- `begin()` - begins serial communication, with a specified baud rate (many examples use either `9600` or `115200`).
42+
- `print()` - prints the content to the Serial Monitor.
43+
- `println()` - prints the content to the Serial Monitor, and adds a new line.
44+
- `available()` - checks if serial data is available (if you send a command from the Serial Monitor).
45+
- `read()` - reads data from the serial port.
46+
- `write()` - writes data to the serial port.
47+
48+
For example, to initialize serial communication on both serial ports, we would write it as:
49+
50+
```
51+
Serial.begin(9600); //init communication over USB
52+
Serial1.begin(9600); //communication over RX/TX pins
53+
```
54+
55+
***The [Serial](https://www.arduino.cc/reference/en/language/functions/communication/serial/) class is supported on all Arduino boards.***
56+
57+
## Arduino UART Pins
58+
59+
The default TX/RX pins on an Arduino board are the D0(RX) and D1(TX) pins. Some boards have additional serial ports, see table below:
60+
61+
| Form Factor | RX | TX | RX1 | TX1 | RX2 | TX2 | RX3 | TX3 |
62+
| ----------- | --- | --- | --- | --- | --- | --- | --- | --- |
63+
| MKR | D0 | D1 | | | | | | |
64+
| UNO | D0 | D1 | | | | | | |
65+
| Nano | D0 | D1 | | | | | | |
66+
| Mega | D0 | D1 | D19 | D18 | D17 | D16 | D15 | D14 |
67+
2368

2469
## Technical Specifications
2570

@@ -37,7 +82,7 @@ As seen in the image above when using parallel communication an 8-bit message wo
3782

3883
The key components of UART include the transmitter, receiver, and baud rate. The transmitter collects data from a source, formats it into serial bits, and sends it via a TX (Transmit) pin. The receiver receives it via a RX (Receive) pin, processes incoming serial data and converts it into parallel data for the host system. The baud rate determines the speed of data transmission.
3984

40-
### Timing and synchronization
85+
### Timing and Synchronization
4186

4287
Timing and synchronization are crucial aspects of UART communication. Unlike synchronous serial communication protocols such as SPI and I2C, UART operates operates asynchronously, meaning it doesn't rely on a shared clock signal to coordinate data transmission. Instead, it uses predefined baud rates to determine the timing of data bits.
4388

@@ -124,12 +169,60 @@ The polarity of the stop bit(s) can vary, with some systems using a high stop bi
124169

125170
![Stop Bits](./assets/stopBits.png)
126171

127-
## Examples
172+
## Serial USB Examples
173+
174+
To send data between an Arduino and a computer, you will need to the board to a computer with a **USB cable**.
175+
176+
### Basic Print Example
177+
178+
This example will send the string `Hello World!` from an Arduino to a computer, using the `Serial.println()` function. Data will be sent every one second.
179+
180+
```arduino
181+
void setup(){
182+
Serial.begin(9600); //initialize serial communication at a 9600 baud rate
183+
}
184+
185+
void loop(){
186+
Serial.println("Hello world!");
187+
delay(1000);
188+
}
189+
```
190+
191+
`Serial.print()` / `Serial.println()` is used in almost all Arduino sketches, as you can understand what goes on in the board, and design programs to provide you information on specific events.
192+
193+
### Read
194+
195+
To send data from a computer to an Arduino (from the Serial Monitor), we can make use of the `Serial.available()` and `Serial.read()` functions. First, we check if there's any data available, and if so, we read it and print it out.
196+
197+
This example will essentially print out whatever you enter in the Serial Monitor (because we send the data to the board, but we also print it back).
198+
199+
```arduino
200+
int incomingByte = 0; // for incoming serial data
201+
202+
void setup() {
203+
Serial.begin(9600); //initialize serial communication at a 9600 baud rate
204+
}
205+
206+
void loop() {
207+
// send data only when you receive data:
208+
if (Serial.available() > 0) {
209+
// read the incoming byte:
210+
incomingByte = Serial.read();
211+
212+
// say what you got:
213+
Serial.print("I received: ");
214+
Serial.println(incomingByte, DEC);
215+
}
216+
}
217+
```
218+
219+
## RX/TX Pin Examples
128220

129221
This section contains some basic UART examples, where you send data between two Arduino boards. To set it up, connect the TX with RX pins on both boards, following the circuit below:
130222

131223
![Connecting two Arduino boards via UART.](./assets/circuit.png)
132224

225+
133226
### Transmit / Receive Messages
134227

135228
This example allows you to send messages (strings) back and forth between devices. Upload the following sketch to both devices:

0 commit comments

Comments
 (0)