Hi guys, please i need help.

I need to implementate PID regulator on arduino that can generate a PWM signal to control a relay output. I use the library example (title Relay_Output) but I don’t understand the code:

#include <PID_v1.h>

#define PIN_INPUT A0
#define RELAY_PIN 6

double Setpoint, Input, Output;
double Kp=2, Ki=5, Kd=1;

PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

int WindowSize = 5000;
unsigned long windowStartTime;

void setup() {

windowStartTime = millis();
Setpoint = 100;

//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);


void loop()
Input = analogRead(PIN_INPUT);

if (millis() - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
if (Output < millis() - windowStartTime)

else digitalWrite(RELAY_PIN, LOW);


Question 0) This is a PID controller?

Question 1) Why in function SetOutputLimits the second variable is WindowSize = 5000? The max limit is 255 or not?

Question 2) What is the Output? Output is calculate by compute() but i don’t understand this function

Question 3) What is the frequency of PWM output signal?

Question 4) In which way the dutycycle of signal is calculated in every loop?

I receive the input signal by temperature sensor and my target is keep the temperature costant, is enaugh this code? Thank you very much guys.

Your code is clearly incomplete.

Here’s a pretty good write up by the PID Library’s author: Improving the Beginner’s PID – Introduction « Project Blog
It’s multi-part, so keep clicking on the “NEXT >>” link to read more.