0
0
AutocadHow-ToBeginner · 3 min read

How to Print Float Value to Serial Monitor in Arduino

To print a float value to the serial monitor in Arduino, use Serial.print() or Serial.println() with the float variable and specify the number of decimal places as the second argument. For example, Serial.println(myFloat, 2); prints the float with two decimal places.
📐

Syntax

The basic syntax to print a float value to the serial monitor is:

  • Serial.print(floatValue, decimalPlaces);
  • Serial.println(floatValue, decimalPlaces);

Here, floatValue is the float variable you want to print, and decimalPlaces is an optional number that sets how many digits appear after the decimal point.

arduino
Serial.print(floatValue, decimalPlaces);
Serial.println(floatValue, decimalPlaces);
💻

Example

This example shows how to print a float variable with two decimal places to the serial monitor. It initializes the serial communication at 9600 baud and prints the float value repeatedly every second.

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

void loop() {
  float temperature = 23.45678;
  Serial.print("Temperature: ");
  Serial.println(temperature, 2); // Print float with 2 decimal places
  delay(1000); // Wait 1 second
}
Output
Temperature: 23.46 Temperature: 23.46 Temperature: 23.46 ...
⚠️

Common Pitfalls

Common mistakes when printing floats to the serial monitor include:

  • Not specifying the number of decimal places, which prints many digits and can look messy.
  • Using Serial.print() without Serial.begin() in setup(), so nothing appears.
  • Confusing Serial.print() and Serial.println() — the latter adds a new line after printing.
arduino
/* Wrong way: prints many decimals and no new line */
void setup() {
  Serial.begin(9600);
}

void loop() {
  float value = 3.14159;
  Serial.print(value); // prints 3.14159...
  delay(1000);
}

/* Right way: specify decimals and use println for new line */
void setup() {
  Serial.begin(9600);
}

void loop() {
  float value = 3.14159;
  Serial.println(value, 2); // prints 3.14 with new line
  delay(1000);
}
📊

Quick Reference

Tips for printing floats to the Arduino serial monitor:

  • Always call Serial.begin(baudRate) in setup().
  • Use Serial.println(floatVar, decimals) to print with fixed decimal places and a new line.
  • decimalPlaces can be 0 to 6; more than 6 is ignored.
  • Use Serial.print() if you want to continue printing on the same line.

Key Takeaways

Use Serial.print() or Serial.println() with two arguments to print floats with decimal precision.
Always start serial communication with Serial.begin() in setup().
Specify the number of decimal places to control float output formatting.
Serial.println() adds a new line after printing; Serial.print() does not.
Decimal places can be set from 0 to 6 for float printing.