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


  • 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.


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.





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 "" or "" in order to verify the connections.


                    import RPi.GPIO as GPIO

FAN_PIN = 23
GPIO.output(FAN_PIN, True)


                    import RPi.GPIO as GPIO

FAN_PIN = 23
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      

FAN_PIN = 23

while True:
	#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)
		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.




  • d0ughb0y 2015-10-06

    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.

  • Yadanar Jewel 2015-10-22

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

  • Dominic Jackson 2018-11-10

    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?