Arduino Projects    |   Raspberry Pi  |     Electronic Circuits   |    AVR    |    PIC    |    8051    |    Electronic Projects

How to Receive and Send Serial Data using Arduino

Summary

Developed By: 
Ajish Alfred
A microcontroller board can talk to the user by using LED display, buzzer, LCD screens etc., and the user can talk back to the microcontroller using switches, keyboard etc. There are few communication methods where the user and microcontroller can have a two-way communication using a single device or medium. The serial port of the microcontroller provides the easiest way by which the user and the microcontroller can write their data in the same medium and both can read each other’s data. When the serial port of the microcontroller is connected to the PC and the incoming and outgoing data is monitored using software and displayed in a window, it forms the simplest text user interface (TUI) setup for the microcontroller.
In such a system the user can send the data from the PC to the microcontroller’s serial port using software running in the PC, and can view the data which is send to the PC by the microcontroller in the same software. The microcontroller should have the capability to read the data send by the PC and send the data back to the PC. Normally the coding for enabling the serial data read and write in a microcontroller is difficult since the one who programs should have gone through all the register details of the microcontroller. Being an easy prototyping platform the arduino has built-in functions for accessing the serial port and hence there is no need to go to the register details. There are so many functions to send and receive data with and without formatting them in such a way to display in a serial monitoring software window.
This project demonstrates a simple program which can be used to receive an ASCII character send by the HyperTerminal of the PC and send the same data back to the PC.
How To Receive And Send Serial Data Using Arduino

Description

The AVR microcontroller boards which are provided with all the basic circuitry for the operation of the microcontroller which has been flashed with the arduino boot-loader are called arduino boards. The arduino board used in this project is the arduino pro-mini board and the IDE version of the arduino is 1.0.3 for windows. The image of the arduino pro-mini board and the arduino IDE are shown below;

Arduino

 

Arduino

Since the arduino pro-mini board has no circuitary for interfacing it with the serial port or the USB port of the PC, an external USB to TTL converter board is required to connect it with the PC. This hardware helps in programming the arduino board and also helps in the serial communication with the USB port of the PC.

Arduino

It is assumed that the reader has gone through the project how to get started with the arduino  and tried out all the things discussed there. The arduino IDE has so many built-in functions which help in the serial communication process and the details of a few functions for initializing the hardware and sending the data to the serial port are discussed in the previous project how to send data from the serial port of arduino. The functions like Serial.begin(),Serial.print() and Serial.println() were used in that project.

This particular project uses few more functions which helps in receiving the data from the serial port and which helps in sending the character value to the serial port without formatting them. The functions are namely ‘Serial.available()’, ‘Serial.read()’ and ‘Serial.write()’ whose details are discussed in the following section.

Serial.available()

This particular function is always called before reading a data byte from the serial port of the arduino. This function returns a positive value when the data byte reception is complete and otherwise it returns a non-positive value. It serves the purpose of waiting till the data byte reception is complete so that there should not be any error in reading the data byte.

The Serial.available() function can be used to simply check whether an entire data byte is received or not as shown in the following statement

                                                            If (Serial.available())

                                                            {

                                                                        ;

                                                            } else;

The code will enter the ‘if condition’ only when the one byte of data is completely received in the serial port of the arduino board.

The Serial.available() function can be used for waiting till an entire data byte is received as shown in the following statement

while (!Serial.available());

Please note the logic NOT symbol ‘!’ used with the function Serial available() so that the condition remain true till the data reception is complete and becomes false when after the completion of the data byte reception.

Serial.read()

The function Serial.read() is used to read a data byte from the serial port of the arduino. It can return the data byte which can then be stored in a variable or used for some condition check etc. The following statement shows how the data byte is read from the serial port and is stored into a variable.

var = Serial.read();

The above statement should always appear after making a function call to the Serial.available() to make sure that the data byte reception has been completed before reading the data. The statement reads the data byte and stores the data byte to a variable ‘var’. The variable could be of type integer or character.

Serial.write()

The Serial.write() is also a function which is used to send the data byte to the serial port of the arduino like the functions Serial.print() and Serial.println() explained in the previous project how to send data from the serial port of arduino. Unlike actually printing the data value in the serial port by sending the data byte after formatting it as ASCII character as the functions Serial.print() and Serial.println() does, the function Serial.write() simply sends the data byte to the serial port. For example the statement given below will print the character ‘A’ in the serial monitor software.

Serial.write(65);

It is possible since the ASCII value of the character ‘A’ is 65. Suppose if one use the value ‘10’ as the parameter of the function Serial.write() then it won’t display anything in the serial monitor software since there is no displayable ASCII character of the value 10, but if the same parameter 10 is used with the function Serial.print() as shown in the following statement will print ‘10’ in the serial port.

Serial.print(10);

THE CODE

The code written for this particular project initializes the serial port with a baud rate of 9600 using the function Serial.begin(). The code can check whether all the data bytes have been received or not before reading them with the help of the function Serial.available(). Once the Serial.available() function returns a positive value then the code reads the data from the serial port using the function Serial.read(). The code sends back the same value using the function Serial.print() to the serial port. The code written for the project is given below;

int led = 5;

// incoming serial byte

int inByte = 0;        

void setup()

{

  // initialize the led pin as an output.

  pinMode(led, OUTPUT); 

  // start serial port at 9600 bps

  Serial.begin(9600);

  // wait for a while till the serial port is ready

  delay(100);

// send the initial data once //   

  Serial.print('\n');

  Serial.print("    EG LABS   ");

  Serial.print('\n');

  Serial.print('\r');

  Serial.print("Serial Tx-Rx Demo");

  Serial.print('\n');

  Serial.print('\r');

  Serial.print('\n');

}

void loop()

{

  // if we get a valid byte, read analog ins:

  if(Serial.available())

  {

    // get incoming byte:

    inByte = Serial.read();         

    // send the same character back to serial port

     Serial.write(inByte);

    // blink the LED once //   

    digitalWrite(led, HIGH);      

    delay(100);    

  }

  else

    digitalWrite(led, LOW);  

}

Whenever a key is pressed in the keyboard the code receives the data byte and sends back the same byte which will appear in the serial port. The code also blinks an LED each time it transmits a data to the serial port using the functions pinMode (), digitalWrite () and delay(). The details of those functions are already discussed in the previous project on How to use digital input and digital output of the arduino board.Once the coding is finished one can verify and upload the code to the arduino board as explained in the project how to get started with the arduino.

Circuit Diagram

Video

Code

This Code is only visible to Registered users. Please Login/Register

Components

Resistor Image
Resistor

Resistor is a passive component used to control current in a circuit. Its resistance is given by the ratio of voltage applied across its terminals to the current passing through it. Thus a particular value of resistor, for fixed voltage, limits the current through it. They are omnipresent in electronic circuits.

...
LEDs | Light Emitting Diode
LED
 
Light emitting diodes (LEDs) are semiconductor light sources. The light emitted...
Arduino Pro Mini
Arduino Pro Mini

The Arduino Pro Mini is an ATmega168 based microcontroller board. The board comes with built-in arduino bootloader. It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 8 analog inputs, an on-board resonator, a reset button, and holes for mounting pin headers....

You are here