Project

Raspberry Pi Project: Control a DC Fan

October 06, 2015 by Travis Fagerness

Control a fan based on the current temperature with a Raspberry Pi!

Control a fan based on the current temperature with a Raspberry Pi!

Requirements

  • Raspberry Pi
    • Used in article: Model B Revision 1.0 with Raspbian (Debian GNU/Linux 7.6 (wheezy))
  • I2C temperature sensor
  • Method to connect Raspberry Pi to internet
    • Use in article: Raspberry Pi wired directly to router
  • Google account to create and access sheets
  • NMOS or NPN transistor capable of handling the voltage and current requirements of the fan
  • Schottky diode
  • Small DC fan
    • Used in article: 12V/600mA fan

Set Up the Hardware

Temp sensor

Follow the instructions in this article to connect a temperature sensor to the Raspberry Pi.

Fan

Follow the wiring diagram below to connect the Raspberry Pi to the temp sensor and to the fan through the transistor. The diode is there to prevent the fan from damaging the transistor due to potential voltage flyback when turning the fan off. A diode in this configuration is sometimes called a snubber. You have to cut the ground wire feeding the fan in order to put the transistor in series. The transistor is acting as a low-side switch in this configuration.

The maximum current of the Pi GPIO output is 16mA. This means the transistor must have a high enough hFE to conduct the current needed to run the fan. Size the base resistor to limit the amount of current the Pi delivers to the transistor to avoid damage. I used 180 ohms which would give a base current of approximately (3.3-0.7)/180 = 14.4mA. The transistor I selected has an hFE of 150 when conducting 2A, so switching a 600mA load is no problem.

 

Software

I2C

Follow the article here to setup I2C to communicate to the temp sensor.

Installing GPIO capability

Type the following in the Pi terminal to install GPIO capability: sudo apt-get install python-rpi.gpio

Testing Connections

Upload the following Python program to the Raspberry Pi in order to test the fan control from the GPIO. Run the script "turn_fan_on.py" or "turn_fan_off.py" in order to verify the connections.

turn_fan_on.py
import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
FAN_PIN = 23
GPIO.setup(FAN_PIN, GPIO.OUT)
GPIO.output(FAN_PIN, True)

turn_fan_off.py
import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
FAN_PIN = 23
GPIO.setup(FAN_PIN, GPIO.OUT)
GPIO.output(FAN_PIN, False)

Controlling the fan based on temperature

The following script implements some logic that turns on the fan when the temperature has risen above TEMP_THRESHOLD. The fan will stay on until the temperature drops below the threshold - TEMP_HYST. This way the fan doesn't rapidly turn on and off when the room is around the temperature threshold.

import RPi.GPIO as GPIO
import smbus
import time
#0 = /dev/i2c-0
#1 = /dev/i2c-1
I2C_BUS = 0
bus = smbus.SMBus(I2C_BUS)
    
#7 bit address (will be left shifted to add the read write bit)
DEVICE_ADDRESS = 0x48      
TEMP_THRESHOLD = 78
TEMP_HYST = 2

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
FAN_PIN = 23
GPIO.setup(FAN_PIN, GPIO.OUT)

while True:
	time.sleep(1)
	#Read the temp register
	temp_reg_12bit = bus.read_word_data(DEVICE_ADDRESS , 0 )
	temp_low = (temp_reg_12bit & 0xff00) >> 8
	temp_high = (temp_reg_12bit & 0x00ff)
	#convert to temp from page 6 of datasheet
	temp  = ((( temp_high * 256 ) + temp_low) >> 4 )
	#handle negative temps
	if temp > 0x7FF:
		temp = temp-4096;
	temp_C = float(temp) * 0.0625
	temp_F = temp_C * 9/5+32
	print "Temp = %3.1f C -- %3.1f F" % (temp_C,temp_F)

	#control the fan based on the temp
	if(temp_F > TEMP_THRESHOLD):
		GPIO.output(FAN_PIN, True)
	if(temp_F < (TEMP_THRESHOLD - TEMP_HYST)):
		GPIO.output(FAN_PIN, False)

In the video below, I'm using ice cubes in a plastic bag to simulate the room cooling down. When the ice cubes are applied to the temp sensor, the fan turns off. When I remove them, the temperature rises and the fan turns on.

Give this project a try for yourself! Get the BOM.

3 Comments
  • D
    d0ughb0y October 06, 2015

    I built a fan controller using arduino mega. It controls 3 fans via pwm that ramps up fan speed based on temperature and has real time rpm reading. Fan speed can also be controlled manually via web interface.
    https://youtu.be/DwWnl3bREww

    Like. Reply
  • Y
    Yadanar Jewel October 22, 2015

    Does the fan work controlled by temperature sensor without using respberry pi and auduino?

    Like. Reply
  • Dominic Jackson November 10, 2018

    Can I easily adapt your instructions to control two fans one an intake fan and on an out take fan for some vegetables I am growing?

    Like. Reply