The Serial.print command lets you see what's going on inside the Arduino from your computer.
For example, you can see the result of a math operation to determine if you are getting the right
number. Or, you can see the state of a digital input pin to see if the Arduino is a sensor or switch
properly. When your interface circuits or program does not seem to be working, use the
Serial.print command to shed a little light on the situation. For this command to show anything,
you need to have the Arduino connected to the host computer with the USB cable.
For the command to work, the command Serial.begin(9600) must be placed in the setup()
function. After the program is uploaded, you must open the Serial Monitor window to see the
response.
There are two forms of the print command. Serial.print() prints on the same line while
Serial.println() starts the print on a new line.
Here is a brief program to check if your board is alive and connected to the PC
void setup()
{
Serial.begin(9600);
Serial.println("Hello World");
}
void loop() {}
Here is a program that loops in place, displaying the value of an I/O pin. This is useful for
checking the state of sensors or switches and to see if the Arduino is reading the sensor properly.
Try it out on your Arduino. After uploading the program, use a jumper wire to alternately
connect pin 2 to +5V and to Gnd.
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(digitalRead(2));
delay(100);
}
If you wanted to see the states of pins 2 and 3 at the same time, you can chain a few print
commands, noting that the last command is a println to start a new line.
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("pin 2 = ");
Serial.print(digitalRead(2));
Serial.print("
Serial.println(digitalRead(3));
pin 3 = ");
20
Need help?
Do you have a question about the uno and is the answer not in the manual?
Questions and answers