Raspberry Pi - Cooling System using DHT Sensor

This tutorial instructs you how to use a Raspberry Pi, a fan and either a DHT11 or DHT22 sensor to regulate temperature.

If you would like to use a DS18B20 sensor instead of a DHT sensor, please refer to Raspberry Pi - Cooling System using DS18B20 Sensor for more information.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×DHT11 Temperature and Humidity Sensor
1×DHT11 Temperature Humidity Sensor Module (short)
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×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

You can use DHT22 sensor instead of DHT11 sensor.

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 DHT Sensor

The fan utilized in this tutorial requires a 12v power supply. If power is supplied, the fan will turn on, and if not, it will remain off. To control the fan with a Raspberry Pi, a relay must be inserted between them.

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

Wiring Diagram

  • The wiring diagram using DHT11
The wiring diagram between Raspberry Pi and DHT11 cooling fan system

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram using DHT22
The wiring diagram between Raspberry Pi and DHT22 cooling fan system

This image is created using Fritzing. Click to enlarge image

How System Works

  • Raspberry Pi obtains the temperature from the temperature sensor.
  • If the temperature surpasses the upper threshold, Raspberry Pi activates the fan.
  • If the temperature drops below the lower threshold, Raspberry Pi deactivates the fan.

The loop is repeated continuously.

If you wish to activate and deactivate the fan when the temperature is above or below a certain point, simply set the upper and lower limits to the same number.

Raspberry Pi Code

Raspberry Pi Code for Cooling System with DHT11 sensor

Let's write a Python code for Raspberry Pi that when the temperature rises above 25°C, the Raspberry Pi will activate the fan. The fan will remain on until the temperature drops to 20°C or lower.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Install the library for DHT11 temperature and humidity sensor by running the following command:
sudo pip3 install Adafruit_DHT
  • Create a Python script file cooling.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-cooling-system-using-dht-sensor """ import Adafruit_DHT import RPi.GPIO as GPIO import time # Constants THRESHOLD_ON = 25 # Upper threshold of temperature, change to your desired value THRESHOLD_OFF = 20 # Lower threshold of temperature, change to your desired value FAN_PIN = 12 # GPIO pin connected to relay, change to your desired GPIO pin DHT11_PIN = 4 # GPIO pin connected to DHT11, change to your desired GPIO pin # Setup GPIO.setmode(GPIO.BCM) GPIO.setup(FAN_PIN, GPIO.OUT) def read_temperature(): try: humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHT11_PIN) return temperature except Exception as e: print(f"Error reading temperature: {e}") return None def control_fan(temperature): if temperature is not None: if temperature > THRESHOLD_ON: print("The fan is turned on") GPIO.output(FAN_PIN, GPIO.HIGH) elif temperature < THRESHOLD_OFF: print("The fan is turned off") GPIO.output(FAN_PIN, GPIO.LOW) try: while True: temperature = read_temperature() control_fan(temperature) time.sleep(0.5) except KeyboardInterrupt: pass finally: GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 cooling.py
  • Change the temperature of the environment around the sensor.
  • Check out the status of the fan on the serial monitor.

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Raspberry Pi Code for Cooling System with DHT22 sensor

Similar to the DHT11, we can create the cooling sytem using DHT22 sensor by using the below Python code for Raspberry Pi

""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-cooling-system-using-dht-sensor """ import Adafruit_DHT import RPi.GPIO as GPIO import time # Constants THRESHOLD_ON = 25 # Upper threshold of temperature, change to your desired value THRESHOLD_OFF = 20 # Lower threshold of temperature, change to your desired value FAN_PIN = 12 # GPIO pin connected to relay, change to your desired GPIO pin DHT22_PIN = 14 # GPIO pin connected to DHT22, change to your desired GPIO pin # Setup GPIO.setmode(GPIO.BCM) GPIO.setup(FAN_PIN, GPIO.OUT) def read_temperature(): try: humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, DHT22_PIN) return temperature except Exception as e: print(f"Error reading temperature: {e}") return None def control_fan(temperature): if temperature is not None: if temperature > THRESHOLD_ON: print("The fan is turned on") GPIO.output(FAN_PIN, GPIO.HIGH) elif temperature < THRESHOLD_OFF: print("The fan is turned off") GPIO.output(FAN_PIN, GPIO.LOW) try: while True: temperature = read_temperature() control_fan(temperature) time.sleep(0.5) except KeyboardInterrupt: pass finally: GPIO.cleanup()

Advanced Knowledge

The above approach to regulation is the on-off controller, also referred to as a signaller or "bang-bang" controller. This technique is easy to put into practice.

An alternative approach known as the PID controller exists. This method is more effective in maintaining a desired temperature, however, it is complex and challenging to comprehend and apply. 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!