Skill Level: Intermediate
The Arduino IDE is the basis for almost all Arduino based programming and board management. The built-in Serial Monitor allows us to view serial output from the attached board.
With the advent of the arduino-cli command line utility, people can now use their own trusted source code editors for developing their sketches and the arduino-cli utility for compiling and uploading those sketches to their boards. One drawback to this approach, however, is the inability to view a sketch’s serial output while the sketch is running. The screen command line utility comes to the rescue. This utility is available on most macOS and Linux based systems.
Note, if you are not yet familiar with the arduino-cli utility and want to give it a try, please see the Using The Arduino Command Line tutorial and the Arduino Command Line Cheatsheet for assistance.
Before using the screen utility, we first need to retrieve some information. The first is the serial port to which your Arduino board is attached. This is accomplished by running the following command to list your system’s available serial ports both before and after plugging in your Arduino board and seeing which one was added.
$ ls /dev/tty.*
The /dev/tty.usbmodem14202 port appeared on my Mac with an Arduino Uno WiFi Rev2 attached.
The second piece of information we need is the baud rate (number of bits per second) of the serial connection to the board. This is specified within our sketch with the
Serial.begin(9600); statement when printing serial output is utilized. The
9600 argument, in this case, is the baud rate. You may use other baud rates, e.g. 115200, but just make sure you know which rate is actually being used or you will see strange characters in the serial output when they don’t match.
We now have all of the information we need and are ready to see the screen utility in action. Compile and upload a sketch to your board using the arduino-cli utility. This should probably go without saying, but make sure your sketch actually does print something using the
Open a terminal window and attach the screen utility to your board’s serial output with the following command by replacing the <port> and <baud> fields with the actual information retrieved previously for your setup.
$ screen <port> <baud>
In my case, this becomes
$ screen /dev/tty.usbmodem14202 9600
Once connected, you should see your sketch’s serial output being printed to the screen. Not bad, eh?
To disconnect the screen utility, press CTRL-a followed by CTRL-\ and then press y at the prompt asking if you really want to quit.
The screen command line utility can be quite handy for viewing serial output when we want to use our own code editors. With it, I no longer need to copy code into the Arduino IDE just to use the Serial Monitor.
By the way, if you are on a Windows system, I understand a terminal program, such as Putty, should provide similar functionality to the screen utility.