Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Introduction of serial communication

in Arduino Serial Communication

In this tutorial, we will focus on serial communication with the Serial Monitor and some basic commands. The serial monitor in the Arduino IDE enables us to send and receive data from the Arduino board and the computer. This type of serial communication falls under hardware communication, as can be read in the introduction lesson.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Basic Example

The code below is a basic example of displaying text on the Serial Monitor. Connect the Arduino to your pc -> upload the sketch -> open the Serial Monitor, and look at your results.

// Basic serial print example. 
void setup()
{
    Serial.begin(9600);
    Serial.print("Greetings from ArduinoPlatform");
}
 
void loop()
{
}

This is a basic example of displaying text in the serial monitor. Connect the Arduino to a PC, upload the following sketch, open the serial monitor and be amazed…

After uploading the sketch, open the serial monitor. If you see the “Greetings from ArduinoPlatform” message, you have everything set up correctly.

If you do not see the message “Greetings from ArduinoPlatform” you need to figure out what is going wrong. If the sketch is uploaded correctly and the Arduino is powered, then the baud rate is the most likely issue.

Baud rate: When using serial communication, you need to make sure that you are using the same baud rate at both ends. So how do you know that? Well, you can tell that in your code. It is the value that you assign when using the Serial.begin(baudrate); command. In the sketch, we set the baud rate to 9600.

Serial.begin(9600); The code above tells the Arduino to open a hardware serial channel at 9600 baud rate. You should select the same value in the serial monitor.

There are many baud rates that you could choose from, as long as you choose the same baud rates for the connected devices you should be ok.

Serial.print(“Greetings from ArduinoPlatform”);

The Serial.print() command can is used to print data to the serial port as human-readable ASCII text. Numbers are printed using an ASCII character for each digit.

End of Line character

What if we wanted to print more information than one line to the serial monitor. Can we just simply copy the serial.print() command with some other information. Yes, you can. However, this will result that the printed commands will be displayed on the same line.

The code below demonstrates that.

// Basic serial print example. 
void setup()
{
    Serial.begin(9600);
    Serial.print("Greetings from ArduinoPlatform");
    Serial.print("ArduinoPlatform");
}
 
void loop()
{


}

So if you want to change, you could use some commands to print data on a new line. The code above does not tell the Serial Monitor to print a new line. We need to add an end-of-line character.

There are two lines of end characters:

Add rn to what you are printing or use Serial.println().

Both options will result in the same output.

// Basic serial print example. 
void setup()
{
    Serial.begin(9600);
    Serial.println("Greetings from ArduinoPlatform");
    Serial.print("ArduinoPlatform");
}
 
void loop()
{


}

Formatting in tabs

For quick and easy formatting, spacing can be used. However, the output can become a bit messy. A better solution to this problem is using the tab command “t”. This works the same way as tabs works in a word document. It moves the cursor over to the next column and lines out the output.

Using the tab means that columns stay aligned unless the value of the printed message is to large. Then it will move the cursor over to the next tab column.

If we apply this to the previous coding example the line, “ArduinoPlatform” is printed on the next column.

// Basic serial print with tabs
void setup()
{
  Serial.begin(9600);
  Serial.print("Greetings from ArduinoPlatform");
  Serial.print(“t / ”);
  Serial.println("ArduinoPlatform");
}

void loop()
{


}
Previous Post
Getting user input from the serial monitor with Arduino
Next Post
Serial communication between two Arduino Boards
You must be logged in to post a comment.
Menu