2.3 What is a serial port

Before writing code, you need to figure out what a serial port is.

Various encyclopedias have given answers to this question, but for beginners, these words are inevitably somewhat official. In layman's terms, the serial port is the channel for communication between chips.

Generally speaking, we use the UART serial port on the development board to communicate, which uses two signal lines for communication, one named TX (message sender) and the other named RX (message receiver), and their responsibilities are very single.

TX: can only send messages

RX: can only listen to messages

So you should cross-connect TX and RX when using:

 

In addition, when the serial port is in use, if there is no additional cable to give a synchronous clock signal, the baud rate of the serial port needs to be specified, which is equivalent to the agreement between the two chips: I said that I can output 100 binary signals per minute, So on average, the duration of each signal is 1/100 of a second, and you only need to receive it every 1/100 of a second to keep up with me.

Of course, there are some loopholes in the analogy here, and the actual communication process will be slightly more complicated to ensure the reliability and correctness of the communication.

Let's start practicing now! Due to space constraints, you can refer to the API manual for the specific functions used.

2.4 Send HelloGitHub

Here, Arduino has prepared Serial (serial port) for us, and it only takes a few simple steps to send messages.

Here is an official Arduino library, the Serial object prepared for us. If you don't understand C++ object-related concepts and it doesn't affect your use, a syntax like Serial.begin() is equivalent to a function call (or more officially called a "method"), it's just a function specific to Serial. code show as below:

#include

void setup()
{
// Set the baud rate to 9600, and keep the same when our computer reads
Serial.begin(9600);
}

void loop()
{
// Serial output line Text, will automatically add line breaks
Serial.println("HelloGitHub");
// Wait for a while to prevent sending too fast and get stuck
delay(1000);
}

"Burn" the above program into the development board, and then click "Serial Monitor" (a browser-like developer mode) to view the serial port.

 

At this point, you can see the message sent in the console:

 

2.5 Receiving messages

Now that we've covered how to send, let's talk about how to make Arduino receive messages from a computer or whatever.

Here's a piece of "echo" code:

#include

int count; // record the number of bytes in the
buffer char buffer[65]; // store the characters read from the buffer

void setup()
{
// do some initialization
Serial.begin(9600 );
count = 0;
}
void loop()
{
// This Serial.available() will return how many characters are currently accepted and stored in the buffer
count = Serial.available();
    
if (count > 0) // if there is something in the buffer
{
// read count characters into buffer
Serial.readBytes(buffer, count);
// add end, for sending later
buffer[count] = '\0 ';
// Send
Serial.println(buffer);
}
// Give a little time to receive more messages, otherwise we can only return letter by letter
delay(800);
}

Burn the above program and check the serial port according to the previous method. You need to enter the content to be sent in this box, and then use the shortcut key Ctrl+Enter to send.

 

Here we will see in the console that the message is sent to the computer after the board receives the message sent by the computer. The received message is exactly the same as the sent message, so it is called "echo".

So far, the basic use of the serial port is finished. Although the functions implemented here are very simple, this is the only way for all great gods. All complex functions are actually implemented with these seemingly simple basic functions.

3. Entry

3.1 Community

Arduino has a very active Chinese community, and most of the questions from newcomers can be answered enthusiastically by netizens. There are still many people in the community who share their own works, and even a novice with zero foundation can grow quickly here.

 

3.2 Exhibition of works

Ultrasonic obstacle avoidance car

 

Three degrees of freedom robotic arm

 

The production of simple password lock

 

Multifunctional Transparent Display Desk Station

 

I believe that you can also make such cool electronic gadgets in the near future.