Temp controller

Hello Everyone,

I am currently working on a project to control temperature of my wine fridge using a DHT22 sensor controlling a SSR to kick the compressor on if the temp rises above 5C and off below 2C as well as turn a fan on if humidity is higher then 75% and off below 70%. When testing the program the ambient conditions are 41% and 20C and there is always 5v at pin 4 and always 0v at pin7, is this my code or is there something else possibly going wrong?

Thanks,

Vince

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define fan 4
#define comp 7

int maxHum = 75;
int minHum = 70;
int maxTemp = 5;
int minTemp = 2;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(fan, OUTPUT);
  pinMode(comp, OUTPUT);
  Serial.begin(9600); 
  dht.begin();
  
}

void loop() {
  // Wait a few seconds between measurements.
  delay(10000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  
  if(h > maxHum) {
      digitalWrite(fan, HIGH);
  if(h < minHum) 
      digitalWrite(fan, LOW);
  }
  if(t > maxTemp) {
      digitalWrite(fan, HIGH);
  if(t < minTemp) 
      digitalWrite(comp, LOW);
  }

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C ");

}

I am thinking that with 1 or more Peltier coolers that trigger on at 3.5C and off at 2.5C might do until the door opens, big temperature rise starts, hits 5C and the compressor takes down it to 2C… won’t the coolant still in the coils take it lower? If it hits 5C it will get a bit warmer before the cooling takes effect.

Peltier wafers use 12V to move heat from one side to the other. Keep the hot side cool and the cool side can be much colder. They’re inefficient, the fridge needs to be extra insulated or the compressor will cycle occasionally, but they can cycle without much if any penalty except running on full Amps of 12V DC. The more expensive Peltier wafers are used for vehicle and trailer refrigerators, do make ice and have no motor or seals or coolant to fail… just 12V.

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define fan 4
#define comp 7

int maxHum = 75;  // get in the habit of storing small values in small variables
int minHum = 70;   // every one of these should be byte not int
int maxTemp = 5;   // suppose you started with ints and one day array those. 
int minTemp = 2;    // will you think to change them then or be in a rush to goal?

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(fan, OUTPUT);
  pinMode(comp, OUTPUT);
  Serial.begin(9600);   //  speed up your Serial to 115200 to clear the print buffer quicker.
  dht.begin();
 
}

void loop() {
  // Wait a few seconds between measurements.
  delay(10000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  // That's because it runs on slow serial communications!

  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
 
  if (h > maxHum)              digitalWrite(fan, HIGH);
  else if (h < minHum)        digitalWrite(fan, LOW);

  if (t > maxTemp)              digitalWrite(fan, HIGH);
  else if (t < minTemp)        digitalWrite(comp, LOW);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");

}

I think that a thick wall box of tile layers or basalt for cold mass with very thick styrofoam with shiny metal vapor barrier outside, sealed and reflecting would make a steady coldbox.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What does your monitor show?

Thanks Tom... :slight_smile:

Hi,
This;

 if (h > maxHum) {
    digitalWrite(fan, HIGH);
    if (h < minHum)
      digitalWrite(fan, LOW);
  }
  if (t > maxTemp) {
    digitalWrite(fan, HIGH);
    if (t < minTemp)
      digitalWrite(comp, LOW);
  }

Should be this.

  if (h > maxHum)
  {
    digitalWrite(fan, HIGH);
  }
  if (h < minHum)
  {
    digitalWrite(fan, LOW);
  }
  if (t > maxTemp)
  {
    digitalWrite(fan, HIGH);
  }
  if (t < minTemp)
  {
    digitalWrite(comp, LOW);
  }

Nested if statements don’t work like you want them to.
Tom… :slight_smile:

Tremolo:
DHT22 sensor [...] on if humidity is higher then 75% and off below 70%.

Are you aware that this is within the margin of error of this device (which is no better than +/-5% for humidity)?

  if (h > maxHum)
  {
    digitalWrite(fan, HIGH);
  }
  if (h < minHum)
  {
    digitalWrite(fan, LOW);
  }
  if (t > maxTemp)
  {
    digitalWrite(fan, HIGH);
  }
  if (t < minTemp)
  {
    digitalWrite(comp, LOW);
  }

this appears to have minTemp override maxHum

since they can interdependently turn on the fan, it would seem that the logic would need to allow either condition to override the other.

(is interdependently a word ? )

also, since there is only one action could this be written as

  if (h > maxHum)  { digitalWrite(fan, HIGH); }
  if (h < minHum)   {digitalWrite(fan, LOW); }
  if (t > maxTemp)  {digitalWrite(fan, HIGH); }
  if (t < minTemp)   {digitalWrite(comp, LOW); }

[edit to add brackets above , exact same, but on one line
or, because it is a single action, without brackets, below ]

  if (h > maxHum)   digitalWrite(fan, HIGH); 
  if (h < minHum)   digitalWrite(fan, LOW); 
  if (t > maxTemp)  digitalWrite(fan, HIGH); 
  if (t < minTemp)   digitalWrite(comp, LOW);

I know I am alone in this sort of coding, but It really helps me to see all the code on one page and in a way I can quickly follow the actions.

dave-in-nj:

  if (t > maxTemp)  digitalWrite(fan, HIGH);

Should this not be .... digitalWrite(Comp, HIGH)" ?

CharlesDavis:
Should this not be .... digitalWrite(Comp, HIGH)" ?

I think you are right ! and the whole reason I like the one-line is to see that soft of thing !
is the scope to run the compressor by itself only based on temperature
and run the fan only based on humidity ?
are there 2 fans ? or no circulating fan for when in cooling mode ?
refrigerated coolers run the fans when the compressor is run
the wine fridge is a bit different.
assuming the compressor and fan run as a unit but independantly
and the fan can also be run as needed for humidity
then the logic might be
if temp too high, run compressor
if compressor is running, run fan
if humidity is too high, run fan
if compressor is off and humidity is low, turn off fan

if a relay is used to run the compressor, it could be connected to the fan
a second relay controlled by the humidity would address any overlap. ie: fan run is in parallel.
if there is no fan to circulate the chilling,...
I would have to go back and review the wiring diagram.... post#2 ?

Lose the DHT sensor. Use a normal temp sensor and humidity sensor. DHT are not worth the effort.

I made the following changes and it is now working.

  if (h > maxHum)  {digitalWrite(fan, HIGH);  }
  if (h < minHum)  {digitalWrite(fan, LOW);   }
  if (t > maxTemp) {digitalWrite(comp, HIGH); }
  if (t < minTemp) {digitalWrite(comp, LOW);  }

Not to sure about the brackets and how they actually work in this code (I took it from an example and I am a complete noob at this) but doing that and changing fan to comp did fix it.

As for the fan and compressor that was asked about, the fridge just has a coil in the back that gets cold. I was going to just run a 5v fan in the fridge to circulate air all the time then thought maybe have it run off the humidity signal since its there. Then if I run into the issue where humidity gets too low I can move the run to run all the time and add a mini USB humidifier to the system and wire it in to where the fan was.

So being that I am new to all this, is this the best way to go about this? I have an Arduino and a Raspberry Pi and would really like to use the Pi but I find it much easier to understand coding with Arduino. I can easily make changes to code I find online where as with the Pi it makes no sense to me. I would really like to add some type of GUI and data logger to the program which I feel is better suited for the Pi. But maybe I should use both and setup the Pi to run a GUI and data logger.

Thanks for the help!

Vince.

Ripcrow:
Lose the DHT sensor. Use a normal temp sensor and humidity sensor. DHT are not worth the effort.

Why?
Tom.... :slight_smile:

  if (h > maxHum)  {digitalWrite(fan, HIGH);  }
  if (h < minHum)  {digitalWrite(fan, LOW);   }
  if (t > maxTemp) {digitalWrite(comp, HIGH); }
  if (t < minTemp) {digitalWrite(comp, LOW);  }

if h > maxHum then h is also > minHum.

  if (h > maxHum)        digitalWrite(fan, HIGH);  // you only need the braces to hold multiple statements
  else if (h < minHum)  digitalWrite(fan, LOW);

The else makes it not run the 2nd check if the 1st was true. I don’t know if the compiler optimizer does this anyway.

Any time you code an if, think about what it doesn’t catch.

GoForSmoke:

  if (h > maxHum)        digitalWrite(fan, HIGH);  // you only need the braces to hold multiple statements

else if (h < minHum)  digitalWrite(fan, LOW);

you only need the braces to hold multiple statements

No, you need multiple brackets to make it understandable and help make it structured.
If you do not use { } I consider it bloody lazy.
It makes no difference to the compiled code.
Tom… :slight_smile:

TomGeorge:


Why?
Tom.... :slight_smile:

comparison

TomGeorge:
No, you need multiple brackets to make it understandable and help make it structured.
If you do not use { } I consider it bloody lazy.
It makes no difference to the compiled code.
Tom.... :slight_smile:

If I use { and } I give them their own lines and indent them the same as the if.

GoForSmoke:
If I use { and } I give them their own lines and indent them the same as the if.

Yes good practice.

GoForSmoke:

  if (h > maxHum)  {digitalWrite(fan, HIGH);  }

if (h < minHum)  {digitalWrite(fan, LOW);  }
  if (t > maxTemp) {digitalWrite(comp, HIGH); }
  if (t < minTemp) {digitalWrite(comp, LOW);  }




if h > maxHum then h is also > minHum.



if (h > maxHum)        digitalWrite(fan, HIGH);  // you only need the braces to hold multiple statements
  else if (h < minHum)  digitalWrite(fan, LOW);




The else makes it not run the 2nd check if the 1st was true. I don't know if the compiler optimizer does this anyway. 

Any time you code an if, think about what it doesn't catch.

So what should I have this code read? Should I change

  if (h > maxHum)  {digitalWrite(fan, HIGH);  }
  if (h < minHum)  {digitalWrite(fan, LOW);   }
  if (t > maxTemp) {digitalWrite(comp, HIGH); }
  if (t < minTemp) {digitalWrite(comp, LOW);  }

to

  if (t > maxTemp)        digitalWrite(comp, HIGH);  
    else if (t < minTemp)  digitalWrite(comp, LOW); 

  if (h > maxHum)         digitalWrite(fan, HIGH);  
    else if (h < minHum)  digitalWrite(fan, LOW);

Are braces needed?

Some changes you make just to tighten the code and yourself in the process.

The else statement is on the same level as the if, they spring from the same expression as false and not-false.

You can do ladder logic all on the same level:

if ( expression )
{
}
else if ( expression )
{
}
else if ( expression )
{
}
else if ( expression )
{
}
else
{
}

The switch-case statement makes a single expression to choose from many cases, the two serve different purposes best.

Braces are not required but can make it easier to quickly see if they're balanced and what logic they relate to. They are required to treat a series of lines as a group but in your example there's no need for braces except possibly source code readability.