Solar Differential Temp Controller Lock Ups

Hi All

Firstly - Amazing forum - All of my programming questions have so far been answered by reading previous posts and trying the suggestions already given.

But I have built my code and tested my setup and unfortunately it isn’t working…well it is but it frequently locks up. Basically this is a differential temperature controller - albeit a very basic one. It seems once it reaches its “switch on” temp the LCD stops displaying both tempreature readings, and 9 out of 10 times when turned on it also doesnt display the temp.

I have removed all delays as I suspected they were the issue but this hasnt helped.

Any guidance you can provide will be much appreciated.

Thanks Mike

#include "DHT.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define DHTPIN 26// what pin we're connected to
#define DHTPIN2 28// what pin we're connected to
#define DHTTYPE1 DHT22   // DHT 22  (AM2302)
#define DHTTYPE2 DHT22   // DHT 22  (AM2302)

const int diffON = 1;
const int diffOFF = 0.5;
const int RELAY1 = 42;

DHT dht1(DHTPIN, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);


void setup() {
  Serial.begin(9600); 
  dht1.begin();
  dht2.begin();
  
    pinMode(RELAY1, OUTPUT);
  digitalWrite(RELAY1, LOW);
  
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("Ret: ");
  lcd.setCursor(0,1);
  lcd.print("Sol: ");
}
 
void loop() {
  
  float t0 = dht2.readTemperature(); //roof/solar temp
  float t1 = dht1.readTemperature(); //pool return temp sensor
       if(t0 > (t1 + diffON))
     digitalWrite(RELAY1, HIGH);
     do{
     }while(t0 > (t1 + diffOFF));
     digitalWrite(RELAY1, LOW);

  lcd.setCursor(4,0);
  lcd.print(t0);
  lcd.setCursor(4,1);
  lcd.print(t1);
}

mike_perth:
Hi All

Firstly - Amazing forum - All of my programming questions have so far been answered by reading previous posts and trying the suggestions already given.

But I have built my code and tested my setup and unfortunately it isn't working....well it is but it frequently locks up. Basically this is a differential temperature controller - albeit a very basic one. It seems once it reaches its "switch on" temp the LCD stops displaying both tempreature readings, and 9 out of 10 times when turned on it also doesnt display the temp.

I have removed all delays as I suspected they were the issue but this hasnt helped.

Any guidance you can provide will be much appreciated.

Thanks Mike

Try replacing the relay write code (temporarily) with a write to pin 13 (the built in LED).
It's possible that the inductive kickback of the relay is crashing the Arduino. If your board runs stable when it turns the "relay" (LED) on and off, you've found the problem.

If that IS the problem, then try solving it with a snubber diode. Get a 1 amp, any volts diode from Radio Shack (a 1N4001, 4002, etc...) and connect it across the relay COIL contacts. The striped end of the diode goes to the positive side of the coil and the other end to the negative.

What happens is when the relay is energized, magnetic energy is stored in the coil. When you de-energize it, the magnetic field collapses and induces a voltage across the coil contacts (in the opposite polarity).

Faraday's law says that the faster the field collapses, the higher the voltage it generates. In fact, that's how a car ignition coil works. The magnetic field is built up with 12 volts, then when the circuit is broken (by the ignition computer or points in an older car), the field collapses in microseconds, inducing a very high voltage.... 25000 to 50000 or more volts that fire the spark plug.

Anyway, the diode across the relay coil conducts when the kickback occurs and instead of probably a few thousand volts of a spike, it gets clamped to 0.7 volts (the forward drop of the diode).

If you determine that the relay coil is the problem, try the diode. Putting a small capacitor (like a 0.1 uF ceramic or disk type) in parallel with the diode will help too. The capacitor will slow down the field collapse speed give the diode time to start conducting.

Good luck!

This is a problem:

const int diffOFF = 0.5;

This is an endless loop, if ever t0>t1:

    do{
     }while(t0 > (t1 + diffOFF));

Another thing to do before you post your code is a Ctrl-T on the source code in the IDE. That will reformat the code in a more common C style that most of us will find easier to read. It will also help you see things like this:

  if (t0 > (t1 + diffON))
    digitalWrite(RELAY1, HIGH);
  do {
  } while (t0 > (t1 + diffOFF));
  digitalWrite(RELAY1, LOW);

what is the do-while loop doing? If t0 is greater that the sum of t1 and diffOFF, what terminates the loop?

jremington:
This is a problem:

const int diffOFF = 0.5;

This is an endless loop, if ever t0>t1:

    do{

}while(t0 > (t1 + diffOFF));

Good catch!

My instinct was "relay coil + random lockups = inductive kickback problem". I wasn't even looking at the code for anything other than some external hardware that might be the cause.

Thanks all - commenting out my while "loop" has fixed the lockups...now to sort out code to turn relay off - thanks again much appreciated.