How to show graph with PID type on Arduino?

Hello Everyone,

I'm Saran, This code below is working, I can see respond on serial monitor but I cannot show on graph.

Could you please suggest me how to show graph on Arduino with PID type follow code below,

#include <PID_v1.h>
const int photores = A0;          // Photo resistor input
const int pot = A1;               // Potentiometer input
const int led = 13;                // LED output
double lightLevel;                // variable that stores the incoming light

// Tuing parameters  
float Kp=0;                       // Initial Proportional Gain
float Ki=10;                      // Initial Intergral Gain
float Kd=0;                       // Intial Differential Gain

double Setpoint, Input, Output;  // Thes are just variables for storingvalues
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);  
                               // This sets up our PDID Loop
                               // Input is our PV
                               // Output is our u(t)
                               // Setpoint is our SP
const int sampleRate = 1;       // Variable that determines how fast our PID loop runs

// Commuication setup
const long serialPing = 500;    // This determines how often we ping our loop
// Serial pingback interval in milliseconds

unsigned long now = 0;

// placehodler for current timestamp
unsigned long lastMessage = 0; // This keeps track of when our loop last spoke to serial
// last message timestamp

void setup()
{
 lightLevel = analogRead(photores);                 // Read in light level
 Input = map(lightLevel, 0, 1024, 0, 255);          // Change read scale to analog out scale
 Setpoint = map(analogRead(pot), 0, 1024, 0, 255);  // get out setpoint from session
 Serial.begin(9600);                                // Start a serial session
 myPID.SetMode(AUTOMATIC);                          // Turn on the PID loop
 myPID.SetSampleTime(sampleRate);                   // Sets the sample rate
 
 Serial.println("Begin");                            // Hello World!
 lastMessage = millis();                            // Timestamp
}

void loop()
{
 Setpoint = map(analogRead(pot), 0, 1024, 0, 255);  // Read our setpoint
 lightLevel = analogRead(photores);                 // Get the light level
 Input = map(lightLevel, 0, 900, 0, 255);           // Map it to the right scale
 myPID.Compute();                                   // Run the PID loop
 analogWrite(led, Output);                          // Write out the output from the PID loop to our LED piin

 now = millis();                                    // Keep track of time
 if(now - lastMessage > serialPing)                 // If it has been long enough give us some info on serial
 {
   // this should execute less frequently
   // send a message back to the mother ship
  Serial.print("Setpoint =");
  Serial.print(Setpoint);
  Serial.print(" Input = ");
  Serial.print(Input);
  Serial.print(" Output = ");
  Serial.print(Output);
  Serial.print("\n");
  
  if (Serial.available() > 0)                       // If we sent the program a command deal with it
  {
    for (int X = 0; X < 4; X++)
    {
      switch (X)
      {
        case 0:
          Kp = Serial.parseFloat();
          break;
        case 1:
          Ki = Serial.parseFloat();
        case 2:
          Kd = Serial.parseFloat();
        case 3:
          for (int y = Serial.available(); y == 0; y--)
          {
            Serial.read();                                // Clear out any residual junk
          }
          break;
      }
    }    
    Serial.print(" kp,Ki,Kd = ");
    Serial.print(Kp);
    Serial.print(",");
    Serial.print(Ki);
    Serial.print(",");
    Serial.print(Kd);                                    // Let us know what we just recived
    myPID.SetTunings(Kp, Ki, Kd);                         // Set the PID gain constants and start running
  }
  
  lastMessage = now;                                     // update the time stamp.
 }
}

Thank you very much.

Welcome to the forum Saran...

It all depends on what you want to do with the data afterwards really!

As far as I know, there are few options, but vary in complexity and hardware need. I shall list in the order of simplicity (for me at least). I am certain there are other means

  1. The newer versions of the Arduino IDE, includes a serial plotter. essentially you output your variables to the serial monitor Serial.println(variable), and then open the plotter to view it.

There plenty of tutorials online and I don't want this to be a long post. here's a quick links:

Video:- YouTube

If you prefer to read, rather than watch a video

  1. Arduino Tutorials includes a method for plotting results using Processing:
    https://www.arduino.cc/en/Tutorial/Graph

or use Python for interface the data

  1. You can also have a microSD Card interface and store the data to the SD Card and later connect it to PC and use Excel or something similar to edit/manipulate/graph the data.

  2. There are other ways, as suggested (port the data into Matlab or Excel using a serial port) for example but I am not certain of the extent you want to go.

As an added tip and for those that may be interested in this post & PID control. It's a little unfair to use the PID library without mentioning Brett for excellent work and tutorials on

http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/

There are some simulators built:

a. based on Brett's code, thanks to Raul for porting the code into an online simulator

b. One I use a lot Online PID Simulator & Fuzzy Logic Simulator (P, I, PI, PID) | Bulanık Mantık Simulatörü

Good luck and share your knowledge and results

0xSAM

Thank you very much 0xSAM,
I still trying follow your suggestions and will respond to you.

No problems, I only gave you more things to read and research... good luck

           for (int y = Serial.available(); y == 0; y--)
           {
             Serial.read();                                // Clear out any residual junk
           }

That's not right. It should be "y != 0"

Pete

P.S. Read How to post code properly.

Pete

Thank you very much Pate, Can I convert this serial monitor results to graph?

Thanks for the code tags :slight_smile:

I haven't used the serial plotter. If you explain what your output looks like, perhaps someone who has used it can help.

Pete

I need to show the results like picture below,