// https://wokwi.com/projects/410423413759806465
// Arduino_PID_Example_Relay
//
// Code from
// https://github.com/br3ttb/Arduino-PID-Library/blob/master/examples/PID_RelayOutput/PID_RelayOutput.ino
// Modifications:
// Add pinMode(RELAY_PIN,OUTPUT);
// Switch sense of output conditional per https://github.com/br3ttb/Arduino-PID-Library/issues/136
//
// See also:
//
// https://wokwi.com/projects/412042610362971137 -- PID_Basic.ino
// https://wokwi.com/projects/410423413759806465 -- PID_Relay.ino
// https://wokwi.com/projects/411487751748467713 -- PID_AdaptiveTunings.ino
//
// Tune for mid-range sensor-pot with Setpoint=512
// Tune Kp = 5000/512=9.97 for full on at Pot=0
/********************************************************
* 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" it'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 PIN_INPUT 0
#define RELAY_PIN 6
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
//double Kp=9.76, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
pinMode(RELAY_PIN,OUTPUT);
windowStartTime = millis();
//initialize the variables we're linked to
// Setpoint = 100;
Setpoint = 512;
//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(PIN_INPUT);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if (millis() - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
// if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
if (Output > millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
else digitalWrite(RELAY_PIN, LOW);
}
You are the process
Move the "Process" slider toward the lit LED