Arduino: How to do Serial Communication

Introduction

The Arduino IDE is an alternative to write code for the Arduino board and you are supposed to understand all the features (at least the essentials) of your IDE. And, Serial Monitor is one of the essential features that every Arduino-sketcher should be aware of. If you are using Arduino in Visual Studio, then the Console Window will do the job of a Serial Monitor.

What the Serial Monitor is exactly

The Serial Monitor is a monitoring tool for Serial Communication. As you know, every microcontroller uses a serial port to communicate with the connected computer or other communication modules. Eventhough we don't have a serial port in our Arduino board, we use the Universal Serial Bus (USB) for the communication. And, Arduino treats the USB as a serial port.

The Wikipedia says:

Serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus. This is in contrast to parallel communication, where several bits are sent as a whole, on a link with several parallel channels.

Background

Serial Monitor is no more than a debugging tool. It lets programmers execute their code in a programmed way where they are aware of "what is happening". You can determine the flow of code and it also allows you to user input via Serial Monitor.

Before going to sketch, we should keep in mind that it's a debugging tool and here we will not use any hardware PIN to implement it. Simply, we just need an Arduino board (in my case, I have Arduino UNO).

For now we will try some basic input and output (I/O) operations as shown in the following code.
  1. void setup()   
  2. {  
  3.     // Baud Rate  
  4.     Serial.begin(9600);  
  5.     Serial.println("Hello Arduino");  
  6.     Serial.println("Hey, C# Corner");  
  7. }  
  8. void loop()  
  9. {}  
And you'll get its output through Serial Monitor (Ctrl+Shift+M) or Tools>Serial Monitor and it would be like the following.



Explanation

So, this seems to be something new. We haven't written anything like in our previous article. Because we didn't get a chance to do so.

The first thing you should do is to define the Baud Rate of the Serial Communication. So, what is Baud Rate? It's a data rate (or, communication rate) in bits per second. Here, we are declaring 9600 and implicitly it means 9600 bits per second.

It's a default rate for Serial Communication, else, you can choose from 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200.
 
Next, we have a println() function that prints the string in the Serial Monitor. And, on every execution it shifts to a new line (in other words every statement is appended with \n).

Up to this, we have printed what we were instructed. We will now ask the users what to print it out.

There is also a function called print(), but it differs with println(). The basic difference is that it doesn't switch to the next line after every execution. Whereas, println() does after every execution. That's what an extra ln (in other words line) can do. So, sketch would be like.
  1. byte user_input;  
  2. void setup() {  
  3.     // Baud Rate  
  4.     Serial.begin(9600);  
  5.     Serial.println("Hello Arduino");  
  6.     Serial.println("Hey, C# Corner");  
  7. }  
  8. void loop() {  
  9.     if (Serial.available()) {  
  10.         user_input = Serial.read();  
  11.         Serial.println(user_input);  
  12.     }  
  13. }

And the output would be like this:



Here, we ed the input as “Ravi” and the Serial Monitor accepts my string and parses (converts) it into a byte data type since we have declared user_input as a byte type. And, it converts "R","a","v","i" to the respective ASCII values since the byte data type stores only ASCII values.

Note: "R" is equivalent to 82, "a" equivalent to 97, "v" equivalent to 118 and "i" equivalent to 105.

What if I want to print in character mode instead of ASCII codes? So, for this we have a function called write(), that accepts byte values and returns characters.

  1. byte user_input;  
  2. void setup() {  
  3.     // Baud Rate  
  4.     Serial.begin(9600);  
  5.     Serial.println("Hello Arduino");  
  6.     Serial.println("Hey, C# Corner");  
  7. }  
  8. void loop() {  
  9.     if (Serial.available()) {  
  10.         user_input = Serial.read();  
  11.         Serial.write(user_input);  
  12.     }  
  13. }  

The rest of the code will the remain same except the Serial.write(user_input) function. Now, it will show it differently as in the following:



Note: In the preceding code, we have used available() to check whether the Serial Port is ready or not for accepting user input.

So, next we will try to parse Serial.read() to char type data.

The Sketch would be like:

  1. void setup() {  
  2.     // Baud Rate  
  3.     Serial.begin(9600);  
  4.     Serial.println("Hello Arduino");  
  5.     Serial.println("Hey, C# Corner");  
  6. }  
  7. void loop() {  
  8.     if (Serial.available()) {  
  9.         char input = Serial.read();  
  10.         Serial.println(input);  
  11.     }  
  12. }

And, the output is as expected.



In spite of char, you can use int to get an integral value.

Conclusion

It's one of the most-used features of Arduino coding. And, we will use this often in our future articles. Next, we will try Temperature Module and display it in Serial Monitor.

Up Next
    Ebook Download
    View all
    Learn
    View all