Maker.io main logo

Control a DC Fan with a Raspberry Pi

2017-03-02 | By All About Circuits

License: See Original Project Raspberry Pi

Courtesy of All About Circuits

Learn how to use a Raspberry Pi to control a fan based on the current temperature!

Required Parts and Software

  • Raspberry Pi Model B Revision 1.0 with Raspbian (Debian GNU/Linux 7.6 (wheezy))
  • TMP102 I2C temperature sensor
  • Method to connect Raspberry Pi to internet
    • Used in this project: 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 this project: 12V/600mA fan

Setting Up the Hardware

Temperature sensor

In order to connect the temperature sensor to the Raspberry Pi, follow the instructions from this project.

Fan

Use the wiring diagram below to connect your Raspberry Pi to the temperature sensor and to the fan through the transistor. The diode is used to prevent the fan from damaging the transistor from potential voltage flyback when turning the fan off. Diodes in this configuration are sometimes called snubbers. You will need to cut the ground wire feeding the fan in order to put the transistor in series. In this configuration, the transistor is acting as a low-side switch.

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

 

 Software

I2C

To setup I2C to communicate to the temp sensor, follow this project.

Installing GPIO capability

To install GPIO capability, type the following into the Pi terminal: sudo apt-get install python-rpi.gpio

Testing Connections

In order to test the fan control from the GPIOUpload, enter the following Python program to the Raspberry Pi. in order to verify the connections, run this script: "turn_fan_on.py" or "turn_fan_off.py".

turn_fan_on.py 

Copy Code
                  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

Copy Code
                  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

This script shown below implements some logic that turns on the fan when the temperature rises above the TEMP_THRESHOLD. This will keep the fan on until the temperature drops down below the threshold - TEMP_HYST. This way, the fan won’t turn on and off rapidly when the room is around the temperature threshold.

Copy Code
                  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 use ice cubes in a plastic bag in order to simulate the room cooling down. When the ice cubes are applied to the temperature sensor, the fan turns off. The temperature rises and the fan turns on when I remove them.

 

 

制造商零件编号 2N3904BU
TRANS NPN 40V 0.2A TO92-3
onsemi
制造商零件编号 1N5817
DIODE SCHOTTKY 20V 1A DO41
onsemi
制造商零件编号 SEN-13314
TMP102 DIGITAL TEMP SENSOR BOARD
SparkFun Electronics
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum