Arduino - Heating System

In this tutorial, we are going to control room temperature using a heating element and DS18B20 temperature sensor. When the temperature is too cool, turn on the heating element. When the temperature is warm, turn off the heating element. You can also adapt the code for other temperature sensor such as DHT11 or DHT22, LM35 instead of the DS18B20 sensor.

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×4.7 kΩ resistor
1×Relay
1×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 Heating Element and DS18B20 Temperature Sensor

The heating element used in this tutorial uses the 12v power supply. If power is supplied for the heating element, the heating element emits heat. To control heating element by Arduino, we need to use a relay in between.

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

Wiring Diagram

  • Wiring diagram with breadboard
Arduino heating system wiring diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram with adapter (recommended)
Arduino teamperature controls heating system wiring diagram

This image is created using Fritzing. Click to enlarge image

How System Works

  • Arduino reads the temperature from the temperature sensor
  • If the temperature falls below an lower threshold, Arduino turn on the heating elements
  • If the temperature rises above a upper threshold, Arduino turn off the heating element

The above process is repeated infinitely in the loop.

Arduino Code for Cooling System with DS18B20 sensor

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-heating-system */ #include <OneWire.h> #include <DallasTemperature.h> #define SENSOR_PIN 2 // Arduino pin connected to DS18B20 sensor's DQ pin #define RELAY_PIN A5 // Arduino pin connected to relay which connected to heating element const int TEMP_THRESHOLD_UPPER = 20; // upper threshold of temperature, change to your desire value const int TEMP_THRESHOLD_LOWER = 15; // lower threshold of temperature, change to your desire value 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_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("The heating element is turned off"); digitalWrite(RELAY_PIN, LOW); // turn off } else if(temperature < TEMP_THRESHOLD_LOWER){ Serial.println("The heating element is turned on"); digitalWrite(RELAY_PIN, HIGH); // turn on } delay(500); }

In the above code, the Arduino turn on the heating element when the temperature falls below 15°C, and keep the heating element on until the temperature is above 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
  • Check the temperature of heating element and your room

Advanced Knowledge

The above controlling method is the on-off controller, also known as a signaller or "bang-bang" controller. This method is very simple to implement.

There is an alternative method called the PID controller. With the PID controller, the desired temperature is more stable but very difficult to understand and implement. Therefore, the PID controller is not popular in temperature control.

The Best Arduino Starter Kit

※ OUR MESSAGES