Arduino Nano - Heating System

This tutorial instructs you how to use Arduino Nano to control the temperature of a room using a heating element and a DS18B20 temperature sensor.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
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 Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of Heating Element and DS18B20 Temperature Sensor

The heating element utilized in this tutorial requires a 12v power supply. When power is supplied to the heating element, it will emit heat. In order to control the heating element with Arduino Nano, a relay must be used as an intermediary.

If you are not familiar with temperature sensor and heating element (pinout, functionality, programming ...), the following tutorials can help you:

Wiring Diagram

  • Wiring diagram using a breadboard.
The wiring diagram between Arduino Nano and heating element

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram using a terminal adapter (recommended).
The wiring diagram between Arduino Nano and control heating element

This image is created using Fritzing. Click to enlarge image

We recommend buying a DS18B20 sensor along with its accompanying wiring adapter for a seamless setup. This adapter includes an integrated resistor, removing the need for an additional resistor in the wiring.

How System Works

  • Arduino Nano obtains the temperature from the temperature sensor.
  • If the temperature drops below a certain lower limit, Arduino Nano activates the heating elements.
  • When the temperature exceeds an upper limit, Arduino Nano deactivates the heating element.

The loop is repeated continuously.

Arduino Nano Code for Cooling System with DS18B20 sensor

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-heating-system */ #include <OneWire.h> #include <DallasTemperature.h> #define RELAY_PIN A5 // The Arduino Nano pin connected to the IN pin of relay module #define DS18B20_PIN 2 // The Arduino Nano pin connected to DS18B20 sensor's DQ pin const int THRESHOLD_OFF = 20; // upper threshold of temperature, change to your desire value const int THRESHOLD_ON = 15; // lower threshold of temperature, change to your desire value OneWire oneWire(DS18B20_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. DS18B20.begin(); // initialize the sensor pinMode(RELAY_PIN, OUTPUT); // initialize digital pin as an output } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures temperature = DS18B20.getTempCByIndex(0); // read temperature in Celsius if (temperature > THRESHOLD_OFF) { Serial.println("The heating element is turned off"); digitalWrite(RELAY_PIN, LOW); // turn off } else if (temperature < THRESHOLD_ON) { Serial.println("The heating element is turned on"); digitalWrite(RELAY_PIN, HIGH); // turn on } delay(500); }

In the code mentioned above, the Arduino Nano will activate the heating element when the temperature is less than 15°C and will keep it on until the temperature exceeds 20°C.

Detailed Instructions

  • Connect Arduino Nano to a computer using a USB cable
  • Launch the Arduino IDE, select the correct board and port
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Dallas”, then locate the DallasTemperature library created by Miles Burton.
  • Click the Install button to add the library.
Arduino Nano Dallas Temperature library
  • You will be asked to install the dependency. Click Install All button to install OneWire library.
Arduino Nano onewire library
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Change the temperature of the environment around the sensor.
  • Check out the temperature of the heating element and the temperature of the room.

Advanced Knowledge

The above technique of control is called the on-off controller, which is also known as a signaller or "bang-bang" controller. It is very easy to put into practice.

An alternative approach known as the PID controller exists. This method of temperature control is more stable, however it is complicated to comprehend and put into practice. As a result, the PID controller is not widely used.