Serial.print(data)

Description

Prints a data to the serial port. This command can take many forms:

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

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

prints the 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 ASII character represented by the value 79. See the ASCII table for more.

Serial.print(str) if str is a string or an array of chars, prints str 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);
}


See also

Reference Home