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 be printed for integral data types (byte, char, int, long, short, unsigned char, unsigned int, unsigned long, word). The permitted values are:
- BIN: binary, or base 2
- OCT: octal, or base 8
- DEC: decimal, or base 10
- HEX: hexadecimal, or base 16
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:
- The base (format) to be printed for integral data types (byte, char, int, long, short, unsigned char, unsigned int, unsigned long, word). The permitted values are:
- BIN: binary, or base 2
- OCT: octal, or base 8
- DEC: decimal, or base 10
- HEX: hexadecimal, or base 16
Return Values
- print() returns the number of bytes written, though reading that number is optional. Data type: size_t.
Example Code
Example 1
The result on Serial Monitor:
Example 2
The result on Serial Monitor:
※ 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
- Language : Arduino - Serial
- Language : Serial.available()
- Language : Serial.availableForWrite()
- Language : Serial.begin()
- Language : Serial.end()
- Language : Serial.find()
- Language : Serial.findUntil()
- Language : Serial.flush()
- Language : Serial.getTimeout()
- Language : if(Serial)
- Language : Serial.parseFloat()
- Language : Serial.parseInt()
- Language : Serial.peek()
- Language : Serial.println()
- Language : Serial.read()
- Language : Serial.readBytes()
- Language : Serial.readBytesUntil()
- Language : Serial.readString()
- Language : Serial.readStringUntil()
- Language : serialEvent()
- Language : Serial.setTimeout()
- Language : Serial.write()
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |