Sunday 1 February 2015

Arduino Serial Port

Arduino Serial Port

UART/USART

       We will speak about Serial Communication on Arduino. Serial Communication means transmitting and receiving data on the same cable. To provide this we use UART (Universal Asynchronous Receiver Transmitter)/USART(Universal Synchronous Asynchronous Receiver Transmitter) communication units. Roughly, serial communication is the process to send data with starting bit, data bit, parity bit and stop bit. We can adjust the communication speed with the unit of bit speed which is called as BAUD RATE. Now, let's learn how to do it.
       Arduino lets us to communicate with a computer or any other device by using serial port. Actually, we use the serial port uncounsciously while loading the code to the Arduino. We can use serial port in different projects. We will make a simple application for this. Arduino presents us special codes to use for serial communication. Firstly, we should specify begining, baud rate and then the data which we will send or receive. Of course we want to see the results and Arduino has a screen to see them. We can see it by clicking on the burning glass on the main screen of Arduino IDE. It is on the upper right corner.



When we connected Arduino board to pc and clicked on the glass, this screen will be on.


It is possible to see the value of BAUD RATE on the lower right corner. We can adjust it from there. Pay attention BAUD RATE values on the screen and in the code must be same to send and receive data properly.

We will make an application to see the time on the serial port screen.



// we declare time variable to show the time
int time=0;

void setup() {

/* starting to serial communication use Serial.begin comand.
 The value which is between paranthesis, shows the baud rate */ 

Serial.begin(9600);

}

void loop() {
  
/* we use Serial.print command to have write what we want to see on the screen */
  
    Serial.print("Connection Time: "); 
  
/*Serial.println command has a small difference. it passes to next line after
ownself */

  
   Serial.println(time);
   time++;
  
  /* arduino uses milisecond as the time unit. 
 we use 1000 milisecond delay command for 1 second */
   delay(1000);

}

After loading the code to Arduino, we will see the connection time on the screen. We just added serial communication application to our projects. We will make higher level projects about this issue. Keep in touch!

If you liked this post please subscribe it to reach more people! Thank you :)

1 comment :

Thanks for your comments :)