Use Serial.print() to Display Arduino output on your computer monitor

In many cases while using an Arduino, you will want to see the data being generated by the Arduino. One common method of doing this is using the Serial.print() function from the Serial library to display information to your computer’s monitor.

In this lesson, you will learn the intricacies of the Serial.print() function.

In the following 2 video lessons on this page you’ll dive into using the Serial.print() function. Here are the specific topics we will cover in these lessons:

  • Why would you want to use the Serial.print() function?
  • A brief overview of the Serial library
  • The basic use of the Serial.print() function
  • Adjusting the number of digits to be displayed after the decimal point with the Serial.print() function.
  • Adjusting the format to display with the Serial.print() function
  • Formatting the output with text and tabs

Why Would You Want to Use the Serial.print() Function?

You may know that a function is a programming tool – it performs a specific task for you. The Serial.print() function’s task is to send information from your Arduino to your computer, so you can see the value displayed on your computer’s monitor.

There are an endless number of reasons you may want to send information from the Arduino to a computer display, but two reasons really stand out to me…

Programming Electronics Academy members, check out the Arduino Course for Absolute Beginners to practice using the Serial Library in your code.

Not a member yet?  Sign up here.

The first reason is being able to see information that you are generating with your Arduino.

For example, if you have a temperature sensor hooked up to your Arduino and you want to see the value that the temperature sensor is recording, then you can use the Serial.print() function to send the data to a computer monitor via the USB cable. If you open up the serial monitor window (Tools > Serial Monitor), you will see the values streaming in from the Arduino.

Image of serial monitor widow using Serial.print() with Arduino to send bits one at a time

The other big reason to send information to a computer display using the Serial.print() function is for developing and debugging Arduino sketches.

Very often, when you are developing an Arduino sketch, what you end up coding does something differently than what you expected it to do. Maybe you have a variable that gets incremented every so often and blinks an LED when it reaches a threshold. When you upload the code to the Arduino, you notice that the LED is blinking more often than it should.

You can look at the code until your eyes bleed, but actually visualizing the variable being incremented [via the Serial.print() function], to see its values every time through the loop() can help explain what is happening very quickly.

A Brief Overview of the Serial Library

We can’t talk about the Serial.print() function, without briefly talking about the Serial library.

Generally speaking, a library is simply a collection of functions that all have something in common.

The print() function is part of a library called the Serial library. Now, it’s not cereal like Cheerios or Captain Crunch we’re talking about – it’s serial as in “one after another”.

Serial.print() with Arduino send bits one at a time

The serial library allows us to interface the Arduino with other hardware, like a computer.

In order for us to use the functions of the Serial library, we have to initiate serial communication – to do this we use the Serial.begin() function. Serial.begin() needs to go in the setup().

void setup() {

  //Initiate Serial communication.
  Serial.begin(9600);

}

Now for reasons beyond the scope of this discussion, it is convenient to use the number 9600 in the Serial.begin() function. The value 9600 specifies the baud rate. The baud rate is the rate at which information will pass from the Arduino to the computer, or in the other direction.

The Basic Use of the Serial.print() Function

Let’s talk about how to use the Serial.print() function.

Say we have a sketch. This sketch has a variable called coolFactor.

I want to be able to monitor the value of the coolFactor variable – that is, I want it displayed on my computer screen. A perfect use for the Serial.print() function!

The first thing we must do in the Arduino sketch is begin serial communications. Like we just said, we use the Serial.begin() function and place it within the setup() of the sketch.

//A variable to hold the level of coolness
int coolFactor = 3;

void setup() {

  Serial.begin(9600);

}

void loop() {

  //Send the value of coolFactor to the the Serial port.
  //So we can see it in the serial monitor window
  Serial.print(coolFactor);

}

Now in the loop(), if I want to display coolFactor’s value with print(), I simply type Serial.print() and in the parenthesis I type the variable name.

Programming Electronics Academy members, use the coding challenges in the Bread and Butter: I/O section of the Arduino Course for Absolute Beginners to drive home these basic coding skills.

Not a member yet?  Sign up here.

If we upload this sketch to the Arduino, the value of coolFactor will be sent to the serial port every time through the loop(). In the Arduino IDE, if you open up the serial monitor window [Tools > Serial Monitor], you will see the values streaming down.

More Serial.Print() formatting

Displaying decimal points with Serial.print()

Let’s revisit the code from our last episode and make some critical adjustments. We’ve transformed the “cool factor” variable into a float, enabling decimal points, and set its value to 3.141592.

//A program to talk about the intricacies of the print() function

//A variable to track coolness
float coolFactor = 3.141592;

void setup() {

  //Initate Serial Communcartion.

  Serial.begin(9600);

}  //close setup

void loop() {
  // Send the value of coolness to the computer display
  Serial.print(coolFactor);
  Serial.print(" ");

  coolFactor = coolFactor + 1;

}  //close loop

Now, as we upload this code, observe how it appears on the serial port.

You’ll notice that it’s incrementing, yet it displays only two decimal places. By default, Serial.print shows two decimal places, but don’t worry; you can effortlessly adjust it.

When you send a decimal number via Serial.print, the function requires a second parameter. This parameter specifies the desired number of decimal places.

Let’s add a second parameter as the number 4. To clarify, the first argument is the number to display, and the second argument determines the decimal places to show.

  Serial.print(coolFactor, 4);

Let’s proceed to upload this and observe the serial monitor. All right, the number four has been sent, and now 4 digits are shown after the decimal point.

Achieving this is quite straightforward, you just add the second argument to serial.print().

If the first argument has the datatype of float (floating point number), then the second argument specifies the number of digits shown after the decimal point.

The print function also enables specifying the output format.

Programming Electronics Academy members, check out the Writing Functions section of the Arduino Course for Absolute Beginners to master coding your own functions.

Not a member yet?  Sign up here.

Outputting decimal, binary, hexadecimal with Serial.print()

What if you want to change the output format of the value on the Serial Monitor, can Serial.print() do that?

Yes it can! In fact, Serial.print() still takes two arguments, but in the case the second argument is the format you want to display.

The default output format from Serial.print() is decimal, which is base 10 for us humans to read and understand.

However, various other base formats are available:

  • DEC – decimal (base 10)
  • BIN – binary (base 2)
  • OCT – octal (base 8
  • HEX – hexadecimal (base 16)

The following code will display “cool factor” as a binary number:

  Serial.print(coolFactor, BIN);

Occasionally, you’ll find a need to display numbers in these different formats, and knowing how is pretty useful.

However, and even more useful thing to understand is some basic spacing techniques for your serial printing.

Printing multiple variable on a the same line

You’ll often find you want some “helper text” printed next to a variable value with your serial prints.

There are different ways to achieve this, but the simplest is just using multiple Serial.print() functions as demonstrated in the code below:

  Serial.print("Cool Factor = ");
  Serial.print(coolFactor);
  Serial.print(" ");

Note the quotes around “cool factor equals” to indicate that it should be sent as text. Without the quotes, the compiler would search for variables like “cool,” “factor,” and “equals,” leading to errors.

The quotes clarify that it’s meant to be interpreted as text.

Another way to format the text output is by including a tab, denoted by the “\t” . Now, we can see that a tab has been inserted between each output, providing some spacing and formatting.

Serial.print("\t");

Serial.println()

The final topic to cover is the Serial.println, know as the “print line” function. The primary distinction between the print and print line functions is that print line starts a new line at the end of its output.

  Serial.print("Cool Factor = ");
  Serial.println(coolFactor);

Serial.println() can take the same arguments as Serial.print() – again, the only difference is that it adds a new line after it’s output.

installing Arduino libraries

Installing Arduino Libraries | Beginners Guide

IoT sewage project

Pumping poo! An IoT sewage project

ESP32 webOTA updates

How to update ESP32 firmware using web OTA [Guide + Code]

error message Brackets Thumbnail V1

expected declaration before ‘}’ token [SOLVED]

Compilation SOLVED | 1

Compilation error: expected ‘;’ before [SOLVED]

Learn how to structure your code

11 Comments

  1. […] In this week’s episode we will talk about the intricacies of the print() function.  This is the second part of a two part series on the print() function (Click here for the first part). […]

  2. Rex Pimplemyer on February 18, 2018 at 8:21 pm

    Thank you very much for this video. I guess (as usual) everyone who made vids and blogs about the Arduino and sensors just assumed that no one would be so stupid not to know where the serial monitor was. I didn’t know it had one because no one mentioned it. I was wondering where the output from the DHT11 was supposed to be printing and you showed me what to look for. Thank you.

  3. NIBISHAKA on September 22, 2019 at 1:40 pm

    I’ve dealing with libraries without knowing what they really mean.Thank you very much for this tutorial.

  4. Andy Crofts on October 25, 2019 at 5:24 am

    “Looking at code until your eyes bleed”
    I’ve got around that. I use a technique called “Rubber Duck Debugging. Basically buy a small plastic bath duck.
    Put it next to the computer. Show it the code that doesn’t work, and explain in detail every line and variable. The duck doesn’t say anything, but the process slows down your thinking. After a few lines, you suddenly go “Aw, grief!!!”, and see your mistake clearly! Works well for me!

  5. Andrew on November 5, 2019 at 3:43 am

    Michael, cordial greetings from Poland 🙂 Your blog is great!

    • Michael James on November 5, 2019 at 9:47 am

      Thank you Andrew! I hope you can find it helpful.

  6. Claire on January 30, 2022 at 7:53 pm

    I’m brand new to Arduino and was playing around with the HR-SRO04 ultranosonic sensor and couldn’t figure out why I wasn’t seeing the print statements (“Ping: x cm”)….I suppose the NewPing documentation assumed the reader already knew to open up a Serial monitor. This post is exactly what I needed. Now I can read the output of my ultransonic sensor, thank you!

Leave a Comment