Thursday, April 7, 2022

Arduino IDE Serial Plotter Syntax and Tips

 Introduction

The Serial Plotter allows you to generate line graphs in realtime as your Arduino program runs. The tool appears under Tools, or Ctrl-Shift-L. You cannot run the Serial Console and the Plotter at the same time, and you need to have the correct serial port selected.

Basic Operation

 In it's simplest form, you could just print data values while in loop() terminal. Separate multiple values with a comma, space, or tab (\t) character. Terminate the line with a newline sequence (\n) or endl if using Streaming. You'll need to maintain the order of the data elements being printed.

Additionally you can print a legend header with the names and colors of the graph lines.

Active Legend / Header

My preferred way to graph is with a header displaying names, colors, and values. In demo 3 of the example, I display the actual values in the legend, then offset the graphs vertically. I added center lines, t, b, and c. The lowest graph is constrained because sometime you have extreme values that would mess up your display because of auto-ranging. Sometime just knowing that a value is "off the charts" is good enough - you can refer to the legend for the actual value.

In short, print "label=<hdr_value>:graph_value\n". label=<hdr_value> will just be a dynamic string in the header, graph_value will be used for plotting. Separate these sequences with a comma, space, or tab, with a single "\n" line termination. In some cases a simple label could be printed once. Printing the labels in a loop allows you to restart the Serial Plotter window and keep your legend.

Sample Plot


References:

See https://dreamonward.com/2020/07/25/arduino-serial-plotter-labels/ and the referenced documentation at https://github.com/arduino/Arduino/blob/ba34eb640e5f6c2f8618debf61022f731c0eab74/build/shared/ArduinoSerialPlotterProtocol.md

The Code, The Code!

Here's some code you can play with:

#include <Streaming.h>

int demo = 3;

void setup() {
  Serial.begin(9600);   // USB "baud" rate doesn't typically matter
  delay(500);           // wait for the Serial port to start up
  switch (demo) {
  case 1:
      // no legend, single value
    break;
  case 2:
    // no legend, multiple values
    break;
  case 3:
    // legend printed in loop with values
    break;
  default:
    break;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  switch (demo) {
  case 1: demo_1();
    break;
  case 2: demo_2();
    break;
  case 3: demo_3();
    break;
  default:
    break;
  }
  delay(50);
}

int x;
int y=90;

double sine_x, sine_y;

void update_values() {
  sine_x = 1 * sin(float(x)*PI/180);
  sine_y = 1 * sin(float(y)*PI/180);
  x += 2;
  y += 2;
  if (x>=360) x%=360;
  if (y>=360) y%=360;
}

void demo_1() {
  update_values();
  Serial << sine_x << endl;
}

void demo_2() {
  update_values();
  Serial << sine_x << "," << sine_y << endl;
}

void demo_3() {
  update_values();
  Serial <<  "sine(x)=" << sine_x << ":" << sine_x + 1.5
         << " sine(y)=" << sine_y << ":" << sine_y - 1.5
         << " sine(x)=" << sine_x << ":" << constrain(sine_x, -.75, 0.75) - 4.5
         << " t:" <<  1.5
         << " b:" << -1.5
         << " c:" << -4.5
         << endl;
}

No comments:

Post a Comment

Micca OriGen G2 DAC Problems

The Micca OriGen G2 is a premium DAC, preamp, and headphone amp primarily intended to be connected to a PC. The Problem & Troubleshootin...