In addition to sending human readable text messages over your Arduino’s serial communications you can also send and plot numeric data. In this very short tutorial we’ll learn to leverage this and use our Arduino to plot sin(x)
.
We can open the serial plotter via the Arduino context menu under Tools -> Serial Plotter
. The serial plotter expects numeric values to be printed to the serial monitor seperated by new lines. For example, if we wrote a program to print
1
2
3
4
...
N
to the serial monitor the serial plotter would plot y = x + 1
on the screen.
For our example let’s plot sin(x)
. The code to do this is quite straightforward so I’ll just reproduce it below:
float x = 0.0;
void setup() {
Serial.begin(9600);
}
void loop() {
float y = sin(x);
x += 0.05;
Serial.print(y);
Serial.print("\n");
}
If you upload this to your Arduino and open the serial plotter you’ll see sin(x)
being plotted on the screen.
loop()
function being run?