Controling a Relay with a temperature probe

Hi to every one,

I am very amateur in Arduino Programming,

I have an Arduino uno, a relay and a DS18B20 sensor probe.

Let’s define:
Temperature as T
Relay as R.

For T>=27 R=ON (the digital output will be HIGH) //In that moment a cooling system will cool the probe

While T>=25 the Relay will be ON (HIGH)

For T<25 R=OFF (the digital output will be LOW) //In that moment the cooling system will stop

While T<27 the Relay will be OFF (LOW)

Summing, for temperatures higher or equal to 27 I want the cooling system to cool the probe until to reach 25 Celsius. When the temperature is below 25 I want to stop and start again for temperatures >=27 Celsius.

P.S. I have already the code for sensor to read the temperature and I miss only this part.

Thanks in advance


What you're looking for is called hysteresis and can be implemented very simply:

if (T >= 27)
  // Turn R on
else if (T < 25)
  // Turn R off

WORKS GREAAAAT!!! THANK YOU A LOT!!! :slight_smile:

Define your control in terms of a setpoint and swing. It will make it easier to tweak later on if you need to.

int setpoint = 26;
int swing = 1;

if (T >= set point + swing) {
  // R on
else if (T < setpoint - swing) {
  // R off

I Understood, thank you a lot!!