Arduino - Temperature Sensor - Relay

This tutorial presents how to use an Arduino and a DS18B20 temperature sensor to control a relay. Specifically, the Arduino is programmed to continually measure the temperature using the DS18B20 sensor. The relay is then adjusted according to the following rules:

The relay then can connect to fan, heating element, cooling element or other things.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Relay
1×DC Power Jack
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

Buy Note: Many DS18B20 sensors on the market are low-quality. We highly recommend buying the sensor from the DIYables brand using the link above. We tested it, and it worked well.

About Relay and Temperature sensor

If you do not know about relay and temperature sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

  • Wiring diagram with breadboard
arduino temperature sensor relay wiring diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram with adapter (recommended)
arduino DS18B20 temperature sensor relay wiring diagram

This image is created using Fritzing. Click to enlarge image

We suggest purchasing a DS18B20 sensor that comes with a wiring adapter for easy connection. The adapter has a built-in resistor, eliminating the need for a separate one in the wiring.

Arduino Code - Temperature Sensor Triggers Relay

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-temperature-sensor-relay */ #include <OneWire.h> #include <DallasTemperature.h> const int RELAY_PIN = A5; // Arduino pin connected to the relay's pin const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DATA pin const float TEMPERATURE_THRESHOLD = 20; // °C OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library float temperature; int relay_state; void setup() { Serial.begin(9600); // initialize serial pinMode(RELAY_PIN, OUTPUT); sensor.begin(); // initialize the sensor } void loop() { sensor.requestTemperatures(); // send the command to get temperatures temperature = sensor.getTempCByIndex(0); // read temperature in Celsius if (temperature > TEMPERATURE_THRESHOLD) relay_state = HIGH; // set relay_state to HIGH else relay_state = LOW; // set relay_state to LOW digitalWrite(RELAY_PIN, relay_state); // control the relay // print to serial Serial.print("Temperature: "); Serial.print(temperature); Serial.print("°C => relay's state: "); Serial.println(relay_state); }

Quick Steps

  • Do the wiring between Arduino, temperature sensor and relay as above diagram
  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “DallasTemperature”, then find the DallasTemperature library by Miles Burton.
  • Click Install button to install DallasTemperature library.
Arduino Dallas Temperature library
  • You will be asked to install the library dependency
  • Click Install All button to install OneWire library.
Arduino onewire library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Change temperature around the temperature sensor
  • See the change of relay
  • See the result on Serial Monitor, It looks like below:
COM6
Send
Temperature: 19.1°C => relay's state: 0 Temperature: 19.3°C => relay's state: 0 Temperature: 19.7°C => relay's state: 0 Temperature: 19.8°C => relay's state: 0 Temperature: 20.1°C => relay's state: 1 Temperature: 20.6°C => relay's state: 1 Temperature: 21.4°C => relay's state: 1
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Arduino Code - Temperature Sensor Triggers Relay with Tolerance

While in operation, the temperature may experience fluctuations above or below the predetermined threshold, leading to frequent and sudden state changes on the relay, which can cause unforeseen operational issues. To address this problem, it is possible to include a margin of tolerance in the threshold. The code below demonstrates how this solution can be implemented:

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-temperature-sensor-relay */ #include <OneWire.h> #include <DallasTemperature.h> const int RELAY_PIN = A5; // Arduino pin connected to the relay's pin const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DATA pin const float TEMPERATURE_THRESHOLD = 20; // °C const float TOLERANCE = 0.5; // °C OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library float temperature; int relay_state; void setup() { Serial.begin(9600); // initialize serial pinMode(RELAY_PIN, OUTPUT); sensor.begin(); // initialize the sensor } void loop() { sensor.requestTemperatures(); // send the command to get temperatures temperature = sensor.getTempCByIndex(0); // read temperature in Celsius if (temperature > (TEMPERATURE_THRESHOLD + TOLERANCE)) relay_state = HIGH; // set relay_state to HIGH else if (temperature < (TEMPERATURE_THRESHOLD - TOLERANCE)) relay_state = LOW; // set relay_state to LOW digitalWrite(RELAY_PIN, relay_state); // control the relay // print to serial Serial.print("Temperature: "); Serial.print(temperature); Serial.print("°C => relay's state: "); Serial.println(relay_state); }

The code presented above specifies a threshold of 20°C and a tolerance of 0.5°C. The following conditions apply:

  • If the temperature is below 19.5°C, the relay is turned off.
  • If the temperature is above 20.5°C, the relay is turned on.
  • If the temperature is between 19.5°C and 20.5°C, the relay's state is kept unchanged.

The Best Arduino Starter Kit

※ OUR MESSAGES