Arduino Basic Tutorial

Arduino Basic Tutorial: How to use Arduino Serial Plotter for Data Visualization

Description:

The Arduino Serial Plotter is a useful tool that allows developers to visualize data in real-time from an Arduino board. It is a feature available in the Arduino Integrated Development Environment (IDE) that enables developers to plot data from their microcontroller projects in a graphical format. This feature is particularly useful in applications that require data visualization, such as sensor monitoring, data acquisition, and control systems.

The Arduino Serial Plotter works by receiving data through the serial port from the connected Arduino board. The data can be anything that is being read by the Arduino’s analog or digital pins, such as sensor data or other measurements. Once the data is received, it is plotted on a graph in real-time, allowing developers to analyze the data visually and make informed decisions based on the trends they see.

The Arduino Serial Plotter is straightforward to use. To open it, just click on tool in the Arduino IDE and select the “Serial Plotter” option from the menu. From there, they can select the serial port used by the Arduino board. Once these settings are configured, the Arduino Serial Plotter will start plotting the data as it is received.

One of the most significant advantages of using the Arduino Serial Plotter is its simplicity. It requires no additional hardware or software, and developers can easily modify the plotter’s settings to match their project’s requirements. The plotter can also be customized to display multiple data sets simultaneously, making it easier to compare and analyze different data streams.

In addition to its simplicity, the Arduino Serial Plotter is also an excellent tool for debugging and troubleshooting Arduino projects. By visualizing data in real-time, developers can quickly identify any anomalies or issues with their code and take corrective action.




Amazon Links:

Arduino Uno

Arduino Mega

Arduino Nano

Potentiometer

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

How to open Arduino serial plotter window:

To open the Arduino Serial Plotter in the Arduino IDE, follow the steps below:

Step 1: Connect the Arduino Board

Connect the Arduino board to your computer using a USB cable. Ensure that the Arduino board is properly connected and that the appropriate drivers are installed.

Step 2: Open the Arduino IDE

Open the Arduino IDE on your computer. If you do not have the Arduino IDE installed on your computer, you can download it for free from the Arduino website.

open arduino Ide for plotting sensor data in arduino serial plotter

Step 3: Select the Serial Port

In the tool menu, select the correct serial port that your Arduino board is connected to. To do this, select the serial port from the drop-down menu.

select the correct serial port for plotting the data in arduino serial plotter

Step 4: Open the Arduino Serial Plotter

After selecting the correct serial port, click on the “Serial Plotter” located in the tool menu of the Arduino IDE or press the “Ctrl+Shift+L” This will open the Arduino Serial Plotter.

open arduino serial plotter to read the sensor data in real time

Step 5: Start Plotting Data

With the Arduino Serial Plotter configured to your liking, you can start plotting data from your Arduino board. The plotter will automatically start displaying data in real-time as it is received through the serial port.



Single line plotting in Arduino

Single line plotting in Arduino refers to the process of graphing data from a sensor or other source in real-time using the Arduino Serial Plotter tool. This tool allows you to visualize data on a graph that updates continuously as new data is received from the Arduino board.

To perform single line plotting in Arduino, you will typically use the Serial library to communicate data from the board to the computer. Once the data is received by the computer, you can open the Serial Plotter tool in the Arduino IDE and select the appropriate baud rate and COM port to establish a connection.

Next, you will need to send data from the Arduino board to the computer using the Serial.print() or Serial.println() functions. These functions allow you to send numerical data as ASCII characters over the serial connection.

Once the data is received by the computer, it will be displayed on the Serial Plotter as a line graph. You can adjust the settings of the Serial Plotter to change the range and scale of the graph, as well as the color and thickness of the line.



Circuit diagram for single line plotting:

circuit diagram for sending single sensor data to arduino serial plotterExample: how to plot sensor data in single line in Arduino serial plotter:

Here’s an example Arduino code that reads the analog input from a variable resistor (potentiometer) you can use any sensor but in my case, I am using variable resistor (potentiometer) and plots the data in the Serial Plotter

output:

how to plot sensor data in single line in Arduino serial plotter



Code Explanation:

int potPin1 = A0;  // Analog input pin for first potentiometer

This line of code creates an integer variable potPin1 and assigns it the value A0. A0 is a predefined constant in Arduino that corresponds to analog input pin 0 on the board. This means that potPin1 is now set to the analog input pin where the first potentiometer is connected.

void setup() {

  Serial.begin(9600);

}

In the setup() function, we’re initializing the serial communication between the Arduino board and the computer by calling the Serial.begin() function with a baud rate of 9600. This sets up a serial connection with the computer that we can use to send data from the Arduino board to the Serial Monitor and Serial Plotter in the Arduino IDE.

void loop() {

  // Read analog input from first potentiometer

  int potValue1 = analogRead(potPin1);

  // Send data over serial connection

  Serial.print(potValue1);

  // Delay to allow time for serial communication

  delay(100);

}

In the loop() function, we’re reading the analog input from the first potentiometer by calling the analogRead() function with the potPin1 variable as the argument. This function reads the voltage level on the specified analog input pin and converts it to a digital value between 0 and 1023. The result is stored in an integer variable called potValue1.

Next, we’re using the Serial.print() function to send the potValue1 over the serial connection. This function sends a value or string to the serial connection without a newline character at the end. In this case, we’re sending the potValue1 without any additional formatting.

Finally, we’re adding a delay() statement at the end of the loop to give the Serial Monitor and Serial Plotter time to receive and display the data. In this case, we’re using a delay of 100 milliseconds, but you can adjust this value as needed depending on how frequently you want the potentiometer values to be sent over the serial connection.




Multiple line plotting in Arduino

Multiple line plotting in Arduino refers to the ability to plot data from multiple sources on a single graph using the Arduino Serial Plotter tool. This tool allows you to visualize and compare data from different sensors or other sources in real-time, making it useful for monitoring and analyzing complex systems.

When you plot multiple lines in the Serial Plotter, each line represents data from a different source. For example, you might use one line to plot data from a temperature sensor, another line to plot data from a light sensor, and a third line to plot data from a humidity sensor. By plotting all of this data on a single graph, you can easily see how the different variables are related and how they change over time.

To plot multiple lines in the Serial Plotter, you will need to modify your Arduino code to send data from multiple sensors or other sources over the serial connection. You can then configure the Serial Plotter to display the data on a single graph with multiple lines.

Once you have set up your code and Serial Plotter, you can view the multiple line graph in real-time. The Serial Plotter updates the graph as new data is received over the serial connection, allowing you to see changes and trends as they happen.



Circuit diagram for multiple line plotting:

circuit diagram for sending multiple sensor data to arduino serial plotter

Example: how to plot multiple sensor data in multiple line in Arduino serial plotter:

Here’s an example Arduino code that reads analog input from multiple variable resistors (potentiometers) you can use different sensor but in my case I am using variable resistors (potentiometer) and plots the data in the Serial Plotter:

output:

how to plot multiple sensor data in multiple line in Arduino serial plotter



Code Explanation:

int potPin1 = A0;  // Analog input pin for first potentiometer

int potPin2 = A1;  // Analog input pin for second potentiometer

In these lines, we’re defining two integer variables potPin1 and potPin2 and assigning them the values A0 and A1, respectively. These values correspond to the analog input pins on the Arduino board that are connected to the two potentiometers. By defining these variables at the top of our code, we can easily change which pins we’re using without having to search through the entire code to find and replace the pin numbers.

void setup() {

  Serial.begin(9600);

}

In the setup() function, we’re initializing the serial communication between the Arduino board and the computer by calling the Serial.begin() function with a baud rate of 9600. This sets up a serial connection with the computer that we can use to send data from the Arduino board to the Serial Monitor and Serial Plotter in the Arduino IDE.

void loop() {

  // Read analog input from first potentiometer

  int potValue1 = analogRead(potPin1);

  // Read analog input from second potentiometer

  int potValue2 = analogRead(potPin2);

  // Send data over serial connection

  Serial.print(potValue1);

  Serial.print(“,”);

  Serial.println(potValue2);

  // Delay to allow time for serial communication

  delay(100);

}

In the loop() function, we’re reading the analog input from both potentiometers using the analogRead() function. This function reads the voltage level on the specified analog input pin and converts it to a digital value between 0 and 1023. We’re storing the result of each analogRead() function call in a separate integer variable (potValue1 and potValue2), which we can later send over the serial connection.

Next, we’re using the Serial.print() and Serial.println() functions to send the potentiometer values over the serial connection. The Serial.print() function sends a value or string to the serial connection without adding a newline character at the end, while Serial.println() sends a value or string with a newline character at the end. In this case, we’re sending the two potentiometer values separated by a comma (,) using Serial.print() and adding a newline character using Serial.println() to ensure that each set of values is displayed on a new line in the Serial Monitor and Serial Plotter.

Finally, we’re adding a delay() statement at the end of the loop to give the Serial Monitor and Serial Plotter time to receive and display the data. In this case, we’re using a delay of 100 milliseconds, but you can adjust this value as needed depending on how frequently you want the potentiometer values to be sent over the serial connection.



Displaying sinusoidal waveform in Arduino serial plotter:

A sinusoidal waveform is a type of periodic waveform that oscillates between a minimum and maximum value in a smooth and continuous manner. The shape of the waveform is defined by a mathematical function called a sine wave, which has a characteristic “sine” shape.

In the Arduino Serial Plotter, you can generate sinusoidal waveforms by sending a series of data points that follow a sine wave pattern over time. By plotting these data points on the Serial Plotter’s X-Y graph, you can visualize the waveform and observe its frequency, amplitude, and phase.

To generate a sinusoidal waveform in the Arduino Serial Plotter, you can use the sin() function in Arduino to calculate the sine wave values, and then send these values over the serial connection to be plotted on the Serial Plotter.



Example: how to plot sinusoidal waveforms in Arduino serial plotter:

Code explanation:

This Arduino code generates a sinusoidal waveform and sends it over the serial connection to be plotted in the Serial Plotter. Here’s a step-by-step explanation of what the code does:

  • In the setup() function, the code initializes the serial communication with a baud rate of 9600.
  • In the loop() function, the code generates a sinusoidal waveform by using a for loop. The loop variable i is incremented by 5 on each iteration, which means that the loop will generate 72 data points for one complete cycle of the waveform (360 degrees divided by 5 degrees per step).
  • Inside the loop, the code calculates the value of the sine wave at the current time index i using the sin() function. The value is calculated by multiplying i by 2 * PI and dividing the result by the waveform period of 1000 milliseconds.
  • The sine wave value is then sent over the serial connection using the Serial.println() function. This function sends the value as a string of characters, followed by a newline character.
  • After sending the value, the code adds a delay of 100 milliseconds using the delay() function. This delay ensures that the serial data is transmitted at a slower rate, which allows the Serial Plotter to keep up with the incoming data and plot the waveform smoothly.
  • The loop continues to generate and send data points until the program is stopped.

When you upload this code to an Arduino board and open the Serial Plotter in the Arduino IDE, you should see a sinusoidal waveform being plotted on the graph like the below figure. The waveform will have a frequency of 1 Hz (one complete cycle per second) and an amplitude of 1, since the sin() function returns values between -1 and 1. You can adjust the waveform frequency and amplitude by changing the values in the sin() function or the for loop.

display sinusoidal waveform in arduino serial plotter

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button