PID thermostat with DS18b20

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

Heating your house will result in a very slow change in temperature for the sensor to read. The temperature sensor you are using is not lightening fast. So, I wonder why you need PID at all. When all you are doing is turning the heater on or off, I really can't see the need for PID.

On any given pass through loop(), it is cold enough to need to start the heater, or it isn't. On any given pass through loop() it is warm enough to not need to be heating anymore, or it isn't.

Add a degree or so of Hysteresis.

PaulS:
Heating your house will result in a very slow change in temperature for the sensor to read. The temperature sensor you are using is not lightening fast. So, I wonder why you need PID at all. When all you are doing is turning the heater on or off, I really can't see the need for PID.

On any given pass through loop(), it is cold enough to need to start the heater, or it isn't. On any given pass through loop() it is warm enough to not need to be heating anymore, or it isn't.

I find in the market thermostats with PID process like Siemens rev24 and i want to build something like this in order to manage better my house.

What do you think that PID is going to do for you that on and off control will not?

Also, as the temperature gets close to the set point, your PID system is going to call for running the heat for a small proportion of your window. Do you really want to turn it on for less than a second?

What is your heating system. I'd guess that most types would have their service lifetime substantially reduced by such rapid cycling.