I want to control my bar fridge with temp sensor and relay,

This is my first project with code, i copied this: https://robojax.com/learn/arduino/?vid=robojax-DHT22_11_TM1637_relay


I want some extra code that will prevent the fridge (ie relay) from going on too often.

For my purposes I want the relay to close if the temp is above 4c, but only if the relay has been off for say 20min or more. FYI this is because my power source does not like to start the compressor when it is hot, so i don't want it cycling if it was just on.


1 Answer 1


Cool question! ;-) Instead of a fixed timer, I would build in some (so-called) hysteresis, which means turning refrigeration on above a certain temperature (say 4 °C) but only turning off when the temperature has dropped again to a temperature well below that (say 3 °C). This ensures the temperature stays within set limits while at the same time preventing fast cycling. You don’t need any timers for this and it will work in any situation (even when it’s very hot or very cold outside of the fridge). This is how commercial thermostats work.

If you really need to ensure the compressor cools enough between cycles, you just need to start a timer when it turns off, and not turn on again before it expires. Look at examples like BlinkWithoutDelay for some coding ideas.

  • Thanks, I actually found BlinkWithoutDelay about an hour before reading your message. But i am relay out of my wheel house here, how to i put the 2 together? BTW I like the Hysteresis idea, but the door opining can cause a 5 degree rise in temp, that takes a few minutes or longer to stabilize again. Jul 12, 2020 at 0:50
  • You could add a tactile / magnetic (Reed / Hall Effect) switch to detect when the door is open, you could use this as a trigger to tell the software why such a dramatic drop has occurred and to wait x time till temperature settles again. The Hysteresis idea sounds like a good solution as well, you could achieve the same effect by calculating a rolling average over a defined time period and only enable the fridge when this rolling average drops below the trigger temp. This would help smooth out the sensor readings a little. Aug 10, 2020 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.