0
\$\begingroup\$

I am designing a PID temperature controler; as far as I managed to write it so far, you find the code below.

Can you please help me check if Integrator wind up and other PID elements are configured please?

When i run the code at some point close to the set point , it starts behaving like an ON and OFF code

I don't know if this is a valid question but im asking it anyways .

and if you can make any edits . i will be glad to check them out

// programing the pid
const int pin = 3;  // PWM analog output 
double cumerror = 0; // Initialising the cumulator of the integrator to 0

double input, setpoint, integral, proportional, piderror, pidvalue;

// Input values
double kp = 25; // seting proportional at 25
double ki = 0;  // seting the integral at 0

void setup() {
  setpoint = 37; // seting the setpoint at 37
  Serial.begin(9600);
}

void loop() {
  double inputvalue = map(analogRead(A0), 0, 1023, 0,
                          100); // reading the values from the analog signals
                                // and mapping it to a 100 deg scale

  piderror = setpoint - inputvalue; // calculating the error

  // calculating the integral ((Naturally ki= cumerror *dt), we do not have to
  // multiply by dt, since dt = 0 because i am using the sample time delay(1000)
  // every time )
  cumerror += piderror;
  integral = ki * cumerror;

  // calculating the proportional error

  proportional = kp * piderror; // calculating the proportional error

  // changing the P and I parameters as the error approaches the set point
  if (3 < piderror < 10) {

    kp = 10;
    ki = 0.5;
    cumerror += (1 * piderror);
    integral = ki * cumerror;
  }
  // Making the parameters =0 as the error approaches more 0

  if (piderror < 3) {
    proportional = 0;
    pidvalue = 0;
    integral = 0;
  }

  pidvalue = proportional + integral;

  if (pidvalue <= 0) {
    pidvalue = 0;
  }

  if (pidvalue > 255) {
    pidvalue = 255;
  }

  analogWrite(pin, pidvalue); // sending the pid value to the heater

  delay(1000);
  
  cumerror=cumerror;
}
\$\endgroup\$
14
  • 1
    \$\begingroup\$ Where are your design specs? > Max Temp. rate of change expected. load profile, error tolerance, resolution, step change overshoot? \$\endgroup\$ Jul 14, 2020 at 16:48
  • 1
    \$\begingroup\$ Maybe you could explain what you are trying to implement rather than dumping the code. You have a PI controller with no antiwindup and are doing some weird stuff near the setpoint is what I make of it. \$\endgroup\$ Jul 14, 2020 at 17:03
  • \$\begingroup\$ Your code is poorly formatted, making it difficult to read and difficult to debug. Please edit to fix that first. Indent it properly and consistently. Remove blank lines within a section unless they are required for clarity. Remove all the Serial.print() lines as they (should) have no affect on the operation - unless you suspect there is a problem there (in which case you should explain why you think there is a problem or why we need to see them). Remove spaces before punctuation , or ; and add spaces after punctuation for legibility and clarity. i.e, Make it look professional. \$\endgroup\$
    – Transistor
    Jul 14, 2020 at 17:11
  • \$\begingroup\$ Marcus has tidied the code for you. Why are you manipulating the P constant based on the value of piderror? \$\endgroup\$
    – Transistor
    Jul 14, 2020 at 17:32
  • \$\begingroup\$ @Transistor , yeah , because i am assuming as the temperature approaches the set value , to reduce te PID error , i should reduce both P and I \$\endgroup\$ Jul 14, 2020 at 17:35

1 Answer 1

1
\$\begingroup\$

KP, KI and KD are usually fixed and not manipulated based on the error as you are doing. Instead try and get the controller working as P-only.

Try reducing it to this and get P-only control working properly: -

piderror = setpoint - inputvalue;
proportional = kp * piderror; 
pidvalue = proportional + integral;
if (pidvalue <= 0) {
  pidvalue = 0;
}
if (pidvalue > 255) {
  pidvalue = 255;
}
analogWrite(pin, pidvalue);

Start with a much gentler KP - maybe 1 or 2, try it out and report back. Plot a graph if you can and edit it into your question. We'll proceed from there.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.