Relays causing sensors to stop working.

Hi,
I got a DHT22 + cheapy ebay 5v arduino relays.
Anyhow, I’ve made a thermostat that turns an AC on at a set temp and off at a target temperature.

The temperature sends values in serial.
The relay is connected to the 5v and ground on the Arduino, the trigger is set to pin 9.

The code works perfectly when the relay is not connected, but when the relay is connected to pin 9, my temperature sensor will show a NAN value for a brief time after the relay is on.
When the AC reaches the target temperature, the relay turns off and this is where my temp sensor fails to work altogether unless i close and open the serial monitor.

Has anyone experienced a similar problem like this? Is there supposed to be a resistor somewhere like a push button needs?

My code is pretty simple.

int fan = 0; // 0 meanning off, 5 meaning on

int OnTemp = EEPROM.read(100); // gets last setting from the EEPROM
int OffTemp = EEPROM.read(101);

void setup() {
dht2.begin();
Serial.begin(9600);
pinMode(EvRelay, OUTPUT); // pin the relay is plugged into.
}

void loop() {
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();

Serial.print("OnTemp = ");
Serial.println(OnTemp);
Serial.print("OffTemp = ");
Serial.println(OffTemp);
Serial.print("temp = ");
Serial.println(t2);
Serial.println(fan);

if (t2 >= OnTemp )
digitalWrite(EvRelay, HIGH), fan = 5;
else {}
if
(t2 <= OffTemp)
digitalWrite(EvRelay, LOW), fan = 0;
else {}

delay(1000);
}

How is your relay connected? You are driving the coil with a transistor? The relay is NOT powered by the Arduino? The relay coil has a kikckback diode? Why are you asking a hardware question without supplying a schematic?

Why didn't you read the "how to use the forum" stickies?

This will work but is unusual:

if (t2 >= OnTemp )
  digitalWrite(EvRelay, HIGH), fan = 5;
else {}
if
(t2 <= OffTemp)
  digitalWrite(EvRelay, LOW), fan = 0;
else {}

Normally, this would be written as (untested):

if (t2 >= OnTemp ) 
  {
    digitalWrite(EvRelay, HIGH);
    fan = 5;
  }
if (t2 <= OffTemp)
  {
    digitalWrite(EvRelay, LOW);
    fan = 0;
  }

As the OP wrote, the “…code is pretty simple.”
The issues are most likely with the wiring that cannot be seen.

Yes, apologies if it was in the wrong section. I was actually reading some other post about relays in this section and it was late at night.

The wiring is quite simple

DHT22 5v and ground, pin 2 for the signal

The relay itself, has 5v and ground and trigger pin to pin 9
The relay works fine and all too.

this is pretty much the exact relay.

Some Arduinos do not have enough power for a DHT22 and an unspecified relay board. Try a real 5 volt supply but connect the grounds.

What Arduino? What relay board?

relay appears to be the same one I have, which pulls 70mA on the coil.

Finally find the answer!

On the bus on my phone atm,
I have an external power supply plugged into the arduino. It's an Uno. I will try using a separate 5v for the relay and connect the grounds ( ground from arduino GRND and the external 5v supplies negative terminal I hope you meant).

Here's how to take advantage of the opto isolation feature of your single relay module: