Skip to content

Commit 4de89d3

Browse files
Added UART
1 parent 03ee66b commit 4de89d3

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
title: Serial communication (UART)
3+
description: Learn how to use UART on Micropython
4+
author: Pedro Sousa Lima
5+
---
6+
7+
Universal Asynchronous Receiver-Transmitter, or **UART**, is one of the simplest and most widely used communication protocols for connecting devices. It enables point-to-point communication over just two wires, making it a perfect fit for debugging, logging, and connecting to peripheral devices.
8+
9+
## How UART Works
10+
11+
UART communication relies on:
12+
- **TX (Transmit):** Sends data from one device to another.
13+
- **RX (Receive):** Receives data from the transmitting device.
14+
15+
Unlike other protocols like I2C or SPI, UART is asynchronous, meaning it doesn’t use a clock line. Instead, the sender and receiver agree on a common data rate (baud rate), such as 9600 bits per second.
16+
17+
Each device directly connects TX to RX and RX to TX.
18+
19+
20+
21+
### Key Features of UART
22+
23+
1. **Simple Point-to-Point Communication:** Ideal for connecting two devices.
24+
2. **Flexible Speeds:** Baud rates can be adjusted to meet application needs.
25+
3. **Independent Lines:** TX and RX operate independently, enabling bidirectional communication.
26+
27+
## How to Read the Output
28+
29+
To observe the output of your UART communication, you’ll need a secondary board (or a USB-to-UART adapter) to act as a receiver. Here’s how you can set it up:
30+
31+
1. **Primary Board (Transmitter):** Runs the main UART code examples provided below.
32+
2. **Secondary Board (Receiver):** Reads from the UART interface and displays the output on a serial monitor like Arduino IDE, PuTTY, or CoolTerm.
33+
34+
### Circuit Setup
35+
- Connect the **TX1 (GPIO43)** of the primary board to the **RX** of the secondary board.
36+
- Connect the **RX1 (GPIO44)** of the primary board to the **TX** of the secondary board.
37+
- Connect the ground (GND) of both devices together.
38+
39+
![How to wire](assets/UARTTestingSetup.png)
40+
41+
### Code for Secondary Board
42+
The secondary board simply forwards received UART data to its serial monitor. Use the following code:
43+
44+
```python
45+
from machine import UART
46+
47+
# Initialize UART
48+
uart = UART(1, baudrate=9600, tx=43, rx=44) # Adjust TX/RX pins as needed
49+
50+
while True:
51+
if uart.any(): # Check if data is available
52+
ch = uart.read(1).decode() # Read one character
53+
print(ch, end="") # Print to the console
54+
```
55+
56+
### Using the Secondary Board
57+
1. Flash the above code to the secondary board.
58+
2. Open a serial monitor for the secondary board (e.g., Arduino Lab for Micropython, Arduino IDE Serial Monitor, Putty).
59+
60+
61+
## Example 1: Testing UART
62+
63+
In this first example, we’ll create a program that prints `UART says Hi!` every second, allowing you to verify that the UART connection is working.
64+
65+
### Circuit Diagram
66+
For this example:
67+
- **Connect TX1 (GPIO43)** to the RX of your receiving device (e.g., USB-to-serial adapter).
68+
- **Connect RX1 (GPIO44)** to the TX of your receiving device.
69+
- **Connect GND** to the ground of the receiving device.
70+
71+
72+
### Code Example
73+
74+
Copy the following code into your `main.py` file and run it:
75+
76+
```python
77+
from machine import UART, Timer
78+
79+
# Initialize UART
80+
uart = UART(1, baudrate=9600, tx=43, rx=44)
81+
82+
# Timer to send debug message
83+
def send_debug_message(timer):
84+
uart.write("UART says Hi!\n")
85+
86+
# Configure the timer
87+
timer = Timer(0)
88+
timer.init(period=1000, mode=Timer.PERIODIC, callback=send_debug_message)
89+
90+
# Keep the program running
91+
while True:
92+
pass
93+
```
94+
95+
### Expected Output
96+
When connected to a serial monitor, you should see:
97+
98+
```
99+
UART says Hi!
100+
UART says Hi!
101+
UART says Hi!
102+
```
103+
![UART Example](assets/UARTHello.png)
104+
105+
If you don’t see this output, check your connections and ensure the baud rate matches.
106+
107+
## Example 2: Ping-Pong Communication
108+
109+
Next, let’s make the device respond with `pong` when it receives the message `ping`. This demonstrates how to handle incoming UART messages.
110+
111+
### Circuit Diagram
112+
Use the same circuit as before. Ensure TX, RX, and GND are properly connected.
113+
114+
### Code Example
115+
116+
```python
117+
from machine import UART
118+
119+
# Initialize UART
120+
uart = UART(1, baudrate=9600, tx=43, rx=44)
121+
122+
buffer = ""
123+
124+
while True:
125+
if uart.any(): # Check if data is available
126+
ch = uart.read(1).decode() # Read one character
127+
uart.write(ch) # Echo the input
128+
buffer += ch
129+
130+
if ch == '\n': # End of message
131+
if buffer.strip().lower() == "ping":
132+
uart.write("pong\n")
133+
buffer = "" # Clear the buffer
134+
```
135+
136+
### Expected Output
137+
1. Type `ping` followed by `Enter` in your serial monitor.
138+
2. You should see:
139+
```
140+
pong
141+
```
142+
143+
Any other input will simply be echoed back to the sender.
144+

0 commit comments

Comments
 (0)