Skip to content

Commit e283fbf

Browse files
committed
Add serial / uart reference
1 parent 638234c commit e283fbf

File tree

1 file changed

+124
-18
lines changed

1 file changed

+124
-18
lines changed

content/micropython/01.basics/08.reference/reference.md

Lines changed: 124 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ For example:
7575
- [attachInterrupt()](#attachinterrupt)
7676
- [Attaches an interrupt to a pin with specified mode.](#attaches-an-interrupt-to-a-pin-with-specified-mode)
7777
- [detachInterrupt()](#detachinterrupt)
78-
- [digitalPinToInterrupt()](#digitalpintointerrupt)
79-
- [Interrupts](#interrupts)
80-
- [interrupts()](#interrupts-1)
81-
- [noInterrupts()](#nointerrupts)
8278
- [Communication](#communication)
83-
- [Print](#print)
84-
- [Serial](#serial)
79+
- [Serial (USB)](#serial-usb)
80+
- [print()](#print)
81+
- [Serial (UART)](#serial-uart)
82+
- [begin()](#begin)
83+
- [available()](#available)
84+
- [read()](#read)
85+
- [write()](#write)
8586
- [SPI](#spi)
8687
- [Stream](#stream)
8788
- [Wire](#wire)
@@ -907,22 +908,127 @@ while True:
907908
time.sleep(1)
908909
```
909910

910-
### digitalPinToInterrupt()
911-
<!-- TODO -->
912-
## Interrupts
911+
## Communication
913912

913+
## Serial (USB)
914914

915-
### interrupts()
916-
<!-- TODO -->
917-
### noInterrupts()
918-
<!-- TODO -->
919-
## Communication
915+
In the Arduino API, we are accustomed to using `Serial.begin()` and `Serial.print()` to send data from a board to a computer.
920916

917+
The same API is used for sending and receiving data over UART, using `Serial1`. For UART, see the [Serial (UART)](#serial-uart) section.
918+
919+
In MicroPython, to send data over USB, we can use the `print()` function, which prints the content to the REPL. As MicroPython is implemented a bit differently, the REPL also allows you to write commands inside it. The REPL is therefore in many ways, different from the Serial Monitor we are used to in the Arduino IDE.
920+
921+
922+
### print()
923+
924+
`print(content)`
925+
926+
This function prints the content to the REPL.
927+
928+
**Example:**
929+
930+
```python
931+
variable = 5
932+
933+
print("I am a string!") # prints a string
934+
print(variable) # prints value of variable
935+
print(58) # prints a numeric value
936+
print(f"The value is {variable}") # prints a string with a value inserted
937+
```
938+
939+
## Serial (UART)
940+
941+
UART communication is initalized using the `UART` object from the `machine` module.
942+
943+
### begin()
944+
945+
`machine.UART(port, baudrate=baud)`
946+
947+
Initialize UART communication on specified port (default is `1`), and specified baud rate.
948+
949+
**Example:**
950+
951+
```python
952+
import machine
953+
import time
954+
955+
uart = machine.UART(1, baudrate=57600)
956+
```
957+
958+
***Baud rate `57600` is used as using the common `115200` and `9600` interfered with the Arduino IDE's Serial Monitor.***
959+
960+
### available()
961+
962+
`uart.any()`
963+
964+
Checks if there's any data available on the UART port.
965+
966+
**Example:**
967+
968+
```python
969+
import machine
970+
import time
971+
972+
uart = machine.UART(1, baudrate=57600)
973+
974+
while True:
975+
if uart.any():
976+
print("data available")
977+
```
978+
979+
980+
### read()
981+
982+
`uart.read(1)`
983+
984+
Reads one byte from the buffer.
985+
986+
**Example:**
987+
988+
This example reads the first byte of data from the buffer, stores it into the `received_data` string. When a new line character (`\n`) is detected, the received message is printed to the REPL.
989+
990+
```python
991+
import machine
992+
import time
993+
994+
995+
uart = machine.UART(1, baudrate=57600)
996+
received_data = ""
997+
998+
while True:
999+
if uart.any():
1000+
data = uart.read(1)
1001+
received_data += data
1002+
1003+
if received_data and received_data[-1] == '\n':
1004+
print("Received:", received_data[:-1]) # Print the accumulated data (excluding the newline character)
1005+
received_data = "" # Reset the string after printing
1006+
1007+
time.sleep(0.1)
1008+
```
1009+
1010+
### write()
1011+
1012+
`uart.write(message)`
1013+
1014+
Writes / sends data over UART.
1015+
1016+
**Example:**
1017+
1018+
```python
1019+
import machine
1020+
import time
1021+
1022+
1023+
uart = machine.UART(1, baudrate=57600)
1024+
1025+
while True:
1026+
data_to_send = "Hello World!\n"
1027+
uart.write(data_to_send)
1028+
1029+
time.sleep(1)
1030+
```
9211031

922-
### Print
923-
<!-- TODO -->
924-
### Serial
925-
<!-- TODO -->
9261032
### SPI
9271033
<!-- TODO -->
9281034
### Stream

0 commit comments

Comments
 (0)