top of page
  • Writer's picturePuriphico

communication functions in arduino: serial

This article details the ways an Arduino microcontroller can communicate with your computer through the various Serial functions.

  • Serial functions:

  • The various Serial functions are used for communication between the Arduino board and a computer/other devices. All Arduino boards have at least one Serial port, as pictured below:

  • How Serial functions are applied in practice:

  • In order for Serial to work properly, the arduino microcontroller (typically) has to be connected (via a cable connecting the Serial port, known as UART or USART, of the board to the USB port of the computer) to the computer where the code is being processed. 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) are used for Serial communication, as you can see in this image:

  • (Quick warning: if you are using Serial functions in your program, you cannot also use the pins outlined below as digital inputs or outputs.)

  • When you incorporate Serial functions in your code, a few of which I will describe later on, and have the microcontroller hooked up to the computer, you can monitor the serial communication in real time using the Serial monitor/plotter.

The following images depict ways to access the serial monitor/plotter:

  • In my own program, when monitoring handwashing audio frequencies, I used a built-in sensor 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 compare the different patterns using graphical representations. A few examples of resulting graphs are shown below:

As I mentioned briefly, Serial has many functions of its own, and the following are some of the functions that I have used most often and, thus, perceive as most important:

  • if(Serial)

  • Indicates if the Serial port is ready

  • If the Serial port is available, the function returns true.

  • Occasionally replaced with 'while(!Serial)'.

  • serial.available()

  • 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).

  • Often used in the context of If statements (an example can be found 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). (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 screen (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.

  • Its purpose is not concerned with the set-up and preparation of the code, but rather as somewhat of an output in the main loop of the sketch.

  • To use it, write serial.prinln(val, format), where value indicates the value to print (as any data type) and the format specifies the number base, or serial.println("x"), where 'x' (anything enclosed by the quotations) will be printed.

  • Both 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 the microcontroller sends to the computer into readable material - a way of extracting the data, so to speak.

3 of the above four functions are used in the code fragment below:

#SerialFunctions #Serial #Functions #Arduino #Programming #Code #Puriphico

229 views0 comments

Recent Posts

See All
bottom of page