Temperature controlled relay

Hi all,

I am considering using an Arduino board to control a relay based on temperatures from auxiliary sensors connected to the board. I have not yet purchased a board as there are many models so I am looking for some advice on which board to buy.

Some requirements listed below:

  1. The temperature range I am looking at is from 5C to 40C or higher.

  2. The possibility of data logging the temperature values.

  3. At least 2 temperature inputs

  4. At least one output to control my motor relay.

Thanks in advance from your community…

Qbit

Sounds like you are building your own thermostat, but without more information it’s hard to give a specific recommendation. The choice of Arduino board is probably a later decision. First you need to determine what temperature sensors you will use. Some require a 3.3V interface which could influence your choice of board. Likewise the drive circuit for the relay must also be considered. If it really is a motor relay (large current, high voltage) then it probably cannot be driven directly from the Arduino. Again, without specifics it’s hard to say. Look at the specs on the relay to determine the drive. Figure a drive circuit compatible with the temperature sensors (i.e. 3.3V or 5V). Are you space constrained? How will you power the board? Do you need to display or communicate sensor values or relay state? Once you have determined these you can more easily pick the Arduino board.

Thanks Uchip,

After I posted, I found several publications on line. I am leaning towards the uno board as it seems to have a lot

of documented support. Although for the difference in cost, the duo looks like a higher end unit but not sure how much support around it. Looks like I have a bit of reading to do.

The motor will have its own control to take care of the current so the relay will only be required to handle the coil current of the motor control. I see there are several temp sensors( 35,36) and was considering experimenting with one of them. (Sorry, i don’t have the proper part numbers to reference). One of the home automation books describe a project similar to what I need. So thanks for the help so far.

Qbit

ok, so far i have managed to select the Uno board and have been successful with using the TMP36 temperature sensor to read and display though the serial monitor. Additionally, I purchased a Relay shield, which I am going to use to run a small pump off one of the 4 relays. (relay board http://www.robotshop.com/ca/productinfo … lang=en-US)

I have plans for the other relays on the board for a later addition to my project. But first things first, I want to have two temperatures compared and based on the difference, the pump will cycle when a max collector temperature has been achieved and there is a suitable difference between the holding tank and the collector. (please see operational description below)

Just so you don’t think I am a slacker, I have been doing the lessons on adafruit http://learn.adafruit.com/search?q=simon+monk but things are moving too slow for me as my system needs implementation asap as the summer approaches.

Here is a brief outline of my project.

I have built a solar collector for heating water which gets pumped to a holding tank. The holding tank is then used several ways. At the moment I am sending my household cold water line through the holding tank via a high density heat exchanger and raising the temperature of the water prior to it`s entry to my house domestic water heater. This reduces the work the water heater has to do, saving me $$$. The temperature sensors will go on the holding tank and on the solar collectors. When the collector hits 50 deg C, and the tank is say at 20 deg C the programming should have the pump cycle until the collector reaches within 1 degree of the tank temperature.

Typically, I am seeing the tank increase about 2 degrees C on each pumping cycle. So, in this example it would end up at about 22 deg C. (It is a 150 gallon tank so each cycle I only get a marginal increase.)

At this point the solar heated water will have dropped and when it reaches the 23 deg C mark (1 deg diff) it should stop the pump and wait for an increase in collector temperature back to the high target of 50. *I havent given any thought to what happens if it only reaches a point less than 50.* The gist of it is that I dont want to exceed 50 deg but want to cycle when it has a decent difference in temperature between the tank and the collector. Also once the tank is heated fully, I may have to cycle a different pump to divert the extra hot water so the collector does not overheat. So far it has hit 70 deg C which is getting near the upper limit of the plumbing I have.

Here is where I am with trying the relay shield:

I have tried to operate the relay shield but have not been successful with the code I have found from the website.

/*

This Sample code is for testing the Relay shield V2.1 for Arduino.

Editor : Phoebe

Date : 2013.2.28

Ver : 0.1

Product: Relay shield for Arduino

SKU : DRI0144

Hardwares:

  1. Arduino UNO

  2. Relay Shield For Arduino V2.1

3 Power Supply:7~ 12V

*/

byte relayPin[4] = {

2,7,8,10};

//D2 → RELAY1

//D7 → RELAY2

//D8 → RELAY3

//D10 → RELAY

char input=0;

int val;

void setup() {

for(int i = 0; i < 4; i++) pinMode(relayPin*,OUTPUT);*
Serial.begin(57600);
delay(100);
Serial.println(“Press 1-4 to control the state of the relay”);
Serial.println(“waiting for input:”);
for(int j = 0; j < 4; j++) digitalWrite(relayPin[j],LOW);
}
void loop() {
if (Serial.available())
{
char input= Serial.read();
if(input != -1)
{
switch(input)
{

case ‘1’:
Serial.println(“Relay1”);
val=digitalRead(relayPin[0]);
val=!val;
digitalWrite(relayPin[0],val);
break;
case ‘2’:
Serial.println(“Relay2”);
val=digitalRead(relayPin[1]);
val=!val;
digitalWrite(relayPin[1],val);
break;
case ‘3’:
Serial.println(“Relay3”);
val=digitalRead(relayPin[2]);
val=!val;
digitalWrite(relayPin[2],val);
break;
case ‘4’:
Serial.println(“Relay4”);
val=digitalRead(relayPin[3]);
val=!val;
digitalWrite(relayPin[3],val);
break;

default:
if(input != ‘\r’ && input != ‘\n’)
Serial.println(“invalid entry”);
break;
}
}
// else unablerelay();
}
}
When I upload this to the uno with the shield attached, I get it to display the lines “Press 1-4 to control the state of the relay” and
“waiting for input:” I don`t have any buttons connected but would like it to cycle from the programmed output of the temperature sensors.
Any help with online lessons or sites to find some code would be appreciated.
:?
Qbit

Seems like you are off to a good start. You have researched hardware to meet your needs and have started to assemble the system by testing individual components or subsystems to get them working before mashing everything together. I suggest that you continue on that path. For instance, get the sample code for Blink working on your hardware. Then try modifying the Blink code to turn on the Arduino built-in LED when the temperature is above a threshold. Write some simple routines to turn the relay on and off. Once you have these basics nailed then you can start piecing together the whole system.

One suggestion that I would make is that you plan out the overall program at the same time you are working on coding the individual pieces. The idea is to make sure that you have all the pieces you need. In the old days you might have planned by making a flow chart. You don’t have to do that. Do what works for you. I like to break the problem down into steps that I write out in English. For Example:

(1) Program the pins as outputs

(2) Turn all the relays off

(3) Set up the serial port to send status and debug messages

Loop (Arduino does the loop for you)

(4) Measure temperature of heat exchanger and put it in HeatExchanger_Temp

(5) Measure temperature of holding tank and put it in Tank_Temp

(6) If the HeatExchanger_Temp is greater than Tank_Temp turn on pump for 5 minutes

I know that’s not everything you wrote, but this should give you an idea of how to write out your plan. Once you have it written out you can start to translate each line into code. If the line is too complex to code, break it down into multiple simpler sentences. This might give you more bits and pieces to experiment with. Once the pieces are working, your plan tells you how to put them all together.

BTW, the Relay sample code is not looking for buttons. It is looking for characters coming in on the serial line. In other words, it is responding to you typing on the keyboard in the serial monitor. You should be able to turn the relay (and therefore your pump) on and off from the keyboard just with the sample code. I would say it would be useful for you to achieve keyboard control before writing your own code to control the relay. It will ensure that the Arduino is controlling the relay correctly and that the relay is controlling the pump correctly. (That is: Is the pump really connected to Relay1? Is RELAY1 really connected to D2?)

Once you do move to writing your own code to control the relay, I suggest you simplify the sample code some. Since you are likely to be controlling the relays individually it’s not so important to put them into an array or do everything with “for(” loops. Instead you could do this:

#define HeatExchanger_Pump 2
// This defines the heat exchanger pump to be controlled by the relay on D2 = RELAY1
#define Cooling_Pump 7
// This defines the other pump that keeps the exchanger from over heating to be
// controlled by the relay on D7 = RELAY2

// To turn ON the HeatExchanger_Pump use this line of code:
digitalWrite(HeatExchanger_Pump, HIGH);

// To turn OFF the HeatExchanger_Pump use this line of code:
digitalWrite(HeatExchanger_Pump, LOW);

There’s nothing wrong with the sample code but by naming the variables something meaningful you can more easily read what the code is doing specific to your application. Code that reads more easily should make it easier to modify and also to find bugs.

I hope this gives you some direction for next steps. Good luck with your project!

  • Chip

Chip

Thanks for the advice. so far I have figured out how to read two temperatures and display them with the serial monitor.

I have also had success with turning the relays on and off on a timed cycle. By manipulating the code I am in essence learning by trial an error while reading the beginners guide to programming at the same time. I am having trouble with understanding how to hold values and use them in my “if” statement effectively. I am going at it again tonite and will try some of the methods you pointed out.

Here is what I have so far as my sketch. It is successfully displaying the separate temperatures but the relayPin value always stays at “16” on the serial printout??

const int temperatureSPin = 0;
const int temperatureTPin = 1;
int relayPin = A2; // select the pin for the relay control
void setup()
{
// declare the relayPin as an OUTPUT:
pinMode(relayPin, OUTPUT);

Serial.begin(9600);
}
void loop()
{

float voltageS,voltageT, degreesCS, degreesCT, degreesF;
voltageS = getVoltage(temperatureSPin);
voltageT = getVoltage(temperatureTPin);
//print voltages at this point. if done after the conversion
//you only get a value of 1 for some reason
Serial.print(" vTank: ");
Serial.print(voltageT);
Serial.print("vSolar: ");
Serial.print(voltageS);

degreesCS = (voltageS - 0.5) * 100.0;
degreesCT = (voltageT - 0.5) * 100.0;

degreesF = degreesCS * (9.0/5.0) + 32.0;
{

if ((voltageS-voltageT)>.03) digitalWrite (relayPin,HIGH);
}

Serial.print(" deg CS: ");
Serial.print(degreesCS);
//Serial.print(" deg F: ");
//Serial.println(degreesF);

Serial.print(" deg CT: ");
Serial.print(degreesCT);
Serial.print(" relayPin: ");
Serial.println(relayPin);

delay(1000); //off time
}
float getVoltage(int pin)
{
return (analogRead(pin) * 0.004882814);
}

A few notes:

I had the relay turning on and off using a sample sketch so I know it works ok. (analog pin 2)

I have the board powered up with a 9v battery as the relays would not fire with just the usb power.

Here is a sample of what the serial monitor is displaying while running the sketch.

vTank: 0.73vSolar: 0.72 deg CS: 22.27 deg CT: 22.75 relayPin: 16

Qbit

FYI

Thanks Mee_n_Mac! Have you ever done a count? How many of your posts are explaining the code tags?

Qbit,

I’m not sure what you are expecting from your printout. The line that you are using now will print out the value of the relayPin variable. You set that variable to “A2” which is shorthand in the Arduino IDE for “whichever I/O pin that is defined as A2 on the hardware specified for this sketch”. In your case that resolves to 16. It won’t change unless you assign a new value to relayPin.

If what you are trying to do is read the state of the pin then I think the following line will do the trick:

Serial.println(digitalRead(relayPin));

Seems like you are making steady progress. Keep going!

Chip

uChip:
Thanks Mee_n_Mac! Have you ever done a count? How many of your posts are explaining the code tags?

*About* 9752 posts. :mrgreen:

I have added the line you suggested and now get A2 to display either a 1 or 0 depending on weather there is the defined difference between my two temperature sensors. It works like a charm. Now I am trying to have it turn on relay 1 on digital input 2. I think I am confusing analog inputs with digital inputs. I am only using the serial monitor so I can see what is going on when testing. How do I get D2 to go HIGH (turning on relay 1) when A2 reads 1? (or am I just complicating things?)

// We'll use analog input 0 and 1 to measure the temperature sensor's
// signal pins.
 
const int temperatureSPin = 0;
const int temperatureTPin = 1;

 int relayPin = A2;      // select the pin for the relay

void setup()
{
  // declare the relayPin as an OUTPUT:
 pinMode(relayPin, OUTPUT); 
  
  Serial.begin(9600);
}

void loop()

{
   
  float voltageS,voltageT, degreesCS, degreesCT, degreesF;

  voltageS = getVoltage(temperatureSPin);
   voltageT = getVoltage(temperatureTPin);
   //print voltages at this point. if done after the conversion
   //you only get a value of 1 for some reason
 Serial.print("   vTank: ");
  Serial.print(voltageT);
  Serial.print("vSolar: ");
  Serial.print(voltageS);
  
   degreesCS = (voltageS - 0.5) * 100.0;
   degreesCT = (voltageT - 0.5) * 100.0;
  
  degreesF = degreesCS * (9.0/5.0) + 32.0;

{   
  
if ((degreesCS-degreesCT)>1) digitalWrite (relayPin,HIGH);
else digitalWrite (relayPin,LOW);

}
  
  Serial.print("  deg CS: ");
  Serial.print(degreesCS);
  //Serial.print("  deg F: ");
  //Serial.println(degreesF);
  
 
  Serial.print("  deg CT: ");
  Serial.print(degreesCT);
  Serial.print("  relayPin: ");
  Serial.println(digitalRead(relayPin));
 
  delay(1000); //off time
}

float getVoltage(int pin)
{
    return (analogRead(pin) * 0.004882814);
}
 
/*void loop()/*my second if else statement that gives more errors grrrrr

{
   if (relayPin,HIGH)digitalWrite (A2,HIGH); 
else digitalWrite (A2,LOW);

}*/

Qbit,

Have you tried setting relayPin to 2 instead of A2? You say that the relay is on D2 but you are setting A2 HIGH or LOW.

Did you get the sample sketch to work? If so, how does the sample sketch define that pin? You can use that same definition.

Chip