- Used in article: Model B Revision 1.0 with Raspbian (Debian GNU/Linux 7.6 (wheezy))
I2C temperature sensor
- Used in article: TMP102
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
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
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.
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)
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)