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
1×Temperature Sensor DS18B20
1×Relay
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Optional) Screw Terminal Block Shield for Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

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
  • On Arduino IDE, Go to Tools Manage Libraries
Arduino add library
  • Search “OneWire”, then find the OneWire library by Paul Stoffregen
  • Click Install button to install OneWire library.
Arduino onewire library
  • Search “Dallas”, then find the DallasTemperature library by Miles Burton.
  • Click Install button to install DallasTemperature library.
Arduino Dallas Temperature 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