0% found this document useful (0 votes)
183 views

PID Library

This document discusses using a PID controller to control a relay output based on an analog input. It explains that the PID output is analog but the relay is digital, so it uses time proportioning control to translate the PID output into on/off times for the relay within a fixed window size. The PID output determines how long the relay is on during each window, with the remainder being off. It provides code to initialize the PID controller, set the output limits, read the analog input, run the PID computation, and toggle the relay pin accordingly within each window cycle based on comparing the PID output to the time within the window.

Uploaded by

Fathi Arsalan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
183 views

PID Library

This document discusses using a PID controller to control a relay output based on an analog input. It explains that the PID output is analog but the relay is digital, so it uses time proportioning control to translate the PID output into on/off times for the relay within a fixed window size. The PID output determines how long the relay is on during each window, with the remainder being off. It provides code to initialize the PID controller, set the output limits, read the analog input, run the PID computation, and toggle the relay pin accordingly within each window cycle based on comparing the PID output to the time within the window.

Uploaded by

Fathi Arsalan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PID Library

/********************************************************
* 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