Arduino

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

Blog » | Forum » | Playground »

## Serial.print(data)

#### Description

Prints data to the serial port.

#### Parameter

data: all integer types, including char

#### 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:

    /*
      Analog input

     reads an analog input on analog in 0, prints the value out.

     created 24 March 2006
     by Tom Igoe
     */

    int analogValue = 0;    // variable to hold the analog value

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

    void loop() {
      // read the analog input on pin 0:
      analogValue = analogRead(0);

      // print it out in many formats:
      Serial.print(analogValue);         // print as an ASCII-encoded decimal
      Serial.print("\t");                // print a tab character
      Serial.print(analogValue, DEC);    // print as an ASCII-encoded decimal
      Serial.print("\t");                // print a tab character
      Serial.print(analogValue, HEX);    // print as an ASCII-encoded hexadecimal
      Serial.print("\t");                // print a tab character
      Serial.print(analogValue, OCT);    // print as an ASCII-encoded octal
      Serial.print("\t");                // print a tab character
      Serial.print(analogValue, BIN);    // print as an ASCII-encoded binary
      Serial.print("\t");                // print a tab character
      Serial.print(analogValue/4, BYTE); // print as a raw byte value (divide the
                                         // value by 4 because analogRead() returns numbers
                                         // from 0 to 1023, but a byte can only hold values
                                         // up to 255)
      Serial.print("\t");                // print a tab character
      Serial.println();                  // print a linefeed character

      // delay 10 milliseconds before the next reading:
      delay(10);
    }

#### 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