EcoRenovator  

Go Back   EcoRenovator > Improvements > Tools
Advanced Search
 


Blog Register 60+ Home Energy Saving Tips Recent Posts Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Display Modes
Old 02-13-15, 12:50 PM   #1
buffalobillpatrick
Master EcoRenovator
 
Join Date: Mar 2014
Location: Florissant, Colorado
Posts: 599
Thanks: 814
Thanked 59 Times in 55 Posts
Default 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

}

buffalobillpatrick is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:45 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, vBulletin Solutions Inc.
Ad Management by RedTyger
Inactive Reminders By Icora Web Design