You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the Arduino API, we are accustomed to using `Serial.begin()` and `Serial.print()` to send data from a board to a computer.
920
916
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
+
whileTrue:
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
+
whileTrue:
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
0 commit comments