Arduino

Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ

Blog » | Forum » | Playground »

## Serial.print(data)

#### Description

Prints data to the serial port.

#### Parameter

data: integer types, including char, floats
Printing of floats is supported with a precision of two places to the right of the decimal point.
This may change soon.

#### Syntax

This command can take many forms:

Serial.print(b) with no format specified, prints b as a decimal number in an ASCII string. For example,

    int b = 79;
    Serial.print(b);

prints the ASCII string "79".

Serial.print(b, DEC) prints b as a decimal number in an ASCII string. For example,

    int b = 79;
    Serial.print(b, DEC);

prints the string "79".

Serial.print(b, HEX) prints b as a hexadecimal number in an ASCII string. For example,

    int b = 79;
    Serial.print(b, HEX);

prints the string "4F".

Serial.print(b, OCT) prints b as an octal number in an ASCII string. For example,

    int b = 79;
    Serial.print(b, OCT);

prints the string "117".

Serial.print(b, BIN) prints b as a binary number in an ASCII string. For example,

    int b = 79;
    Serial.print(b, BIN);

prints the string "1001111".

Serial.print(b, BYTE) prints b as a single byte. For example,

    int b = 79;
    Serial.print(b, BYTE);

returns the string "O", which is the ASCII character represented by the value 79. For more information see the ASCII table.

Serial.print(str) if str is a string or an array of chars, prints str as an ASCII string. For example,

    Serial.print("Hello World!");

prints the string "Hello World!".

#### Parameters

b: the byte to print, or

str: the string to print

#### Returns

None

#### Example:

    /*
    Uses a FOR loop for data and prints a number in various formats.
    */
    int x = 0;    // variable

    void setup() {
      Serial.begin(9600);      // open the serial port at 9600 bps:
    }

    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.print("\t");

      Serial.println("BYTE");

      for(x=0; x< 64; x++){    // only part of the ASCII chart, change to suit

        // print it out in many formats:
        Serial.print(x);       // print as an ASCII-encoded decimal - same as "DEC"
        Serial.print("\t");    // prints a tab

        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.print(x, BIN);  // print as an ASCII-encoded binary
        Serial.print("\t");    // prints a tab

        Serial.println(x, BYTE);    // prints as a raw byte value,
        //                             then adds the carriage return with "println"
        delay(200);            // delay 200 milliseconds
      }
      Serial.println("");      // prints another carriage return
    }

#### Programming Tips / Known Issues

Serial.print() doesn't work on floats, so you'll need to cast them to an integral type, losing any fractional values. It is sometimes useful to multiply your float by a power of ten, to preserve some of this fractional resolution.

Be careful about doing math inside the brackets e.g.
`Serial.print(x-2, DEC);`
The unsigned char data type, and byte data type will yield incorrect results and act as though they are signed types i.e. type char.

The Serial.print function puts data into a buffer. It will wait for one character to send, before going on to the next character. However the function returns before sending the last character.

#### See also

- ASCII chart
- Serial.begin(speed)
- Serial.available()
- Serial.read()
- Serial.println(data)

Reference Home

Edit Page | Page History | Printable View | All Recent Site Changes