Learn to code

ARDUINO & CODES

Previous Post
Using Switch Statements in Arduino
Next Post
Getting user input from the serial monitor with Arduino

Sending information to the serial monitor using serial print() with Arduino

in Arduino Serial Communication

In the tutorial, Arduino Tutorial: 2.1 Blink a LED you learned how to make your first program.  You wrote a program that would blink a LED on and off. In this tutorial, we will be using the same LED circuit as in Arduino Tutorial: 2.1 Blink a LED. If you do not already make this, you should go ahead and do it now.  

If you are very new to Arduino try these simple examples to get you started. If you are already familiar with the serial monitor feel free to jump ahead. In this first tutorial we will use the following functions to interact with the Serial Monitor:

Serial.begin(9600): You have already seen above, begin() is used to start or enable serial. Serial doesn’t work unless you initiate it. The program will compile OK but nothing will be sent out over serial. Remember to use the same baud rate at both ends.
Serial.print: can be used to display values in different base formats. For example as Hexadecimal or binary.
Serial.println(): I have already mentioned the difference between print() and println(). println() adds carriage return and new line characters (\r\n) and print() does not. But what does print() and println() actually do and how are they different to write()?

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
220 ohm resistor 220 ohm resistor × 1
Dupont Wires Dupont Wires × 2
LED LED × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Arduino Serial Monitor

The code below is a basic example of displaying text on the Serial Monitor. Connect the Arduino to your pc -> upload the sketch -> open the Serial Monitor and look at your results.

// Basic serial print example. 
void setup()
{
    Serial.begin(9600);
    Serial.print("Greetings from ArduinoPlatform");
}
 
void loop()
{
}

After uploading the sketch, open the serial monitor. If you see the “Greetings from ArduinoPlatform” message you have everything set up correctly.

If you do not see the message “Greetings from ArduinoPlatform” you need to figure out what is going wrong. If the sketch is uploaded corretly and the Arduino is powered then the most likely issue is the baud rate.

Be sure that the baud raute at the Arduino IDE and the sketch are the same. The baud rate used in the example is 9600.

If the baud rates are not the same there is a big chance that you will not see anything, or weird characters, on the Serial Monitor.

The baud rate is close connected to the command serial.begin(baud rate);.

Serial.println(): End of Line Characters

If you print two strings you will see that they appear together on a single line. You can change this by using the Serial.println() command.

// Basic serial print example. 
void setup()
{
    Serial.begin(9600);
    Serial.print("Greetings from ArduinoPlatform ");
    Serial.print("Have fun while learning");
}
 
void loop()
{
}

The sketch above will display two lines to the Serial Monitor next toe each other. If you would like to display each line of text on a new line you can add an “end of line character”. In the example above we did not tell the Serial Monitor to start a new line after “Greetings from ArduinoPlatform”.

There a re two line of end characters that can be used to achieve this. These commands are not visible on the Serial Monitor, they just tell the Serial Monitor to start of the next line.

1 – add “\r\n” to what we are printing, or
2 – use Serial.println()

Both produce the same results and Serial.println() simply adds the “\r\n” characters to what it is printing.

 

// Basic serial print example. Serial_Example_003
 
void setup()
{
    Serial.begin(9600);
    Serial.println ("Greetings from ArduinoPlatform ");
    Serial.println ("Have fun while learning");

}
 
void loop()
{
}

After uploading the code above you will see that the two messages are displayed on seperate lines.

BreadBoard Layout

Sending data to the Serial Monitor

// Sketch: Blinking an LED
int pinLed = 10; // declare pin10 as ledpin
void setup()
{
  pinMode(pinLed, OUTPUT);  // set pin10 as output pin
  digitalWrite(pinLed, LOW); // set the pin value on low at the begin
  Serial.begin(9600);
}
void loop()
{
  Serial.println("LED is turned on "); // print to the serial monitor
  digitalWrite(pinLed, HIGH); // turn the LED on
  delay(1000); // delay for 1 second
  Serial.println("LED is turned off "); // print to the serial monitor
  digitalWrite(pinLed, LOW); // turn the LED off
  delay(1000); // delay for 1 second

Now download and run the code and look at your serial monitor. You should see those words printing each time before the Red LED blinks.

So this code will announce that the LED is blinking. Your Serial Monitor should now be looking something like this as it tells the user if the LED is On or Off.

Notice that each time you use Serial.println it goes to the next line. If you want to print to the same line, without advancing to the next line, you should use the command Serial.print.

So, we need to carefully consider when it suitable to use Serial.print vs. when we should use Serial.println.

The first statement is Serial.print(“LED is turned on “); which prints the statement Led is turned on.

The second statement is  Serial.println(“LED is turned off “);which prints the statement LED is turned off.

Do you see the difference between the coding of the two statements? The second statement adds some markup with to the printed string. It will ensure that the statement is printed on the second line and not next to the first printed statement. If you are using the same statement multiple times in your sketch we can simplify the code by making a variable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu