Description
Send data to the serial port in the form of human-readable ASCII code. This function has multiple formats: each digit of the integer will be sent in the form of ASCII code; decimal numbers are also sent in the form of ASCII code, but only two decimal places are reserved by default; byte data will be sent as a single character; characters and strings will be sent in their corresponding forms.
E.g:
- Serial.print(78) sends “78”
- Serial.print(1.23456) sends “1.23”
- Serial.print(‘N’) sends “N”
- Serial.print(“Hello world.”) sends “Hello world.”
This function can also specify the format of the data through additional parameters. The allowed values are: BIN (binary), OCT (octal), DEC (decimal), HEX (hexadecimal). For floating-point numbers, this parameter can specify the number of decimal places.
E.g:
- Serial.print(78, BIN) sends “1001110”
- Serial.print(78, DEC) sends “78”
- Serial.print(1.23456, 0) sends “1”
- Serial.print(1.23456, 2) sends “1.23”
- Serial.print(1.23456, 4) sends “1.2346”
Syntax
Serial.print(val, format)
Example code
int x = 0;
void setup() {
Serial.begin(9600);
}void loop() {
// print labels
Serial.println(“NO”);
Serial.print(“Format”);
Serial.print(“\t”);Serial.print(“DEC”);
Serial.print(“\t”);Serial.print(“HEX”);
Serial.print(“\t”);Serial.print(“OCT”);
Serial.print(“\t”);Serial.print(“BIN”);
Serial.println();for(x=0; x< 64; x++){
Serial.print(x);
Serial.print(“\t”);Serial.print(x, DEC);
Serial.print(“\t”);Serial.print(x, HEX);
Serial.print(“\t”);Serial.print(x, OCT);
Serial.print(“\t”);Serial.println(x, BIN);
//
delay(200);
}
Serial.println(“”);
}
Result