stickers_logo_text

Arduino Tutorial 05: Arduino Serial Port

Hello!!!!!!! and welcome back guys. In our last tutorials we have already learned about “for loop” and we have seen that how for loop simplifies our real life programming problems. If you still facing any problem so, please go to our previous tutorial Tutorial 04 and brush up your skills.

In this tutorial we are again going to use “for loop” and we will introduce you a new programming concept of arduino, that is  serial port programming. When we heard about serial programming obviously, first question comes into mind, that why we use arduino serial port programming. So, the answer is very simply. In our previous tutorial we have seen how to program your arduino uno board with the help of USB. but what if we want to display the arduino uno board information on computer screen or on Arduino serial monitor. or any other terminal. Then you have to use serial programming. Arduino serial port programming is very easy, adaptive and user friendly.

In order to use serial programming first you have to start serial programming in your program with the help of “Serial.begin(9600);” command. This command is responsible to open the serial port of your hardware. You have to initialize only once in “void setup” function. 9600 is the baud rate of communication between arduino uno and computer system. It tells us that how fast data is moving between arduino uno board and computer system.

In our very first experiment we are going to do a very basic “Hello World!” program. In this program are going to take “Hello World!” string from arduino uno board and print this string on serial monitor of arduino IDE.serial monitor     In the above mention image the blue color icon covered with red line is a serial monitor. When you click on this icon, it will open  a new window and the window is called serial monitor as you can see in the below mention image.

serial monitor2

Now, let us start with our first experiment, we are going to print a string “Hello World!” on the serial monitor with the help of arduino uno.

Source code

/* ========================================================
       Project : Serial Comminication
        Author : Jordan
       Created : 26th Nov 2015
   Description : Print "Hello World!" on serial monitor 
============================================================
*/

void setup() {
 // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
 // put your main code here, to run repeatedly:
 Serial.println("Hello World!");
 delay(250);

}

You can check the result on serial monitor as mention below,

serial monitor3

 

As you can see we have successfully completed our first arduino serial port communication experiment and as you can this program successfully print the  string “Hello World!” on serial monitor.

Now, let’s do something more advance. In our next experiment we are going to do a simple exercise. We are going to blink a led with the help of arduino uno and we are  going to print the for loop values on serial monitor, which indicates number of led blinking. Hope you remember the concept of for loop. But first we have to see the Requirements & circuit.

Requirements

  • 1 x Arduino Uno R3 Board
  • 1 x Laptop or pc
  • 1 x Bread board full size
  • 1 x Led(Red,5mm or 3mm)
  • 1 x 1KΩ Resistor
  • 1 x USB cable
  • Jumper wires

Connection Diagram

tutorial2_bb

Source code

/* ========================================================
       Project : Serial Comminication
        Author : Jordan
       Created : 26th Nov 2015
   Description : Print for loop value on serial monitor 
============================================================
*/
int led=13;

void setup() {
 // put your setup code here, to run once:
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop() {
 // put your main code here, to run repeatedly:
for(int i=1;i<=5;i++)
{
 Serial.println(i);
 digitalWrite(led,HIGH);
 delay(250);
 digitalWrite(led,LOW);
 delay(250);
}
Serial.println(" ");
}

You can check the result on serial monitor, as shown below.

s

We have successfully completed our second experiment as shown in the above image. Serial monitor shows how many time for loop is incremented at the time of led blinking.

In our last and final experiment we are going to do same experiment but in proper structured format. For this experiment there is no change in the hardware. We have to do some changes in the program. Let’s see

Source code

/* ========================================================
       Project : Serial Comminication
        Author : Jordan
       Created : 26th Nov 2015
   Description : Print how many time red led is blink 
============================================================
*/
int led=13;

void setup() {
 // put your setup code here, to run once:
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop() {
 // put your main code here, to run repeatedly:
Serial.println("Red led is start to blink");
for(int i=1;i<=5;i++)
{
 Serial.print(" The number of blinking: ");
 Serial.println(i);
 digitalWrite(led,HIGH);
 delay(250);
 digitalWrite(led,LOW);
 delay(250);
}
Serial.println(" ");
}

So as can check the result on serial monitor, as shown below,

serial monitor5

Now, burn the sketch in your arduino uno board and you will see the result on the serial monitor.

!!!!!!!!!!!!!!!!!!!!Congratulations Enjoy!!!!!!!!!!!!!!!!!! 

Hope you guys like our tutorial series. We will see you in the next tutorial with new programming and electronics concepts.

 

comments powered by Disqus

how can we help you?

Please leave your contact details, Our customer relationship manager will assist you.

get in contact
Be the Campus Ambassador of ELAN IIT Hyderabad!