Arduino

Serial.print() vs Serial.println() in Arduino

The Arduino is a popular microcontroller board that is commonly used for DIY projects, robotics, and IoT devices. One of the essential features of Arduino is to exchange data with computers via serial communication.

Serial.print() and Serial.println() are two of the most frequently used commands for serial communication in Arduino. This article will cover the differences between Serial.print() and Serial.println() and how they affect your Arduino project.

Table of Contents

What is Serial Communication

Before we dive into the differences between Serial.print() and Serial.println(), let’s first understand what serial communication is. Serial communication is the process of transmitting and receiving data. Using this Arduino can transfer one bit of data at a time, with the serial communication protocol. In Arduino, we use the Serial object to exchange data with a PC using a USB port.

Serial communication is essential for debugging and monitoring the behavior of an Arduino project. You can use it to print out sensor readings, debug code, or display messages on a computer screen.

Serial.print()

Serial.print() is a function that sends data to the Serial port in a continuous stream. It allows you to send data as a string, a character, or a numerical value. For example, the given code sends a string “Hello, World!” to Arduino Serial port:

Serial.print("Hello, World!");

Serial.print() does not add any line breaks or carriage returns at the end of the data, so the data is printed continuously on the same line.

Serial.println()

Serial.println() is similar to Serial.print(), but it adds a line break character (\n) at the end of the data. This shows us that every time Serial.println() function is called, the next print statement will start on a new line. For example, the given code sends the string “Hello, World!” and adds a line break:

Serial.println("Hello, World!");

This will print “Hello, World!” on the serial terminal of Arduino.

Difference Between Serial.print() and Serial.println()

The primary difference between Serial.print() and Serial.println() is that Serial.print() sends data in a continuous stream, while Serial.println() sends data with a line break at the end.

Now we will cover the example code that explains both these function workings.

Serial.print() Example

Following is the code that explains the usage of Serial.print():

void setup() {
  Serial.begin(9600); // initialize serial communication at 9600 baud rate
}

void loop() {
  int randomValue = random(0, 1023); // generate a random value between 0 and 1023

  Serial.print("Random value: "); // print the label
  Serial.print(randomValue); // print the random value on a new line

  delay(2000); // wait for 500 milliseconds before printing again
}

This code initializes the serial communication with a baud rate of 9600 in the setup() function. The loop() function then generates a random integer value between 0 and 1023 using the random() function and stores it in the variable name randomValue.

The Serial.print() function is then used to print the label “Random value: ” to the serial monitor, followed by the actual random value, which is printed on the same line without a new line character using Serial.print().

The delay() function is used to pause the execution of the loop for 2000 milliseconds (2 seconds).

Output
In the output, we can see all values are printed in a single line without any line break.

Serial.println() Example

The given code demonstrates the usage of the Serial.println() function in Arduino.

void setup() {
  Serial.begin(9600); // initialize serial communication at 9600 baud rate
}

void loop() {
  int randomValue = random(0, 1023); // generate a random value between 0 and 1023

  Serial.print("Random value: "); // print the label
  Serial.println(randomValue); // print the random value on a new line

  delay(2000); // wait for 500 milliseconds before printing again
}

The code for Serial.println() is similar to the above Serial.print() code. The only difference here is Random values which are generated and printed with a line break which is missing in the Serial.print() code.

Output
All values are printed in a new line as we have used Serial.print() instead of Serial.println():

Conclusion

Serial communication is an essential aspect of Arduino programming. The Serial.print() and Serial.println() functions are useful for showing data on the Arduino Serial terminal. Understanding the difference between them and using them appropriately can help you debug your code and communicate with external devices.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.