Do you want to visualize data from your Arduino project in real-time? This tutorial will walk you through an Arduino program that uses MegunoLink’s Time Plot visualizer to plot sine and cosine waveforms on two different y-axes. This is a great way to visualize multiple datasets with different scales simultaneously. Let’s get started!
What You’ll Learn
- How to plot data using MegunoLink’s Time Plot visualizer
- How to use dual y-axes for plotting
- How to customize plot properties like titles, labels, and ranges
What is MegunoLink?
MegunoLink is a powerful tool for creating custom interfaces, dashboards, and visualizations for your Arduino projects. Its Time Plot visualizer is perfect for real-time data plotting.
Program Overview
This program generates sine and cosine waveforms and plots them on a Time Plot in MegunoLink. The sine wave is plotted on the left y-axis, and the cosine wave is plotted on the right y-axis. The plot is updated every 100 milliseconds, and the plot properties (like titles and ranges) are updated every 10 seconds.
Code Walkthrough
Let’s break down the program step by step.
1. Library Includes
1 2 |
#include "MegunoLink.h" #include "ArduinoTimer.h" |
- MegunoLink.h: Provides tools for creating plots and interfaces.
- ArduinoTimer.h: Helps manage timing for sending data and updating plot properties.
2. Global Variables
1 2 3 |
TimePlot MyPlot; ArduinoTimer PlotPropertiesTimer; ArduinoTimer PlotSendTimer; |
- MyPlot: Represents the Time Plot in MegunoLink.
- PlotPropertiesTimer: Tracks when to update plot properties.
- PlotSendTimer: Tracks when to send new data to the plot.
3. Setup Function
1 2 3 4 |
void setup() { Serial.begin(115200); SendPlotProperties(); } |
- Initializes serial communication at 115200 baud.
- Calls
SendPlotProperties()
to configure the plot’s appearance.
4. Main Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void loop() { double dY, dY2; float seconds; float frequency = 0.5; //Hz float phase = 3.141 / 2; if (PlotSendTimer.TimePassed_Milliseconds(100)) { seconds = (float)millis() / 1000; dY = sin(2 * 3.141 * frequency * seconds); dY2 = cos(2 * 3.141 * frequency * seconds + phase); MyPlot.SendData(F("Sinewave"), dY, Plot::Blue, Plot::Solid, 2, Plot::Square, Plot::LeftAxis); MyPlot.SendData(F("Cosinewave"), dY2, Plot::Red, Plot::Solid, 2, Plot::Square, Plot::RightAxis); } if (PlotPropertiesTimer.TimePassed_Milliseconds(10000)) { SendPlotProperties(); } } |
- Lines 4-6: Define variables for the sine and cosine calculations.
- Lines 8-14: Calculate sine and cosine values and send them to MegunoLink every 100 milliseconds.
- Lines 16-18: Update plot properties every 10 seconds.
5. Plot Properties Function
1 2 3 4 5 6 7 8 9 |
void SendPlotProperties() { MyPlot.SetTitle("Sine and Cosine Function Waveforms"); MyPlot.SetXLabel("Time"); MyPlot.SetYLabel("Amplitude"); MyPlot.SetY2Label("Amplitude 2"); MyPlot.SetYRange(-1.5, 5); MyPlot.SetY2Range(-5, 1.5); MyPlot.SetY2Visible(); } |
- Sets the plot title, axis labels, and ranges for both y-axes.
- Makes the right y-axis visible.
How to Use This Code
1. Install Requirements
- Download MegunoLink.
- Install the MegunoLink Arduino library via the Arduino Library Manager.
2. Upload the Code
- Connect your Arduino to your computer.
- Upload the program using the Arduino IDE.
3. Set Up MegunoLink
- Open MegunoLink.
- Download the pre-made interface and open it in MegunoLink.
- Ensure the serial port is set to the correct baud rate (115200).
4. Watch It Work!
- Open the Time Plot visualizer in MegunoLink.
- Observe the sine and cosine waveforms being plotted in real-time.
- Notice how the sine wave is plotted on the left y-axis and the cosine wave on the right y-axis.
Customization Ideas
1. Change Waveform Frequency
Adjust the frequency of the sine and cosine waves:
1 |
float frequency = 1.0; // Change to 1 Hz |
2. Add More Data Series
Add another waveform to the plot:
1 2 |
double dY3 = tan(2 * 3.141 * frequency * seconds); MyPlot.SendData(F("Tangentwave"), dY3, Plot::Green, Plot::Solid, 2, Plot::Circle, Plot::LeftAxis); |
3. Customize Plot Appearance
Change the plot title, axis labels, or colors:
1 2 3 |
MyPlot.SetTitle("My Custom Waveform Plot"); MyPlot.SetYLabel("Voltage"); MyPlot.SetY2Label("Current"); |
Troubleshooting Tips
- No Data in Plot? Ensure the baud rate in MegunoLink matches the program (115200).
- Plot Not Updating? Check that the Arduino is connected and the correct serial port is selected in MegunoLink.
- Library Errors? Reinstall the MegunoLink Arduino library.
Next Steps
- Explore MegunoLink documentation to learn more about plotting and visualization.
- Add sensors to your Arduino and plot real-world data (e.g., temperature, light, or motion).
- Combine this plot with other MegunoLink features like interface panels or data logging.
By understanding this dual-axis plotting example, you’re ready to create professional-quality visualizations for your Arduino projects. What will you plot next?
Download MegunoLink and start visualizing your data today!