Arduino Serial Print Multiple Variables

  1. Print Variable Values on Serial Monitor Using the Serial.print() Function in Arduino
  2. Print Variable Values on Serial Monitor Using Serial.println() Function in Arduino

In this tutorial we will discuss how to print variable values on the serial monitor using the Serial.print() and Serial.println() functions.

If you want to print one or more variable values on the serial monitor on the same line, you can easily do that using the Serial.print() function. This function takes a variable as an input argument and prints that variable on the serial monitor. If you are dealing with floating-point numbers, then you can also pass a second argument in the Serial.print() function, which defines the number of digits after the decimal place. You can print any data type variable with this function. For example, consider we want to print an integer on the serial monitor. See the example code below.

void setup(){
    int myInteger = 10;
    Serial.begin(9600);
    Serial.print(myInteger);
}

In the above code, myInteger is a variable of type int, which we printed on the serial monitor. You can change the variable value according to the given variable. Consider another example where we want to print a floating-point number on the serial monitor up to 2 decimal places. See the example code below.

void setup(){
    float myFloat = 1.1234;
    Serial.begin(9600);
    Serial.print(myFloat, 2);
}

myFloat is a variable of type float, which we printed on the serial monitor. You can change the variable value according to a given variable. In this case, the result will be 1.12 because we passed the second argument in the Serial.print() function, which defines the number of digits after the decimal place. You can change the number of digits according to a given variable. Now consider another example where we want to print multiple variables in the same line. See the example code below.

void setup(){
    int first = 100;
    int second = 200;
    Serial.begin(9600);
    Serial.print(first);
    Serial.print("\t");
    Serial.print(second);
}

In the above code, we are printing two numbers with a tab space between them. In the code, \t is used to print a tab space so that the result will be visible. You can print as many variables as you like, and you can also use different characters to separate them from one another, like a comma. Note that Serial.print() function only prints variables in a single line. If you want to print variables on multiple lines, you have to use the Serial.println() function.

If you want to print variables on different lines, you can do that easily using the Serial.println() function in Arduino. This function performs the same as the Serial.print() function with the difference that this function goes to the next line after printing the variable value. For example, consider you want to print multiple variables on different lines. See the example code below.

void setup(){
    int first = 100;
    int second = 200;
    Serial.begin(9600);
    Serial.println(first);
    Serial.println(second);
}

In the above code, the first variable will be printed on the first line and the second variable on the second line. You can use both of these function to get you desired output. For example, consider we want to print multiple values on multiple lines. See the example code below.

int first = 0;
int second = 50;
void setup(){
    Serial.begin(9600);
}
void loop(){
    while(first <= 50){
        Serial.print(first);
        Serial.print("\t");
        Serial.println(second);
        first = first+1;
        second  = second-1;
    }
    while(1){
       
    }
}

In the above code, we have printed two variables on the serial monitor with a tab space. The first variable will be from 0 to 50, and the second variable will be from 50 to 0.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Arduino Serial

  • Arduino Serial Read String
  • Arduino Clear Serial Monitor