Sketch freezes

I have a setup consisting of a HTU21D temp/humi-sensor, it’s connected to (+)3.3V, (-)GND, (SDA)A4 and (SCL)A5.
Running the example in the library folder works fine.
Connected is a relay (+)5V, (-)GND, and (INI)D10). It will switch on and off as intended (A lightbulb on 220V connected to COM and NC.)

The sketch is running on a Arduino UNO R3.

After running a short while, anything between 30s and 2min it will freeze up.
I tried disabling Serial prints, but it didn’t help. The project is intented to keep a box at 44 centigrade using the lightbulb as heating. Nothing much, so I am all the more frustrated it doesn’t work.

Hope someone sees somethin I sure can’t see.

#include <cactus_io_HTU21D.h>
#include <Wire.h>
//relaypin
#define relay 10
//benchtemp
float benchTemp = 44.4;
//time to heat the box - keeping relay up
#define relayTime 1000

float tempReading = 0.0;

HTU21D htu;

void setup() {
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}

void loop() {
  htu.readSensor();
  tempReading = htu.getTemperature_C();
  Serial.print("tempReading: ");
  Serial.println(tempReading);
  if (benchTemp > tempReading) {
    Serial.println("ON-LOW");
    digitalWrite(relay, LOW);
    delay(relayTime);
  }
  if (tempReading >= benchTemp) {
    /  Serial.println("OFF - HIGH");
    digitalWrite(relay, HIGH);
    delay(relayTime);
  }
  else {
    Serial.println("Else");
    delay(10);
  }

  Serial.println("--------");
  digitalWrite(relay, HIGH);
  delay(3000);
}

There is a good chance you are drawing too much from the Arduino to sustain the relay.

A separate power supply with only the ground shared to the Arduino would be much better.
Try the sketch after the Arduino has been off for a while and keep checking the temperature of the regulator on the Arduino and other chips on the board. better still don't do that just jump to a power supply for the relay !

I tried a few things after posting.

One: The sketch performs well enough if there is no load on the 220V side of the relay.
Two: A separate power supply 5V 150mA with shared ground with the arduino. It seems to work much better. As we speak it has ran for a few minutes.

I am surprised that these relays are labeled "Arduino ready". Doesn't that mean that all the pins should go straigth to Arduino and all is fine? On the module there's nothing about currentdraw.

Thanks for the input.

Oh lots of things are "Arduino" ready its just a matter of working out what the Arduino can do from the spec sheet and what you are attaching needs to make it work.

The Arduino is more than capable of triggering many relays.
However is is not capable of POWERING them all.

Same goes for LED's and lots of other devices where the schematic will show a seperate power supply.

Also be aware that not all relay modules are equal.
Some have OPTO isolation and some don't.

The OPTO enabled ones are much safer to use with Arduino's.
if in doubt google "Arduino relay" and select the pictures option and see the many different methods used.

You can IGNORE ANY that use the Arduino as the power source as thats great for a short test only and nothing more.

The Arduino can provide a certain amount of current from the 5 V pin, which also includes the microcontrollers, 3.3 V voltage regulator and onboard LEDs. If it's powered from USB that's 500 mA. If it's powered with a 5 V power supply via the 5 V pin it's the rating of the power supply (or traces). If it's powered from the barrel jack or Vin pin it will be dependent on the voltage regulator used on your board and what voltage you're powering it at (higher input voltages cause the regulator to run hotter and thus reduce the maximum current). If you go over the limit the voltage regulator/computer/fuse/power supply will reset or shut down. So you do have to pay attention to your power budget. Relays draw a good bit of current, but certainly it is possible to power small ones such as the Songle relays commonly found on the relay modules from an Arduino's 5 V pin so it depends on what other curren. It's handy to get a multimeter so you can check these sorts of things. If you don't have one you can get cheap digital ones on eBay/Aliexpress for around $3 USD w/ free shipping that work fine.

However, from your result of removing the load causing the problem to go away it sounds like the issue could be electrical interference. Separately powering relays that are controlling mains voltage could have reduced this. Most of the "Arduino ready" relay modules have an optoisolator and a jumper you can remove to allow you to use a separate power supply for the relay with no chance of mains voltage finding its way into your low voltage parts. You could also work on placing the relay and 220 V wires so they are farther away from any parts of your circuitry that could pick up the electrical interference.

It is a small Songle relay on some chinese print. In the datasheet it says 71mA, that leaves some room. The Uno should be able to deliver 200mA on VCC and 40mA on digital pins. The HTU21D is only 0.5mA.
I guess that by supplying it via external power it limited electrical interference which was causing the problems. I have used similar relays powered via Arduino numerous times without any problems.
My aquarium has 3 fluorescent tubes of 18W running on 220V, all three and a heating element 90W is connected to 4 relayes powered by an Arduino on a 7.5V 2A power supply. That works, but one single ligthbulb, and it goes crazy.
Could it have something to do with the delays between turning the control pin from HIGH to LOW? Or just the quality of the relays. A lot the stuff I order from China varies quite a bit in build quality.