Temp Sensor to trigger relay

I am attempting to use a temperature sensor to trigger a couple of events. If the temp is below the threshold, light the green led and set relay to off, if it above the threshold, light a red led and set the relay to on. When I disconnect the relay, the led’s and temp/voltage readings act as expected, when I add the relay, the voltage bounces, see serial output below;

voltage: 0.91 deg F: 105.48
voltage: 0.90 deg F: 104.60
voltage: 0.90 deg F: 104.60
voltage: 0.91 deg F: 104.60
voltage: 0.90 deg F: 104.60
voltage: 0.97 deg F: 116.02
voltage: 0.90 deg F: 104.60
voltage: 0.97 deg F: 116.02
voltage: 0.90 deg F: 104.60
voltage: 0.97 deg F: 116.02
voltage: 0.90 deg F: 104.60
voltage: 0.97 deg F: 116.02
voltage: 0.90 deg F: 104.60
voltage: 0.97 deg F: 116.02
voltage: 0.91 deg F: 105.48
voltage: 0.90 deg F: 104.60
voltage: 0.91 deg F: 105.48
voltage: 0.91 deg F: 105.48
voltage: 0.91 deg F: 105.48

when I disconnect the relay, all returns to normal. Can this be done powering the relay from the Arduino UNO, or does the relay need it’s own power source?

Sketch–

const int temperaturePin = 0;
const int redPin = 13; // LED pin
const int greenPin = 12;
const int RELAY1 = 7;

void setup()
{
Serial.begin(9600);

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(RELAY1, OUTPUT);
}

void loop()
{

float voltage, degreesC, degreesF;

voltage = getVoltage(temperaturePin);
degreesC = (voltage - 0.5) * 100.0;
degreesF = degreesC * (9.0/5.0) + 32;

if (degreesF < 105)
{
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, LOW);
digitalWrite(RELAY1, 0);
}
else
{
digitalWrite(greenPin, LOW);
digitalWrite(redPin, HIGH);
digitalWrite(RELAY1, 1);
}
Serial.print("v: “);
Serial.print(voltage);
Serial.print(” - tF: ");
Serial.println(degreesF);

delay(1000);
}

float getVoltage(int pin)
{

return (analogRead(pin) * 0.004882814);

}

Thank you.

There are relays that the arduino can drive, but it rather looks as though the type you're using isn't one of them - what is it?

To give a better idea what's going on, put a serial print in setup - it'll show you whether switching the relay is resetting the arduino.

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

As wildbill says, the relay may be your problem, the output of the arduino is only rated to 40mA at 5V.

Tom...... :)

The relay I am using is - SainSmart 4-Channel Relay Module, see link for more info,

http://www.amazon.com/gp/product/B0057OC5O8/ref=oh_aui_detailpage_o04_s00?ie=UTF8&psc=1

With the Serial.print, what parms are you looking for? I'm pretty new at this. Also, I will attach a drawing when I return to my shop tomorrow.

Thanks.

This is more of a hardware question than programming. You might need a relay board with optocouplers. They isolate the arduino from stray high voltage from damaging or interfering with signals.

Here is a drawing, not great, but I think you'll be able to read it...

I ordered a new relay with optocouplers today, I will post the results.

Also, I’ve noticed that if you turn a relay on and off like this, it’s usually not ideal.

 if (degreesF < 105)
  {
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, LOW);
    digitalWrite(RELAY1, 0);
  }
  else
  {
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, HIGH);
    digitalWrite(RELAY1, 1);
  }

If the temperature hovers around 105, your relay would turn on and off very frequently.

I usually do a range situation.

 if (degreesF < 105)
  {
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, LOW);
    digitalWrite(RELAY1, 0);
  }
  else if (degreesG > 106)
  {
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, HIGH);
    digitalWrite(RELAY1, 1);
  }

That makes a lot of sense, thank you. Also, I threw a diode inline on the voltage to the relay and that fixed the issue with the constant trips. Still anxiously awaiting the new relay board for testing though.

New relay is here, but still need the diode inline... aaargh...

does your temp sensor have a pull up resistor?