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:

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.2345"

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)

Parameter Values

  • 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.
  • format: (optional) specifies:

Return Values

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

Example Code

Example 1

void setup() { Serial.begin(9600); // open the serial port at 9600 bps: // prints character Serial.print('A'); Serial.print('R'); Serial.print('D'); Serial.print('U'); Serial.print('I'); Serial.print('N'); Serial.print('O'); Serial.print('\n'); // prints a new line character Serial.print('\n'); // prints a new line character // prints a string Serial.print("ArduinoGetStarted.com"); Serial.print("\n\n"); // prints a string with two new line characters // prints a floating point number float a = 1.23456; Serial.print(a); // prints 2 decimal places by default Serial.print('\n'); // prints a new line character Serial.print(a, 4); // prints 4 decimal places Serial.print('\n'); // prints a new line character Serial.print(a, 5); // prints 5 decimal places Serial.print('\n'); // prints a new line character Serial.print(a, 6); // prints 6 decimal places Serial.print("\n\n"); // prints a string with two new line characters int x = 77; // prints an integral number in diffrent format in a table Serial.print("DEFAULT"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x); // prints as an ASCII-encoded decimal by default - same as "DEC" Serial.print('\n'); // prints a new line character Serial.print("DEC"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x, DEC); // prints as an ASCII-encoded decimal Serial.print('\n'); // prints a new line character Serial.print("HEX"); // prints a string Serial.print('\t'); Serial.print(x, HEX); // prints as an ASCII-encoded hexadecimal Serial.print('\n'); // prints a new line character Serial.print("OCT"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x, OCT); // prints as an ASCII-encoded octal Serial.print('\n'); // prints a new line character Serial.print("BIN"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x, BIN); // prints as an ASCII-encoded binary } void loop() { }

The result on Serial Monitor:

COM6
Send
ARDUINO ArduinoGetStarted.com 1.23 1.2346 1.23456 1.234560 DEFAULT 77 DEC 77 HEX 4D OCT 115 BIN 1001101
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Example 2

void setup() { Serial.begin(9600); // open the serial port at 9600 bps: Serial.print("DEFAULT"); // prints a string Serial.print('\t'); // prints a tab character Serial.print("DEC"); // prints a string Serial.print('\t'); // prints a tab character Serial.print("HEX"); // prints a string Serial.print('\t'); Serial.print("OCT"); // prints a string Serial.print('\t'); // prints a tab character Serial.print("BIN"); // prints a string Serial.print('\n'); // prints a new line character // only part of the ASCII table, change as desired for (int x = 0; x <= 16; x++) { // print it out in many formats: Serial.print(x); // prints as an ASCII-encoded decimal by default - same as "DEC" Serial.print('\t'); // prints a tab character Serial.print(x, DEC); // prints as an ASCII-encoded decimal Serial.print('\t'); // prints a tab character Serial.print(x, HEX); // prints as an ASCII-encoded hexadecimal Serial.print('\t'); // prints a tab character Serial.print(x, OCT); // prints as an ASCII-encoded octal Serial.print('\t'); // prints a tab character Serial.print(x, BIN); // prints as an ASCII-encoded binary Serial.print('\n'); // prints a new line character delay(50); // delay 50 milliseconds } } void loop() { }

The result on Serial Monitor:

COM6
Send
DEFAULT DEC HEX OCT BIN 0 0 0 0 0 1 1 1 1 1 2 2 2 2 10 3 3 3 3 11 4 4 4 4 100 5 5 5 5 101 6 6 6 6 110 7 7 7 7 111 8 8 8 10 1000 9 9 9 11 1001 10 10 A 12 1010 11 11 B 13 1011 12 12 C 14 1100 13 13 D 15 1101 14 14 E 16 1110 15 15 F 17 1111 16 16 10 20 10000
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTES AND WARNINGS:

  • For information on the asyncronicity of Serial.print(), see the Notes and Warnings section of the Serial.write() reference page.
  • Serial functions are not only used for the communication between an Arduino board and Serial Monitor of Arduino IDE but also used for the communication between:
  • An Arduino board and other Arduino board
  • An Arduino board and other sensors/devices
  • An Arduino board and computer (any Serial software on computer)

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

※ OUR MESSAGES