It can also print number formats by converting those to ASCII.
Serial.print(42); // Outputs the ASCII string "42" to the serial port
Serial.print(1.2345); // Outputs "1.23"
By default, numbers are displayed in decimal and rounded to two decimal
places. You can change both of these. To print a specii c amount of decimal places,
just specify the number of digits after the l oating-point number to be displayed:
Serial.print(1.2345, 0); // Prints "1"
Serial.print(1.2345, 1); // Prints "1.2"
Serial.print(1.2345, 4); // Prints "1.2345"
To display numbers in different formats, you need to specify the numerical
type constant after the number. There are four possibilities: BIN for binary, DEC
for decimal, HEX for hexadecimal, and OCT for octal.
Serial.print(42, BIN); // Prints 0010 1010
Serial.print(42, DEC); // Prints 42
Serial.print(42, HEX); // Prints 2A
Serial.print(42, OCT); // Prints 52
print() prints data but does not append any special characters to the end of
the text. In ASCII, there are a number of these reserved characters. These are
escaped with a backslash ( \ ). For example, how would you print a quote that
has to reside in another quote?
Serial.print(""He said "Captain", I said "what""); // Compiler error
As far as the compiler understands this line, the text starts at the i rst quotation
mark, and ends at the second, so what is all this noise afterward? The compiler
won't understand and will ask you to correct the problem. To show that this is
a special character, you must i rst escape it.
Serial.print(""He said \"Captain\", I said \"what\"");
You need to escape characters like quotation marks, backslashes, and single
There are also other special ASCII characters to be aware of. Consider the
Serial.print("Imagination is more important than knowledge.");
At i rst glance, everything looks good. However, computers are extremely
good at doing exactly what you ask for, and nothing more. The result might not
quite be what you expect when viewed in a terminal: