How To Print To Serial Monitor On Arduino

How To Print To Serial Monitor On Arduino

In this tutorial, I will show you how to use Arduino Serial Monitor effectively to print data in a helpful and time-saving way.

This article will cover all the tips and tricks in one place about printing the data to the serial terminal.

This tutorial will help you understand the intrinsics of the Serial.print() and Serial.println() and use them in your upcoming projects.

You will learn how to print the values in hexadecimal, octal, binary, as well as decimal. I will also show you how to print a certain number of decimal points when printing a float number.

I will give a step-by-step guide to print to the Serial Monitor and format the data to make it more readable and user-friendly!

Let’s get started.

Components Needed To Use Arduino Serial Plotter

Hardware Components

Software

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.


To try all the examples in this article, you only need an Arduino UNO connected to a PC.

No other hardware is necessary.

> Check out our guide to the Top 12 Best Arduino Online Courses

What Is An Arduino Serial Monitor?

Arduino Serial Monitor is a terminal window to print the data using print commands.

You can open the Arduino Serial Monitor easily in the following way.

You can open the monitor only when an Arduino is connected to the PC via a USB cable.

How do I connect Arduino to serial monitor?

So you may be wondering how to open the Serial Terminal on Arduino IDE.

Here are the steps and screenshots showing you the process:

Serial Terminal on Arduino IDE
  1. Click on Tools
  2. Select Serial Monitor out of the options

Here are the details of all the elements present in the Serial monitor window.

Serial monitor window
  1. Serial COM port number to which the Arduino is connected.
  2. A text field where you can type the data you want to send to the Arduino.
  3. Hit the Send button after entering the data in the text field.
  4. Serial monitor window where you see all the data you have printed to the terminal.
  5. If you enable Autoscroll, the text will automatically scroll up so that you can always see the latest message you have printed on the terminal.
  6. You can enable the timestamp option so that you can precisely see the time at which the messages were printed onto the terminal. Arduino IDE picks the time from the computer.
  7. Baud rate settings – You have to set this manually and match it with the baud rate you have set up in your Arduino sketch.
  8. If you click on the Clear Output button, the old messages you have printed onto the terminal will be cleared.

Step-By-Step Instructions To Print Serial Data To Arduino

I will show you various example projects of printing serial data in the following sections. I encourage you to browse through all the examples once.

Later, you can start tinkering with the code examples provided.

All about Serial.Print() and Serial.Println() Command

Serial.Print() commands print data to the terminal in a user-friendly human-readable format.

The serial command prints the data onto the terminal in ASCII format.

This helps you to visualize messages, floating-point numbers, and more. 

1)  How to print text onto the Serial terminal?

To print the text onto the serial terminal, you must enter the text within double-quotes. 

In this example, I will present the complete code. I will show only the serial print command formats for the later examples.

You can reuse the same example for all the remaining examples in the article.

void setup() {
  Serial.begin(9600);
  Serial.println("Printing text messages...");
}
 
void loop() {
 
}

Program the above sketch onto your Arduino. Open the Serial monitor window.

You will see the text messages you have printed on the Serial Monitor.

Printing the status messages

You will need to print text messages on several use cases, such as Printing the status messages, Debugging the projects, etc.

You should also note that the baud rate matches what you mentioned in the Arduino Sketch.

If you see some garbage values on the serial monitor, a mismatch in the baud rate can be the main reason. 

I will only present the serial print commands for the next series of examples.

2)  How to print a single character to the Serial Monitor?

To print a single character to the serial monitor, you can send the data in single quotes. An example is given below.

void setup() {
  Serial.begin(9600);
  Serial.print('P');
}
 
void loop() {
 
}
print a single character to the Serial Monitor

3)  How to print an integer number to the Serial Monitor?

To print an integer character to the serial monitor, you can send the data in single quotes. An example is given below.

  Serial.print(34);

You will see the integer as is on the serial monitor window.

number to the Serial Monitor

4)  How to print a floating-point number to the Serial Monitor?

A floating-point number will also have decimal points. You can follow the same steps you have followed to print an integer. Here is an example.

  Serial.print(3.14);
floating-point number to the Serial Monitor

-> Read our guide about What You Can Build with Adruino.

What is the difference between print() and println()?

The critical difference between Print and Println in the commands below is that the cursor moves to the following line after the printing task when you use the former one.

  Serial.print(3.14);
  Serial.println(22.6);

I will demonstrate it with an example. If we use two terminal print messages without using println() command, the data concatenates.

It will be challenging to understand the messages.

printed in two separate lines

Once you use the println() command, you can read two messages easily as they are printed in two separate lines.

Congratulations! That completes the basics of serial printing.

In the next section, I will show you more about formatting the data you print to the serial monitor.

How To Format And Print Data To The Serial Monitor?

In this section, I will share a few tips to format the data you print to the serial monitor. I will provide examples of all the cases.

I encourage you to try them and make us for these in your future projects.

1) How to print numbers in different number formats on to serial monitor?

It is easy to print the numbers in binary, hexadecimal, octal, and more on the serial monitor.

In the below example code, I will show you how to print a number in all the formats. 

I am using println() command so that every message will appear in a new line. 

Notice the use of both print() and println() commands to format the prints to the serial monitor.

void setup() {
  Serial.begin(9600);
  Serial.print("Printing 36 in hexadecimal: ");
  Serial.println(36, HEX);
  Serial.print("Printing 36 in Binary: ");
  Serial.println(36, BIN);
  Serial.print("Printing 36 in Octal: ");
  Serial.println(36, OCT);
  Serial.print("Printing 36 in decimal (no change): ");
  Serial.println(36, DEC);
}
 
void loop() {
 
}
print numbers in different number formats on to serial monitor
 Serial.println(36, HEX);

You can send the second parameter in the print messages. The second parameter will tell how the message must be printed on the serial monitor.

2) How to print decimal points while printing floating numbers?

You can format the print of floating-point numbers onto the serial monitor. The second parameter will tell the number of decimals printed onto the serial monitor. 

You can use the below code to tinker with the floating-point numbers.

In the below example, we mention the number of decimals printed onto the serial monitor window.

void setup() {
  Serial.begin(9600);
  Serial.println(3.14159, 0);
  Serial.println(3.14159, 1);
  Serial.println(3.14159, 3);
  Serial.println(3.14159, 5);
}
 
void loop() {
 
}
print decimal points

3) More tips on printing data to the serial monitor

There are a few more tips to print the data onto the serial monitor. To issue a tab space between the characters, you can use the below command.

void setup() {
  Serial.begin(9600);
  Serial.print("Column-1");  // prints a label
  Serial.print("\t");         // prints a tab
 
  Serial.print("Column-2");
  Serial.print("\t");
 
  Serial.print("Column-3");
  Serial.print("\t");
 
  Serial.print("Column-4");
  Serial.print("\t");
}
 
void loop() {
 
}

You can see the output on the serial monitor. Giving a tab helps you to print a table of data in a readable format.

tips on printing data to the serial monitor

4) What is the use of the Serial.write() command?

Serial.write() is used to print binary data to the serial monitor. It will print the data as bytes.

You can print individual bytes, a string, an array as well. 

Serial.write() command returns the number of bytes printed successfully.

The below example gives a demonstration. In the below example, 5 bytes are sent.

The variable bytesSent is printed to see the returned value on the serial monitor.

void setup() {
  Serial.begin(9600);
  int bytesSent = Serial.write("Welcome");
  Serial.println("");
  Serial.print("Bytes sent: ");
  Serial.print(bytesSent);
 
}
 
void loop() {
 
}

Arduino serial monitor output screenshot. 

Arduino serial monitor output screenshot

Here is another example: you use everything you have learned to create a lovely table and print it to the serial monitor.

everything you have learned to create a lovely table and print it to the serial monitor

Here is the code used to create the above pattern.

/*
  Uses a for loop to print numbers in various formats.
*/
void setup() {
  Serial.begin(9600); // open the serial port at 9600 bps:
}
 
void loop() {
  // print labels
  Serial.print("NO FORMAT");  // prints a label
  Serial.print("\t");         // prints a tab
 
  Serial.print("DEC");
  Serial.print("\t");
 
  Serial.print("HEX");
  Serial.print("\t");
 
  Serial.print("OCT");
  Serial.print("\t");
 
  Serial.print("BIN");
  Serial.println();        // carriage return after the last label
 
  for (int x = 0; x < 64; x++) { // only part of the ASCII chart, change to suit
    // print it out in many formats:
    Serial.print(x);       // print as an ASCII-encoded decimal - same as "DEC"
    Serial.print("\t\t");  // prints two tabs to accomodate the label lenght
 
    Serial.print(x, DEC);  // print as an ASCII-encoded decimal
    Serial.print("\t");    // prints a tab
 
    Serial.print(x, HEX);  // print as an ASCII-encoded hexadecimal
    Serial.print("\t");    // prints a tab
 
    Serial.print(x, OCT);  // print as an ASCII-encoded octal
    Serial.print("\t");    // prints a tab
 
    Serial.println(x, BIN);  // print as an ASCII-encoded binary
    // then adds the carriage return with "println"
    delay(200);            // delay 200 milliseconds
  }
  Serial.println();        // prints another carriage return
}

-> Learn more about How Easy Is It To Learn Arduino here.

FAQs On Printing Data To Arduino Serial Monitor

1) What are AT commands?

AT commands are attention commands used to interact with a co-processor. I have used AT commands to communicate with a GSM module in one of my earlier projects.

A sample of AT command will look like this:

What are AT commands

You can receive status and configuration information from other devices.

You can also configure various parameters of other devices via AT commands.

AT commands are sent to other modules (such as GPS coordinates, Network status, SMS availability, etc.)

You use Serial print commands to frame the message and send the data.

By connecting serial pins of Arduino with the other devices, you can establish a communication channel and interact with them using AT commands.

2) What is the difference between serial.print() and serial.println() in Arduino?

Both Serial.print() and Serial.println() will print the data to the serial terminal. println() starts the successive print to the serial terminal in a new line.

You can try this example for yourself to see it for yourself!

Serial.println(“”) will simulate hitting enter key on the keyboard. The remaining prints start appearing in a new line.

void setup() {
Serial.begin(9600);
 
Serial.print("Arduino ");
Serial.print("UNO");
 
Serial.println("");
 
Serial.print("I am in a new line");
}
 
void loop() {
 
}

3) What is the difference between serial write and serial Println in Arduino?

The Serial.write() and Serial.print(), both send data to the terminal. The Serial.write() command sends the data as is to the terminal.

This is useful when you have to set the configuration of a peripheral module or read the status from the registers.

On the other hand, the Serial.print() prints the data in the ASCII value format, which is easy to read by humans.

4) How do you add a new line in the serial terminal?

To add a new line in the serial terminal, you can use the below command in your Arduino code.

Serial.println("");

Conclusion

This article gave various benefits and examples of printing on an Arduino’s serial terminal.

I have shown the serial monitor window for all the examples to help you understand the examples easily.

I hope you can now efficiently use serial print commands to build Arduino projects. I have used the serial monitor in almost all of my Arduino projects.

They are an essential tool, especially when communicating with other devices such as Bluetooth, GSM, or WiFi modules (to send AT commands).

I would love to hear your feedback. Did you find this serial monitor data article beneficial?

Please share your feedback in the comments section. Feel free to share the projects you built using the serial monitor window.

I will be excited to know more about it. I will be glad to respond to all your comments as soon as possible. 

Would you like to recommend topics you are interested to learn?

I will be happy to hear it from you.