Wednesday, 3 February 2016

Serial Communication In Arduino #3

                     By the name Serial means "one after the other".   Arduino communicates with computer or any other external device by Serial Communication. This implies that Arduino sends data in terms of bits one after the other. This is called Serial Communication. For this purpose we need a minimum of one Serial Port (One port means 2 pins). One pin to receive data and the other to transmit(send). In Arduino Uno we have 1 Serial Port(0-RX, 1-TX). Digital pin 0 is receiver pin(RX) & 1 is transmitter pin(TX) ( marked red in the below figure).




For instance, consider Arduino has to send a character 'U' to the device attached. How does it send ? ?
We know that it can't understand 'U', it understands only binary language(0's and 1's). So 'U' should be converted to some combination of 0's and 1's. For this purpose, we have ASCI codes. The ASCI code of 'U' is 85 & so in order to transmit 'U', Arduino transmits 01010101 (i.e, binary equivalent of 85) through pin-01(TX). But at what speed? ?
This speed of transfer is mentioned in Serial.begin(x). Here x is the speed in bits/sec. Mostly we use x=9600 i.e, Serial.begin(9600). This means 9600 bits are transmitted per second. So, to transfer 1 bit, it takes (1/9600)=104 micro seconds. Also to transfer each byte, it uses two additional bits to indicate start and stop of that particular byte. As Arduino uses TTL Serial Communication, we have start bit as "0" & stop bit as "1". So to transmit 'U', Arduino sends a wave as shown in the below figure.




                                         **Note that here LSB is transmitted first.

So, for every byte of data transmitted, there are actually 10 bits being sent( 1 bit-start, 8 bits-data & 1 bit-stop).So at a baud rate of 9600 bps, it is actually transmitting (9600/10)=960 bytes of data.


Arduino is transmitting data but where is it going to be printed ?
There is a Serial monitor for this purpose. Once your code is successfully uploaded on your board, if you want to use serial communication, then open serial monitor window
1) Tools-->Serial Monitor  or  2)directly by clicking on Serial Monitor.




Once you click on the Serial Monitor, a window will be popped out as show below. This is Serial Monitor window.







You may notice that baud rate in the serial monitor and in the sketch must match to get the correct output. If they do not match you will get something like this





As said above, if Arduino Uno has only one Serial Port, then can we use more than one device to communicate with the Arduino??


                                             **Click here for the list of ASCI values


No comments:

Post a Comment