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.