Arduino - Control Temperature

In this tutorial, we are going to control the room temperature to a comfortible range using a Arduino, DS18B20 temperature sensor, fan and heating elemen. When the temperature is too hot, turn on the cooling fan to ventilate. When the temperature is cold, turn off the cooling fan and turn on the heating element.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×12V DC Cooling Fan
1×(Alternative) 5V DC Cooling Fan
1×12V Heating Element
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic 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)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

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 Room Temperature Control System

In short, Arduino will turn on the fan when the room is hot and turn on the heating element when the room is cold. The below is the detail of how a room temperature control system works by taking an example of controling the room temperature from 18°C to 26°C:

  • Arduino reads the temperature from the temperature sensor
  • If the temperature exceeds 26°C, Arduino turns on the fan and turns off the heating element
  • If the temperature falls below 18°C, Arduino turns off the fan and turns on the heating element

The above process is repeated infinitely in the loop.

The fan works as a ventilator.

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

Wiring Diagram

Arduino controls temperature wiring diagram

This image is created using Fritzing. Click to enlarge image

Please note that, for the sake of simplicity, the above diagram shows two 12V DC power adapters, but in practice, you can use a single 12V DC power adapter for both the fan and heating element.

Arduino Code

/* * Created by * * This example code is in the public domain * * Tutorial page: */ #include <OneWire.h> #include <DallasTemperature.h> const int TEMP_THRESHOLD_UPPER = 26; // °C, upper threshold of temperature, change to your desire value const int TEMP_THRESHOLD_LOWER = 18; // °C, lower threshold of temperature, change to your desire value const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DQ pin const int RELAY_FAN_PIN = A5; // Arduino pin connected to relay which connected to fan const int RELAY_HEAT_PIN = A4; // Arduino pin connected to relay which connected to the heating element OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // initialize serial sensors.begin(); // initialize the sensor pinMode(RELAY_FAN_PIN, OUTPUT); // initialize digital pin as an output } void loop() { sensors.requestTemperatures(); // send the command to get temperatures temperature = sensors.getTempCByIndex(0); // read temperature in Celsius if(temperature > TEMP_THRESHOLD_UPPER){ Serial.println("HOT! The fan is turned on and the heating element is turned off"); digitalWrite(RELAY_FAN_PIN, HIGH); // turn on the fan digitalWrite(RELAY_HEAT_PIN, LOW); // turn off the heating element } else if(temperature < TEMP_THRESHOLD_LOWER){ Serial.println("COLD! The fan is turned off and the heating element is turned on"); digitalWrite(RELAY_FAN_PIN, LOW); // turn off the fan digitalWrite(RELAY_HEAT_PIN, HIGH); // turn on the heating element } delay(500); }

In the above code, the Arduino turn on the fan when the temperature exceeds 25°C, and keep the fan on until the temperature is below 20°C

Quick Steps

  • 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
  • Make enviroment around sensor hotter or colder
  • See the state of fan and heating element

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit