Guide to Communicating Between Your Arduino and Computer: Serial Communication.

Adrian Lazzi
6 min readSep 30, 2021

--

This article details how you can program your Arduino to communicate with your computer using Serial functionality — an example from my startup puriphico.com is also included! If you find Arduino documentation on ‘Serial’ to be either unclear or vague, and you are not sure how to implement it in your programs, you have come to the right place.

The Groundwork

  • The various Serial functions are used in your program to provide the groundwork for communication between the Arduino board and a computer/other device. But in order to use them in your program, you should first make sure that everything is connected properly:
  • All Arduino boards have at least one Serial port (as pictured below), which happens to be the same port to which you would connect a computer for uploading your Arduino programs anyway (no more things to connect!). This means that, as long as your Arduino is connected to your computer with a USB cable (potentially bluetooth), you can reap the benefits of Serial functionality! Here’s how, in formal steps:
  • In order for Serial to work properly, the Arduino microcontroller (typically) has to be connected to the computer where the code is being processed (specifically, a cable should the Serial port, known as UART or USART, of the board to the USB port of the computer). If you have an Arduino, the cable usually comes standard with the board. On most Arduino boards, pins 0 and 1 (referred to as TX and RX) can also be used for Serial communication, as you can see in the below image. Warning: if you are implementing Serial communication in your programs, I recommend avoiding the usage of pins 0 and 1 to connect loads/other components of your circuit.
  • When you incorporate Serial functions in your code, a few of which I will describe in a moment, and have the microcontroller hooked up to the computer, you can monitor the serial communication in real time using the Serial monitor/plotter. If you are still not seeing the big picture, that is understandable — once I began incorporating Serial communication in my programs, the understanding came naturally, hence why we will now delve into the programming, before putting the two together:

The Code

  • if(Serial)
  • Indicates if the Serial port is ready. If the Serial port is available, the function returns true. What does this mean? It is essentially a check to see that everything on the Arduino side of things is working properly. Nothing to worry about.
  • This is often used in the void setup() of an Arduino program.
  • Occasionally replaced with ‘while(!Serial)’.
  • serial.available()
  • This function gets the number of bytes (characters) available for reading from the Serial port. This is data that has already been collected and stored in the Serial receive buffer (which holds 64 bytes). It does not necessarily need to be included in your programs, but you may come across it occasionally.
  • Often used in the context of If statements, as shown in the example below:
  • Serial.begin()
  • Sets the data rate in bits per second (aka the baud rate) for Serial data transmission (transmission of data between the board and the computer). Basically, it provides an opportunity for you to declare the rate at which data will be transferred (if you want to collect a lot of data, you will set a high baud rate).(Note: you do not need to understand this concept entirely to use it in practice, so do not get discouraged if you do not know what a bit is, for example.)
  • For communicating with Serial monitor, you need to make sure to use one of the baud rates listed in the menu at the bottom right corner of the serial monitor, which we will detail shortly (in the below example, I used 115200; however, many beginner programs use 9600).
  • Syntax for using serial.begin is the following: Serial.begin(speed)
  • Serial.println()
  • This function tells the computer what should be printed in the Serial monitor. Println stands for ‘print line’ — to print multiple things on the same line (ie not creating a new line with each print statement) write Serial.print().
  • It is mostly used in the void loop() of an Arduino program, and, if you have worked with other programming languages, if effectively a print statement (it displays a variable, text, or message in the Serial monitor — hang tight, I will get to it). Occasionally, it is used in the void setup(), but usually only to alert the programmer if there is an error before the void loop() runs, as shown in the below image:
  • Keep in mind that, before you write this in the void setup() of your code, you must first initialize Serial communication, as shown in the highlighted portion of the above image.
  • Why would you want to use this? The computer and microcontroller are capable of storing significant amounts of data when set-up properly, and the serial.println() function is a way of converting the data transferred into readable material — a way of extracting the data, so to speak. This is extraordinarily helpful when analyzing data sets or trying to debug an aspect of your code/evaluate whether something in your circuit is working properly (for example, to analyze audio frequencies of handwashing against those of running water for the calibration mechanism in the most recent puriphico.com prototype, I wrote Serial.println() statements to display the FFT values associated with handwashing.)
  • Below is another example of Serial functions in the context of an Arduino program:

Accessing the Serial Monitor/Plotter

  • Finally! To access the Serial monitor/plotter (the place where you can see print statements/information sent from the microcontroller to the computer), follow the instructions of the below images:
click the magnifying glass in the top right corner of the screen
click the tools tab at the top of your computer screen

Example

  • In my own programs, when monitoring handwashing audio frequencies, I used a built-in sensor on the Arduino Nano 33 BLE Sense to read handwashing frequency patterns; then, in my program, I analyzed these recorded frequency patterns using FFT software. Finally, I was able to print the resulting values on the Serial monitor and paste them into an excel spreadsheet for closer analysis. I performed this same procedure for various other frequency patterns (ie running water without handwashing, generic background noise, etc) and compared the different patterns using graphical representations. A few examples of resulting graphs are shown below:

That’s all there is to it! I hope you can use this article as a reference point when building your incredible Arduino programs.

If you found it helpful, help this article on its way to 50 claps and subscribe to get notified whenever I post a new article! (For video content, check out my YouTube channel https://www.youtube.com/channel/UC25QUdv5BI4KMx598UR0IWA or search for it @puriphico).

My startup puriphico.com is constantly developing improved handwash-monitoring technology for integration in hospital environments— don’t forget to check out puriphico.com to see the latest updates!

--

--

Adrian Lazzi
Adrian Lazzi

Written by Adrian Lazzi

I’m a high-school student in Los Angeles, California who blogs about my engineering project, puriphico.com! Follow for tech tutorials and updates!

No responses yet