Serial.print()

Description

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. For example-

  • Serial.print(78)
    gives "78"
  • Serial.print(1.23456)
    gives "1.23"
  • Serial.print('N')
    gives "N"
  • Serial.print("Hello world.")
    gives "Hello world."

An optional second parameter specifies the base (format) to use; permitted values are

BIN(binary, or base 2)
,
OCT(octal, or base 8)
,
DEC(decimal, or base 10)
,
HEX(hexadecimal, or base 16)
. For floating point numbers, this parameter specifies the number of decimal places to use. For example:

  • Serial.print(78, BIN)
    gives "1001110"
  • Serial.print(78, OCT)
    gives "116"
  • Serial.print(78, DEC)
    gives "78"
  • Serial.print(78, HEX)
    gives "4E"
  • Serial.print(1.23456, 0)
    gives "1"
  • Serial.print(1.23456, 2)
    gives "1.23"
  • Serial.print(1.23456, 4)
    gives "1.2346"

You can pass flash-memory based strings to

Serial.print()
by wrapping them with F(). For example:

Serial.print(F("Hello World"))

To send data without conversion to its representation as characters, use Serial.write().

Syntax

  • Serial.print(val)
  • Serial.print(val, format)

Parameters

Serial
: serial port object. See the list of available serial ports for each board on the Serial main page.

val
: the value to print. Allowed data types: any data type.

Returns

print()
returns the number of bytes written, though reading that number is optional. Data type:
size_t
.

Example Code

1/*
2 Uses a for loop to print numbers in various formats.
3 */
4 void setup() {
5 Serial.begin(9600); // open the serial port at 9600 bps:
6 }
7
8 void loop() {
9 // print labels
10 Serial.print("NO FORMAT"); // prints a label
11 Serial.print("\t"); // prints a tab
12
13 Serial.print("DEC");
14 Serial.print("\t");
15
16 Serial.print("HEX");
17 Serial.print("\t");
18
19 Serial.print("OCT");
20 Serial.print("\t");
21
22 Serial.print("BIN");
23 Serial.println(); // carriage return after the last label
24
25 for (int x = 0; x < 64; x++) { // only part of the ASCII chart, change to suit
26 // print it out in many formats:
27 Serial.print(x); // print as an ASCII-encoded decimal - same as "DEC"
28 Serial.print("\t\t"); // prints two tabs to accomodate the label length
29
30 Serial.print(x, DEC); // print as an ASCII-encoded decimal
31 Serial.print("\t"); // prints a tab
32
33 Serial.print(x, HEX); // print as an ASCII-encoded hexadecimal
34 Serial.print("\t"); // prints a tab
35
36 Serial.print(x, OCT); // print as an ASCII-encoded octal
37 Serial.print("\t"); // prints a tab
38
39 Serial.println(x, BIN); // print as an ASCII-encoded binary
40 // then adds the carriage return with "println"
41 delay(200); // delay 200 milliseconds
42 }
43 Serial.println(); // prints another carriage return
44 }

Notes and Warnings

For information on the asyncronicity of

Serial.print()
, see the Notes and Warnings section of the Serial.write() reference page.

See also

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.