How to Send Serial Data from Arduino
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.
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;
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.
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.
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;
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.
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;
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.
The same function Serial.print() can be used to send a string to the serial port as shown in the following statement.
If two strings are printed one after the other using the Serial.print() function the output will appears as shown below;
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;
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;
// initialize the led pin as an output.
// start serial port at 9600 bps
// wait for a while till the serial port is ready
// send the initial data once //
Serial.print(" EG LABS ");
Serial.print("Serial Tx Demo");
// send this string continuously //
// blink the LED once //
The details of the function setup(), loop() are already explained in the project Get started with arduino. The 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.
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....
Light emitting diodes (LEDs) are semiconductor light sources. The light emitted...
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....