Internet of Things: Arduino Serial Monitor
Introduction
Arduino Serial Monitor
A 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. Even though 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 Arduino Serial Monitor is where you give input signals to the Arduino boards to display the performance.
For now, we will try some basic input and output (I/O) operations as shown in the following code.
- void setup()
- {
- // Baud Rate
- Serial.begin(9600);
- Serial.println("Hello Arduino");
- Serial.println("Hey, C# Corner");
- }
- void loop()
- {}

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).
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, the sketch would be like.
- byte user_input;
- void setup() {
- // Baud Rate
- Serial.begin(9600);
- Serial.println("Hello Arduino");
- Serial.println("Hey, C# Corner");
- }
- void loop() {
- if (Serial.available()) {
- user_input = Serial.read();
- Serial.println(user_input);
- }
- }
And the output would be like this:
Here, we need 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.

- byte user_input;
- void setup() {
- // Baud Rate
- Serial.begin(9600);
- Serial.println("Hello Arduino");
- Serial.println("Hey, C# Corner");
- }
- void loop() {
- if (Serial.available()) {
- user_input = Serial.read();
- Serial.write(user_input);
- }
- }
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:

- void setup() {
- // Baud Rate
- Serial.begin(9600);
- Serial.println("Hello Arduino");
- Serial.println("Hey, C# Corner");
- }
- void loop() {
- if (Serial.available()) {
- char input = Serial.read();
- Serial.println(input);
- }
- }
And, the output is as expected.
In spite of char, you can use int to get an integral value.
That’s all for this chapter. I hope you enjoyed reading!!

Author
Abhishek Kumar Ravi
0
11.5k
4.5m