02-13-15, 12:50 PM | #1 |
Master EcoRenovator
Join Date: Mar 2014
Location: Florissant, Colorado
Posts: 599
Thanks: 814
Thanked 59 Times in 55 Posts
|
Arduino PID Relay Temperature Control
I stole this:
Code:
/******************************************************** * Arduino PID Relay Output Example * The output is going to digital pin 6 which is controlling * a relay. * The current temperature is read via pin A0 * * The pid Library is designed to only output an analog value, * but the relay can only be On/Off. * * To connect them together we use "time proportioning control" * It's a really a long duration version of Pulse Width Modulation (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 //D6 #define T_pin A0 //A0 //Define Variables we'll be connecting to double Setpoint, Input, Output; //Define the Tuning Parameters double Kp=2, Ki=5, Kd=1; //Specify the PID links PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); int WindowSize = 5000; //5 sec unsigned long windowStartTime; void setup() { windowStartTime = millis(); //get current time Setpoint = 100; //Desired Temperature //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(T_pin); //Pass current temperature as Input to PID myPID.Compute(); unsigned long now = millis(); //get now time if(now - windowStartTime > WindowSize) { //time to shift the Relay Window windowStartTime += WindowSize; } if(Output > now - windowStartTime) digitalWrite(RelayPin,HIGH); //ON else digitalWrite(RelayPin,LOW); //OFF } |
|
|