How to Send Serial Data from Arduino

Summary

Developed By: 
Ajish Alfred

The serial communication is the most important communication port which all microcontrollers should have. This enables the user to communicate with the microcontroller using text commands. The microcontroller can also send back data in text format or simply as data bytes. Thus the serial communication provides a text user interface (TUI) for the microcontroller. Apart from simply communicating with the user the serial communication is a very useful debugging tool in the code development process. It can send back the run time status of the internal registers, value of the variables etc. When a given with a microcontroller a developer will first try to make the serial port up and working. The initialization process of the serial port is not such an easy task as far as the common microcontrollers are concerned. One have to go through the data sheet, do some calculation for the precise baud rate which may vary with the crystal frequency etc.

The arduino has built-in functions for accessing the serial port and hence there is no need to go to the register details. One can simply call the functions and get the job done. It makes the coding process fast and the debugging far more efficient. The arduino IDE itself has a serial monitor tool which can display all the data going from and coming into the serial port. This project demonstrates a simple program which can be used to send a string to the serial port of the PC. The data can be displayed in the hyperterminal of the PC or in the arduino’s serial monitor itself.

How To Send Serial Data From Arduino

Description

The arduino board is built around an AVR microcontroller providing all the necessary circuitry for the microcontroller to operate. 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 Getting started with Arduino and tried out all the things discussed there.There are some built-in functions in the arduino IDE which helps in the serial communication process. There is a function which helps to initialize the serial communication port with a particular baud rate and there are functions to send data to the serial port. The functions used in this projects are namely Serial.begin(), Serial.print() and Serial.println() whose details are discussed in the following section.

Serial.begin()  

This function helps to initialize the serial port of the arduino with the baud rate mentioned in its argument. For example to initialize the serial port with a baud rate 9600, the Serial.begin() function can be used as shown below;

Serial.begin(9600);

The above statement will initialize the serial port with the baud rate 9600 which is given as the argument of the function and also with 8 bits of data frame, one start bit, one stop bit and no parity bits.

Serial.print()

The Serial.print() function helps in sending serial data to the devices connected at the serial port of the arduino board. This function can send both ASCII characters and strings to the serial port. For example the Serial.print() function can be used to send a character as shown in the following;

Serial.print(‘A’);

The above statement is able to send the character ‘A’ to the serial port. The function can operate with all ASCII characters; For example the new line and carriage return characters can be send using this function as given in the following statements.

Serial.print(‘\n’);

Serial.print(‘\r’);

The same function Serial.print() can be used to send a string to the serial port as shown in the following statement.

Serial.print(“hello”);

If two strings are printed one after the other using the Serial.print() function the output will appears as shown below;

Serial.println(“hello”);

Serial.println(“world”);

Output:

helloworld

Serial.println()

The Serial.println() functions operation is almost similar to the previously discussed Serial.print() function. The only difference is that the function Serial.println() prints a character or string each time into a new line where as the Serial.print() function cannot do this. One have to use Serial.print(‘\n’);

Serial.print(‘\r’); statements together to get the next character or string to get printed on the new line.

For example if two strings are printed one after the other using the Serial.println() function the output will appears as shown below;

Serial.println(“hello”);

Serial.println(“world”);

Output:

hello

world

THE CODE

In the following code the function call ‘Serial.begin(9600)’ will initialize the serial port with a baud rate of 9600. The function Serial.print() is used to send both the characters and string to the serial port. The function which is called inside the infinite loop as ‘Serial.println("hello world")’ will send the string “hello world” continuously to the serial port each time with a new line and carriage return characters before the string. The code written for this project is given below;

int led = 5;

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 Demo");

  Serial.print('\n');

  Serial.print('\r');

  Serial.print('\n');

}

void loop()

{

  // send this string continuously //

    Serial.println("hello world");

    // blink the LED once //   

    digitalWrite(led, HIGH);      

    delay(1000);                 

    digitalWrite(led, LOW);      

    delay(1000);               

}

The details of the function setup(), loop() are already explained in the project Get started with arduinoThe code also blinks an LED which is connected to the pin number 5 of the arduino board 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....

Browse 4m+ Electronic Products


 
Pick the right product for your next design project with "Mouser Store". Mouser Electronics brings broadest range of semiconductors and electronic components from over 500 industry leading suppliers. 
 
Mouser specializes in the rapid introduction of new products and technologies for design engineers and buyers that includes semiconductors, interconnects, passives, and electro-mechanical components. Providing the best possible service with the flexibility of not requiring a minimum order and same-day shipping, Mouser has it all.
 
 
 
 
When you need the right part right now, think of Mouser
 
Thank you
Team EG