8. Chapter Serial Communication
Serial Communication is a means of Communication between different devices. This section describes Raspberry Pi Pico Serial Communication.
8.1. Project Serial Print
This project uses Raspberry Pi Pico serial communicator to send data to the computer and print it on the serial monitor.
8.1.1. Component List
Raspberry Pi Pico x1 |
USB cable x1 |
|---|---|
|
|
8.1.3. Circuit
Connect Raspberry Pi Pico to the computer with USB cable.
8.1.4. Sketch
8.1.4.1. Sketch_SerialPrinter
Download the code to Pico, open the serial port monitor, set the baud rate to 115200. As shown in the following picture:
As shown above, when the code runs, the data is printed every one second.
8.1.4.2. Reference
- void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL);
Initializes the serial port. Parameter baud is baud rate; other parameters generally use the default value.
- size_t println( arg );
Print to the serial port and wrap. The parameter arg can be a number, a character, a string, an array of characters, etc.
- size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
Print formatted content to the serial port in the same way as print in standard C.
- unsigned long millis();
Returns the number of milliseconds since the current system was booted.
For details, please refer to UART, I2C, SPI default pin .
You can change settings according to the distribution of pins.
8.2. Project Serial Read and Write
From last section, we use serial port on Pico to send data to a computer, now we will use that to receive data from computer. Component and circuit are the same as in the previous project.
8.2.1. Sketch
8.2.1.1. Sketch_SerialRW
Download the code to Pico, open the serial monitor, and set the bottom to Newline, 115200, as shown in the following picture:
Input texts like “ABCDEFG” in the Message Bar and press Enter to print the data received by Pico.
The following is the program code:
1/**********************************************************************
2 Filename : SerialRW
3 Description : Use UART read and write data between Pico and PC.
4 Auther : www.freenove.com
5 Modification: 2021/10/13
6**********************************************************************/
7String inputString = ""; //a String to hold incoming data
8bool stringComplete = false; // whether the string is complete
9
10void setup() {
11 Serial.begin(115200);delay(1000);
12 Serial.println(String("\nPico initialization completed!\n")
13 + String("Please input some characters,\n")
14 + String("select \"Newline\" below and click send button. \n"));
15}
16
17void loop() {
18 if (Serial.available()) { // judge whether data has been received
19 char inChar = Serial.read(); // read one character
20 inputString += inChar;
21 if (inChar == '\n') {
22 stringComplete = true;
23 }
24 }
25 if (stringComplete) {
26 Serial.print("input text: ");
27 Serial.print(inputString);
28 inputString = "";
29 stringComplete = false;
30 }
31}
In loop(), determine whether the serial port has data, if so, read and save the data, and if the newline character is read, print out all the data that has been read.
8.2.1.2. Reference
- String();
Constructs an instance of the String class.
For more information, please visit
https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/
- int available(void);
Get the number of bytes (characters) available for reading from the serial port. This is data that has already arrived and stored in the serial receive buffer.
- Serial.read();
Reads incoming serial data.

