Hi all,
I try to build a PID thermostat with DS18b20. I find the below program :
#include <PID_v1.h>
// Libraries for the DS18B20 sensor
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 on PIN 6 on the Arduino
#define ONE_WIRE_BUS 6
//Solid state relay on PIN 5 on the Arduino
#define RELAY_PIN 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=5, Ki=3, Kd=3;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int WindowSize = 10000;
unsigned long windowStartTime;
void setup()
{
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.begin(115200);
Serial.println("Starting");
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 22;
//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()
{
sensors.requestTemperatures();
Input = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(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);
else
digitalWrite(RELAY_PIN, LOW);
}
The problem it was that if i increase the temp over 22 the relay output take the value "1" all the time and the output take the max value. That's meaning, over heating the house!!!!! When i start to decrease the temp below the 22 the output change value but the relay output change status only for 2-3 times before the output take the zero value......
Thanks in advance