Thursday, 23 October 2014

ARDUINO SERIAL COMMUNICATION

Serial Communication in Arduino

Arduino UNO has a built in UART for serial communication. Rx and TX pins (0 & 1 respectively) in Arduino Uno can be used for communicating data serially with any peripheral device (like Bluetooth, GSM, GPS etc.). There are some built in functions that readers should understand before writing Arduino code for serial data transmission and reception. Some of the commonly used functions in serial data transmission and reception are discussed below-

Serial.println()
  
Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').
Syntax
Serial.println(val)
Serial.println(val, format)
Parameters
val: the value to print - any data type

format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

Returns
size_t (long): println() returns the number of bytes written, though reading that number is optional


Example-

Serial.println(“UMESH DUTTA”) will transmit UMESH DUTTA through the Tx pin of Arduino Uno.

Serial.println(78, HEX) gives "4E"


Serial.print()

Syntax

Serial.print(val)
Serial.print(val, format)

Parameters
val: the value to print - any data type

format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

Returns
size_t (long): print() returns the number of bytes written, though reading that number is optional.


For example-

Serial.print(78, BIN) gives "1001110"

Serial.println(1.23456, 2) gives "1.23"

Serial.print("INDIA") gives "INDIA"

Serial.write()
This function writes binary data to the serial port. This data is sent as a byte or series of bytes.

Syntax

Serial.write(val)
Serial.write(str)
Serial.write(buf, len)

Parameters

val: a value to send as a single byte
str: a string to send as a series of bytes
buf: an array to send as a series of bytes
len: the length of the buffer

Returns

byte

write() will return the number of bytes written, though reading that number is optional

Serial.write(32); // send a byte with the value 32
int bytesSent = Serial.write(“Delhi”); //send the string “Delhi” and return the length of the string.

Serial.begin()

Syntax

Serial.begin(speed)

Serial.begin(speed, config)

Description

Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

An optional second argument configures the data, parity, and stop bits. The default is 8 data bits, no parity, one stop bit.

Example-

Serial.begin(9600); // specifies 9600bpds baud rate for communication of data

Serial.available()

Syntax

Serial.available()


Description

Get the number of bytes (characters) available for reading from the serial port. This is the data that's already arrived and stored in the serial receive buffer (which holds 64 bytes). 

Example-

If some serial data is received and some action is required to be performed at that point of time then the coding can take the following form-

if (Serial.available()>0)
{
………….do something………….
}

Serial.read()

Syntax

Serial.read()

Decription

Reads incoming serial data. 

Returns

the first byte of incoming serial data available (or -1 if no data is available)

Example-

if (Serial.available()>0)
{
temp = Serial.read(); // reads the serially received data and stores it in temporary variable temp
}

Serial.end()

Syntax

Serial.end()

Description

Disables serial communication, allowing the RX and TX pins to be used for general input and output. To re-enable serial communication, call Serial.begin().

There are some other functions that are associated with serial communication and for understanding the left out functions I recommend you to refer the below given link-


Now after understanding most of the functions that are associated with serial communication in Arduino Uno, I will discuss with you some of the programming examples and after going through these examples you will get the answer of question - Where to use which function?

Problem1-

Two led’s are connected to pin number 13 (led1) and 12(led2) of arduino uno.
If serial data ‘1’ is received then led1 should glow, led2 should remain off.
If serial data ‘2’ is received then led2 should glow, led1 should remain off.
If serial data ‘3’ is received then both led1 and led2 should remain on.
If serial data ‘4’ is received then both led1 and led2 should remain off.

Circuit Diagram in proteus-





Code-

char temp;
void setup()
{
  Serial.begin(9600); // Baud rate of 9600bps
  pinMode(12,OUTPUT); //pin 12 declared as output
  pinMode(13,OUTPUT); // pin 13 declared as output
  delay(10); // delay of 10ms
}
void loop()
{
  while(!Serial.available()); // stay on this line if no data is received serially
  if(Serial.available()>0) // check if some data is available to be read
  {
    temp=Serial.read(); // read the incoming serial data byte and store in temp
    if(temp=='1')
    {
      digitalWrite(13,HIGH);
      digitalWrite(12,LOW);
    }
    if(temp=='2')
    {
      digitalWrite(13,LOW);
      digitalWrite(12,HIGH);
    }
    if(temp=='3')
    {
      digitalWrite(13,HIGH);
      digitalWrite(12,HIGH);
    }
    if(temp=='4')
    {
      digitalWrite(13,LOW);
      digitalWrite(12,LOW);
    }
}

The above given code was tested in proteus simulation software and on actual hardware. The code worked perfectly fine in both the cases.


A snap shot of proteus simulation during run time is shown below-


Caution-
Take Crystal frequency of 16MHz in proteus and for that connect a crystal oscillator b/w pin 9 and 10.

Double click on Arduino and a new window will pop up. In this window make sure that clock Frequence is 16MHz ad CKSEL Fuses is set at (0000)Ext. Clock. A snap shot of these settings is given below-


Problem 2

LM 35 temperature sensor output is connected to analog channel A0 of Arduino Uno. Show the digital output of the temperature on virtual terminal of proteus (Send the temperature readings serially after every 1 second).

Circuit diagram-


Code-

float value;
void setup()
{
  Serial.begin(9600); // begin serial communication with baud rate of 9600bps
}
void loop()
{
  Serial.print("temperature:");//print string temperature on serial port
  value=analogRead(A0); // read the analog voltage of channel A0
  value = ((value * 5000.0)/1024.0)/10.0;// software caliberation
  Serial.println(value,2); // print the temperature serially with 2 decimal places
  delay(1000); // delay of 1 second
}

A snap shot during proteus simulation is shown below-


I hope that these examples will prove to be useful for the readers in grasping the programming concepts of Arduino serial communication. These fundamentals will be required when you perform GSM module (SIM900) interfacing with arduino for SMS sending or receiving applications, GPS, Bluetooth and many other applications that require the use of serial communication. I will post the articles on all these real time applications very soon.

THANKS FOR READING…………
UMESH DUTTA

No comments:

Post a Comment