Space In HIndi

 


Pages

Popular

SERIAL COMMUNICATION IN ARDUINO

SERIAL COMMUNICATION IN ARDUINO

Serial communication is the method with which Arduino board communicates with computer or other devices. This communication is nothing but the data transfer between two devices. ‘Serial’ means one after other. We will understand this concept practically in this chapter

Components Required: 
1. Arduino Uno 




Serial communication is process of data transfer one bit at a time sequentially over a communication channel. In this case USB cable is acting as communication channel between Arduino and computer. 

While uploading code from computer to Arduino you might have observed few LED blinking on board. These blinking LEDs are Tx and Rx LEDs representing serial communication pin status. Uploading code is one of the example of serial communication

Displaying “Hello World” on serial terminal

 Interfacing: Interfacing is really very simple. You just need to connect your Arduino board to your computer with USB cable, that’s all.

 Programming: In this program we will print “Hello World” string on serial monitor. In void setup() function we will initialize serial port. 9600 is the commonly used baud rate or data transfer speed. 

In void loop() we need to continuously keep on printing “Hello World” string. For better readability we will use delay of 1 second in between.

void setup() 
Serial.begin(9600); //Serial port initialization
 } 
void loop() 
{
 Serial.println("Hello World"); //Print string, add new line
 delay(1000); // Wait for a second 

After programming it, compile it and upload to the board. Now to open serial monitor click on right hand, upper corner button as highlighted below
After clicking on it you should see output like this.



Make sure baud rate is 9600 to display correct characters on serial monitor.


Controlling LED with keyboard 

In this code we will receive command from computer keyboard and accordingly toggle the LED state. If we send ‘1’ on serial port then LED should turn on and if we send ‘0’ on serial port LED should turn off. 

In this code we will receive command from computer keyboard and accordingly toggle the LED state. If we send ‘1’ on serial port then LED should turn on and if we send ‘0’ on serial port LED should turn off. 

 Interfacing: 


Logic is very clear from the above mentioned problem statement. 
In void setup() we need to initialize pin 4 as output pin. 
We also need to initialize serial port.
 Now in void loop() we need to continuously check on Arduino, is there any data came from computer over serial or not. 
If data is available on serial then next we need to check whether it is ‘0’ / ‘1’. We will check it with if…else statement.
 And make LED pin HIGH / LOW.

void setup() 
{
    pinMode(4, OUTPUT); //LED pin
    Serial.begin(9600); // Initialize serial
 }
 void loop() 
{
     if(Serial.available() >0 ) //If data available on Serial 
    {
     char data = Serial.read(); //read data
    if(data == ‘1’) //If data is 1
     {
         digitalWrite(4, HIGH); //turn LED on 
    }
 if(data == ‘0’) //If data is 0
    {
     digitalWrite(4, LOW); //turn LED off 
    }
 }    

Compile and upload sketch on board. Open serial monitor. Now to turn on LED we need to send ‘1’ on serial port. To do this, type 1 in command box and click on send. The LED will turn on.

Now turn on LED by sending ‘0’ 

1 comment: