100% found this document useful (1 vote)
113 views2 pages

Pid Control Practical

PID() creates a PID controller linked to an input, output, and setpoint. The PID controller calculates changes to the output variable based on the difference between the input and setpoint variables. The PID controller has parameters for proportional gain (Kp), integral gain (Ki), derivative gain (Kd), and direction (direct or reverse). The example code uses a PID controller connected to an analog sensor input and digital output pin to control a relay using time proportioning control, where the PID output value determines the proportion of time the relay spends on versus off during a fixed window period.

Uploaded by

Brooks Ortiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
113 views2 pages

Pid Control Practical

PID() creates a PID controller linked to an input, output, and setpoint. The PID controller calculates changes to the output variable based on the difference between the input and setpoint variables. The PID controller has parameters for proportional gain (Kp), integral gain (Ki), derivative gain (Kd), and direction (direct or reverse). The example code uses a PID controller connected to an analog sensor input and digital output pin to control a relay using time proportioning control, where the PID output value determines the proportion of time the relay spends on versus off during a fixed window period.

Uploaded by

Brooks Ortiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PID()

Description
Creates a PID controller linked to the specified Input, Output, and Setpoint. The PID algorithm is in parallel
form.
Syntax
PID(&Input, &Output, &Setpoint, Kp, Ki, Kd, Direction)
Parameters
Input: The variable we're trying to control (double)
Output: The variable that will be adjusted by the pid (double)
Setpoint: The value we want to Input to maintain (double)
Kp, Ki, Kd: Tuning Parameters. these affect how the pid will chage the output. (double>=0)
Direction: Either DIRECT or REVERSE. determines which direction the output will move when faced with a
given error. DIRECT is most common.
/*******************************************************
* PID RelayOutput Example
* Same as basic example, except that this time, the output
* is going to a digital pin which (we presume) is controlling
* a relay. The pid is designed to output an analog value,
* but the relay can only be On/Off.
*
*
To connect them together we use "time proportioning
* control" Tt's essentially a really slow version of PWM.
* First we decide on a window size (5000mS say.) We then
* set the pid to adjust its output between 0 and that window
* size. Lastly, we add some logic that translates the PID
* output into "Relay On Time" with the remainder of the
* window being "Relay Off Time"
********************************************************/
#include <PID_v1.h>
#define RelayPin 6
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 100;
//tell the PID to range between 0 and the full window size

myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Input = analogRead(0);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
unsigned long now = millis();
if(now - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output > now - windowStartTime) digitalWrite(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
}

You might also like