Serial Communication with Arduino

As soon as you get started with embedded programming, you will want to communicate information from your device (sensor readings, variable information, debugging), as well as be able to send data back to the board from your PC to control your device.

Serial is the best place to start, and allows data to be sent and received from both sides, with very little code.

 

Board to PC Communication

Begin Serial Connection

Note Icon Note:

On nearly all development boards, the Serial device is exposed to the PC via the USB lead used for programming, and is often also broken out as 2 pins (0/1 on Arduino Uno for example).  For now we will use it via the USB Lead as no additional hardware is required.

Note Icon Hardware Note:

The Serial port on your board works at lower voltages than the Serial Ports on your PC - so you cannot just wire those directly together.

 

Board Code:   Simply add Serial.begin(115200); into the Setup() function in your sketch.  Other transmission bit rates (often called baud) can be used, but 115200 is a good default for most cases.

Visual Micro:  We will need to set the same baud rate in the Serial Monitor when we open it, which can be seen in the bottom right corner as a drop down list (and can also be typed into for custom speeds).

 

Sending Messages Back to the PC

To get the board to send information back to the PC, we will have to initialize the Serial connection as above.

Below the Serial.begin(115200) we can add the below lines of code to send us a message every second...

Serial.println("Hello World");
delay(1000);

 

Note Icon Note:

As your board has nothing to do but transmit this message, it will be able to send characters much faster than your PC is able to process them. This is why the delay(1000) is added above to ensure we can only send 1 messages per second (and so we can read it...) , as it will repeat forever in loop().

 

Now Upload this code to your board, and open the Serial Monitor, remembering to select the matching baud rate as shown above.

You should now see your first message back from your board in the Serial Monitor, and then it will repeat each second...

Hello World - Visual Micro Serial Monitor Output

The Serial object has a variety of methods to allow you to print a string, or variable, in a number of formats, and println() puts the newline on the end of that statement for you.

Read More about the Arduino Serial functions here.

 

Sending Messages to the Board

To send messages to the board, we can simply type in the message at the top of the Serial Monitor Window, and press ENTER or the Send Button.

The Line Ending can be configured at the bottom of the window, and is shown above as "Both CR & LF", meaning both characters are added to the end of the transmitted message.

Receiving the Message On Board

In this example we will just send back "Hello " + message, for each message we were sent, so if we type in "World" in our Serial Monitor we should get "Hello World" as the reply.

To do this we need to check if there is any data in the Serial buffer to read, read until the end of a message (we will use the CR & LF as our terminator), and send back our "Hello " + message.

 

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    if (Serial.available()) {
        String message = Serial.readStringUntil('\n');
        Serial.print("Hello ");
        Serial.println(message);
    }
    delay(10);
}

 

 

Now we can communicate with our board, and our board can communicate with us, we can move onto adding some Inputs and Outputs to our project.