Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72ad074

Browse files
Added suggestions
1 parent 379fef0 commit 72ad074

File tree

1 file changed

+26
-4
lines changed
  • content/micropython/06.communication/uart

1 file changed

+26
-4
lines changed

‎content/micropython/06.communication/uart/uart.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Each device directly connects TX to RX and RX to TX.
2626
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:
2727

2828
1. **Primary Board (Transmitter):** Runs the main UART code examples provided below.
29-
2. **Secondary Board (Receiver):** Reads from the UART interface and displays the output on a serial monitor like Arduino IDE, PuTTY, or CoolTerm.
29+
2. **Secondary Board (Receiver):** Reads from the UART interface and displays the output on a serial monitor like Arduino IDEor PuTTY.
3030

3131
### Circuit Setup
3232
- Connect the **TX1 (GPIO43)** of the primary board to the **RX** of the secondary board.
@@ -36,7 +36,7 @@ To observe the output of your UART communication, you’ll need a secondary boar
3636
![How to wire](assets/UARTTestingSetup.png)
3737

3838
### Code for Secondary Board
39-
The secondary board simply forwards received UART data to its serial monitor. Use the following code:
39+
The board simply forwards received UART data to its serial monitor. Use the following code:
4040

4141
```python
4242
from machine import UART
@@ -50,7 +50,6 @@ while True:
5050
print(ch, end="") # Print to the console
5151
```
5252

53-
### Using the Secondary Board
5453
1. Flash the above code to the secondary board.
5554
2. Open a serial monitor for the secondary board (e.g., Arduino Lab for Micropython, Arduino IDE Serial Monitor, Putty).
5655

@@ -68,7 +67,7 @@ For this example:
6867

6968
### Code Example
7069

71-
Copy the following code into your `main.py` file and run it:
70+
Copy the following code into your `main.py` file and run it on the board you designated as "primary":
7271

7372
```python
7473
from machine import UART, Timer
@@ -110,6 +109,8 @@ Use the same circuit as before. Ensure TX, RX, and GND are properly connected.
110109

111110
### Code Example
112111

112+
Copy the following code into your `main.py` file and run it on the board you designated as "primary":
113+
113114
```python
114115
from machine import UART
115116

@@ -129,6 +130,27 @@ while True:
129130
uart.write("pong\n")
130131
buffer = "" # Clear the buffer
131132
```
133+
Now copy the following code into your `main.py` file and run it on the board you designated as "secondary". In this case we are exanding the secondary board to be able to write via UART too:
134+
135+
```python
136+
from machine import UART
137+
import sys
138+
import
139+
140+
# Initialize UART
141+
uart = UART(1, baudrate=9600, tx=43, rx=44) # Adjust TX/RX pins as needed
142+
143+
while True:
144+
# Check if there's data coming from UART
145+
if uart.any():
146+
ch = uart.read(1).decode() # Read one character from UART
147+
print(ch, end="") # Print to USB serial monitor
148+
149+
# Check if there's data enselecttered in REPL
150+
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
151+
input_text = sys.stdin.read().strip() # Read user input
152+
uart.write(input_text + "\n") # Send via UART
153+
```
132154

133155
### Expected Output
134156
1. Type `ping` followed by `Enter` in your serial monitor.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /