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;
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 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.
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
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
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.
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.
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.
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.
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;
// 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-Rx Demo");
// if we get a valid byte, read analog ins:
// get incoming byte:
inByte = Serial.read();
// send the same character back to serial port
// blink the LED once //
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.