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 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.
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;
Fig. 2: Typical Arduino Pro-Mini Board
Fig. 3: Arduino IDE Software Window
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.
Fig. 4: External USB to TTL converter board for programming Arduino and serial communication
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 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.
Project Source Code
### /*================================= EG LABS ======================================= Send a string contnuopusly to the serial port along with Turning on and off a LED connected to digital pin 5 The circuit: * LED attached from pin 5 to ground through a 1K resistor //================================= EG LABS =======================================*/ // give the pin a name: 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); } ###
Circuit Diagrams
Project Components
Project Video
Filed Under: Arduino Projects
Filed Under: Arduino Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.