Arduino Nano - Cooling System using DS18B20 Temperature Sensor

This tutorial instructs you how to use Arduino Nano to control the temperature with the help of a fan and a DS18B20 temperature sensor.

If you would like to use a DHT11 or DHT22 instead of the DS18B20 sensor, please refer to Arduino Nano - Cooling System using DHT 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×12V DC Cooling Fan
1×(Alternative) 5V DC Cooling Fan
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Cooling Fan and DS18B20 Temperature Sensor

The fan in this tutorial requires a 12v power supply. If the power is supplied, the fan will turn on and if not, the fan will be off. To control the fan with Arduino Nano, we must use a relay as an intermediary.

If you are unfamiliar with temperature sensors and fans (including pinouts, how they work, and how to program them), the following tutorials can provide more information:

Wiring Diagram

  • Wiring diagram using a breadboard.
The wiring diagram between Arduino Nano and cooling fan system

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 relay

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

  • The Arduino Nano will take a reading from the temperature sensor.
  • If the reading is higher than the upper threshold, the fan will be activated by the Arduino Nano.
  • If the reading is lower than the lower threshold, the Arduino Nano will switch off the fan.

This loop is repeated without end.

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-cooling-system-using-ds18b20-temperature-sensor */ #include <OneWire.h> #include <DallasTemperature.h> const int THRESHOLD_ON = 25; // upper threshold of temperature, change to your desire value const int THRESHOLD_OFF = 20; // lower threshold of temperature, change to your desire value const int SENSOR_PIN = 2; // The Arduino Nano pin connected to DS18B20 sensor's DQ pin const int FAN_PIN = A5; // The Arduino Nano pin connected to relay which connected to fan OneWire oneWire(SENSOR_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(FAN_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_ON){ Serial.println("The fan is turned on"); digitalWrite(FAN_PIN, HIGH); // turn on } else if(temperature < THRESHOLD_OFF){ Serial.println("The fan is turned off"); digitalWrite(FAN_PIN, LOW); // turn on } delay(500); }

In the code above, when the temperature goes above 25°C, the Arduino Nano will activate the fan. The fan will remain on until the temperature drops below 20°C.

Detailed Instructions

  • Connect Arduino Nano to a computer using a USB cable
  • Launch the Arduino IDE, choose the correct board and port
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Dallas” and locate the DallasTemperature library created by Miles Burton.
  • Then, press the Install button to add it to your project.
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 in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Change the temperature of the sensor's environment by making it hotter or colder.
  • Check the status of the fan on the Serial Monitor.

Advanced Knowledge

This controlling technique is referred to as an on-off controller, which is also known as a signaller or "bang-bang" controller. It is quite easy to implement this method.

An alternative to the traditional temperature control method is the PID controller. This approach provides a more stable desired temperature, however, it is complex and challenging to understand and use. As a result, the PID controller is not widely used for temperature control.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!