Serial.print()

Description

Prints data to 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-

An optional second parameter specifies 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 number of decimal places to use. For example-

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 list of available serial ports for each board on Serialmain page .
val : value to print. Allowed data types: any data type.

Returns

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

Example Code

/*
Uses a for loop to print numbers in various formats.
*/
/*F********************************************************************
*
**********************************************************************/
void 
setup() 
{
    Serial.begin( 9600 ); // open serial port at 9600 bps:
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{
    // print labels
    Serial.print( "NO FORMAT");  // prints a label
    Serial.print( "\t");         // prints a tab

    Serial.print( "DEC");
    Serial.print( "\t");

    Serial.print( "HEX");
    Serial.print( "\t");

    Serial.print( "OCT");
    Serial.print( "\t");

    Serial.print( "BIN");
    Serial.println();        // carriage return after last label

    for( int x = 0; x < 64; x++) 
    { // only part of ASCII chart, change to suit print in many formats:
        Serial.print( x );  // print as ASCII-encoded decimal - same as "DEC"
        Serial.print( "\t\t");  // prints two tabs to accomodate label length

        Serial.print( x, DEC);  // print as an ASCII-encoded decimal
        Serial.print("\t");    // prints a tab

        Serial.print(x, HEX);  // print as an ASCII-encoded hexadecimal
        Serial.print("\t");    // prints a tab

        Serial.print(x, OCT);  // print as an ASCII-encoded octal
        Serial.print("\t");    // prints a tab

        Serial.println(x, BIN);  // print as an ASCII-encoded binary
        // then adds carriage return with "println"
        delay(200);            // delay 200 milliseconds
    }
    Serial.println();        // prints another carriage return
}

Notes and Warnings

For information on asyncronicity of Serial .print() , see Notes and Warnings section of Serial.write() reference page .

See also