Introduction to Embedded Systems
Serial Monitor
Introduction to the Serial Monitor
In the previous lesson we wrote our first Arduino language program, and uploaded and ran it on the Arduino. But we could not see anything it was doing. In this lesson, we want to gain the ability for the code to show us some information. In particular, we want to have the means to display the current values of variables in the program, as the code runs. The most basic way to do this is by using the Serial Monitor.
The serial connection via the USB cable is how the Arduino Uno board can communicate with the attached computer. The Serial Monitor connects to this connection and provides a way to show you this communication traffic. It can be activated by pressing the magnifying glass button in the upper right of the Arduino IDE software window. The Serial Monitor then shows up as a separate window in the Arduino IDE application on your computer.
You would also be able to connect to the serial connection with, say, a Python code from a Jupyter notebook. In fact we do this at the very end of this course and also in our course Data Science and Computing with Python for Pilots and Flight Test Engineers.
For most of this course, however, we will be content with just outputting information to the Serial Monitor window of the Arduino IDE or to a physical display attached to the Arduino, such as a 7-segment (TM1637), a 2004 20×4 LCD, or a SSD1306 OLED display, if you happened to buy one.
If you happen to use an Arduino Uno simulator rather than real hardware, the Serial Monitor appears somewhere in the simulation, sometimes only after the Arduino has written to it for the first time (thus do not be confused, if you do not see it anywhere, before you upload and run your code – it will show up, when it needs to).
Example Program with Output to Serial Monitor
Simple Introductory Case
The code below prints the value of the integer variable \(i\) to the serial monitor window every second, and increments it by one each time.
// Declare/define global variables (one integer and one floating point):
int i;
void setup() {
// put your setup code here, to run once (only at the beginning):
// Initialize serial monitor (choose default baud rate 9600):
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// Print variables to the serial monitor:
Serial.print("Integer: ");
Serial.println(i);
// Wait 1000 milliseconds:
delay(1000);
// Increase value of variable i by 1 during each pass of the loop:
i = i + 1;
}
As you see from the code above, first we need to initialize the Serial Monitor, using the Serial.begin() method of the Serial class. And then we can write to it with the Serial.print() and Serial.println() methods (the latter ends with a new line, while the former does not). The links in this paragraph lead to further descriptions of these functions on the Arduino website.
In this manner, we can write text strings (in quotation marks) and the values of variables to the Serial Monitor, which the user sees in a window on their main computer.
Additional Output
We can enhance the above simply program a bit to demonstrate a few more useful printing features of the Serial Monitor feature of the Arduino.
For floating-point numbers, the default output is two digits after the decimal point. But this can be controlled, by turning the variable into a string first, using the Arduino language specific String() type (really a class, not a primitive data type). The second argument indicates the number of digits retained after the decimal point, when turning the variable into a string of characters, which are then simply printed.
In addition, the code below also shows you how one can conveniently display numbers not only in the decimal numeral system (base-10 positional numeral system), but also hexadecimal (base 16) and binary (base 2).
// Declare/define global variables (one integer and one floating point):
int i;
double pi = 3.14159265359;
void setup() {
// put your setup code here, to run once (only at the beginning):
// Initialize serial monitor (choose default baud rate 9600):
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// Print variables to the serial monitor:
Serial.print("Integer: ");
Serial.println(i);
// Additional things you can output:
Serial.print("Integer (hexadecimal): ");
Serial.println(i, HEX);
Serial.print("Integer (binary): ");
Serial.println(i, BIN);
Serial.print("Floating Point (default display): ");
Serial.println(pi);
Serial.print("Floating Point (show 6 digits after period): ");
Serial.println(String(pi,6));
Serial.println("----------");
// Wait 1000 milliseconds:
delay(1000);
// Increase value of variable i by 1 during each pass of the loop:
i = i + 1;
}
This is how we can have the Arduino Uno show us text and the values of integers and floating-point numbers on our computer screen, while it runs the Arduino language program. We will use this often.