Interacting with the Serial Console

Whether you are using the Arduino send data to a Serial Console, or fetch
data from the Serial Console, you must initialize the Serial Console
in setup(), using the function Serial.begin(). This function sets
the speed (aka “baud rate”) with which the console transmits or receives
data from the Arduino board. Typical values are 9600, 57600, or 115200.
Whatever value you use, it must agree with the baud rate reported in the
bottom line of your Serial console; otherwise you will see no output:

Check the baud rate!

Either change the setting on your Console, or modify your
program so it agrees with the Console setting.

Printing Simple Values

This example just prints common types of values. Note that the loop() function
is empty, since this program is intended to be executed just once:

void setup() {
  Serial.begin(9600);
  while (!Serial) {};                // Wait for the Serial Console to be ready

  char a    = 'x';                   // char values must be enclosed in single quotes
  String b  = "Robotics and Beyond"; // String values must be enclosed in double quotes
  int x     = 15;                    // int's cannot exceed 32767
  int y     = 0x0F;                  // hexadecimal representation of 15.
  long z    = 1000000L;              // long int's can be in the billions
  float n   = 3.14;                  // approximate value of Pi
  double p  = M_PI;                  // more precise value of M_Pi using a built-in constant

  Serial.print("a is ");
  Serial.println(a);
  Serial.print("b is ");
  Serial.println(b);
  Serial.print("x is ");
  Serial.println(x);
  Serial.print("y is ");
  Serial.println(y);
  Serial.print("z is ");
  Serial.println(z);
  Serial.print("n is ");
  Serial.println(n);
  Serial.print("p is ");
  Serial.println(p);
}

void loop() {
}

The sketch above produces the following output on the Serial Console:

a is x
b is Robotics and Beyond
x is 15
y is 15
z is 10000000
n is 3.14
p is 3.14

Notice:

Changing the output format of an integer

The following sketch produces a table of numbers, where each
number is printed out in its decimal form (the default), then in its
hexadecimal equivalent, its octal equivalent, and its binary equivalent.

void setup() {
  Serial.begin(9600);
  while (!Serial) {};         // Wait for the Serial Console to be ready

  for (int i=0; i<=15; i++) {
    Serial.print(i);          // default format; prints the DEC value of i
Serial.print(" ");
Serial.print(i, HEX);     // prints the hexadecimal value of the same number
Serial.print(" ");
Serial.print(i, OCT);     // prints the octal value of the same number
Serial.print(" ");
Serial.println(i, BIN);   // prints the binary value of the same number
  }
}

The above sketch produces the following output:

0 0 0 0
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
10 A 12 1010
11 B 13 1011
12 C 14 1100
13 D 15 1101
14 E 16 1110
15 F 17 1111

Printing Floating Point Numbers with a Specified Precision

The default output precision produced by Serial.print() for a floating
point number is 2 digits after the decimal point. The following sketch
shows how to use a second argument to Serial.print() to adjust that.

void setup() {
  Serial.begin(9600);
  while (!Serial) {};       // Wait for the Serial Console to be ready

  float  mypi      = 3.14;  // an approximate value of Pi
  double builtinpi = M_PI;  // a more precise value of Pi


  for (int i=0; i<10; i++) {
    Serial.print(builtinpi, i);
    Serial.print(" ");
    Serial.println(mypi, i); 
  }
}

The above sketch produces the following output:

3 3
3.1 3.1
3.14 3.14
3.142 3.140
3.1416 3.1400
3.14159 3.14000
3.141593 3.140000
3.1415927 3.1400001
3.14159274 3.14000010
3.141592741 3.140000104

Note that when i <= 2, the value of mypi appears to be the same as
builtinpi, but when the numbers are printed at higher precision, the
difference becomes clear.