Arduino - Cooling System using DS18B20 Temperature Sensor
In this tutorial, we are going to control temperature using a fan and DS18B20 temperature sensor. When the temperature is too hot, turn on the cooling fan. When the temperature is cool, turn off the cooling fan. If you want to use DHT11 or DHT22 instead of the DS18B20 sensor, see Arduino - Cooling System using DHT Sensor.
Hardware Required
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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 Cooling Fan and DS18B20 Temperature Sensor
The cooling fan used in this tutorial uses the 12v power supply. If power is supplied for the fan, the fan on and vice verse. To control fan by Arduino, we need to use a relay in between.
If you do not know about temperature sensor and fan (pinout, how it works, how to program ...), learn about them in the following tutorials:
Wiring Diagram
- Wiring diagram with breadboard
This image is created using Fritzing. Click to enlarge image
- Wiring diagram with adapter (recommended)
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.
How System Works
- Arduino reads the temperature from the temperature sensor
- If the temperature exceeds an upper threshold, Arduino turn on the fan
- If the temperature falls below a lower threshold, Arduino turn off the fan
The above process is repeated infinitely in the loop.
If you want to turn on and turn off the fan when the temperature is above and below a specific value respectively, you just need to set the upper threshold and lower threshold to the same value.
Arduino Code for Cooling System with DS18B20 sensor
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.
- You will be asked to install the library dependency
- Click Install All button to install 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
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.
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.