Table of Contents
Introduction
This project utilizes a temperature sensor and the TINKERplate to turn on a USB powered fan when the room temperature climbs above 78°F. This project will require access to of the purchase of a short USB 2.0 extender cable and a USB powered desk fan. In addition, the USB cable will require some modifications as described below.
Required Parts
The following parts are required for this project:
- A TINKERplate attached to a preprogrammed Raspberry Pi
- From a TINKERkit
- the mini breadboard
- the DS18B20 temperature sensor module
- male to male jumper wires as required
- A USB extender cable like this one.
- A USB powered fan like the one here.
- Wire strippers to perform cable modifications
Assembly Steps
Modify USB Extender Cable
Using a pair of wire strippers, modify the USB extender cable by following the steps shown below:
Finalize Assembly
Assemble the components as shown below. Take some care when routing the jumper wire and the black wire from the modified USB cable into the GND port on the DIgital I/O block.
Code
Copy and paste the code below to a file called TempFan1.py. The run it by typing python3 TempFan2.py at the command prompt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import piplates.TINKERplate as TINK import time tripTEMP=78 #this is the temperature we will operate around hysteresis=1 #we will add a small amount of hysteresis to avoid chatter threshold=tripTEMP-hysteresis #the on/off threshold is the tripTEMP +/- the hysteresis fanON=True #initialize flag and.. TINK.relayON(0,1) #turn on fan TINK.setDEFAULTS(0) #set all Digital I/O ports to their default states TINK.setMODE(0,1,'temp') #set the mode of Digital I/O port 1 to temperature while(True): #start loop temp=TINK.getTEMP(0,1) #collect temperature data if (fanON): if (temp<threshold): #if on and temp is below threshold: fanON=False #clear state TINK.relayOFF(0,1) #turn off fan threshold=tripTEMP+hysteresis #set high threshold else: if (temp>threshold): #if off and temp is above threshold: fanON=True #set state true TINK.relayON(0,1) #turn on fan threshold=tripTEMP-hysteresis #set low threshold print ("Temperature:",temp,", Fan State:",fanON) time.sleep(1) #sleep 1 sec then |
Our program uses a technique called hysteresis to prevent the fan from quickly turning off and on around the trip temperature - a phenomena we refer to as "chatter." The thermostat in your home does this also. For more information about hysteresis, refer to the Wikipedia article here.
Note that the above program can be downloaded from github by typing the following instructions on the command line:
- git clone https://github.com/pi-plates/TINKERplate-Projects.git
- cd TINKERplate-Projects
- python3 TempFan1.py
A slight variation of the above program uses the METER library to display the temperature on the screen. When the fan is off, the temperature text is blue to reflect the cooler temperature. When the fan turns on the color of the text is set to red to indicate warmer temperatures.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import piplates.TINKERplate as TINK import time d = '\u00b0' #create the degree symbol tripTEMP=78 #this is the temperature we will operate around hysteresis=1 #we will add a small amount of hysteresis to avoid chatter threshold=tripTEMP-hysteresis #the on/off threshold is the tripTEMP +/- the hysteresis fanON=True #initialize flag and.. TINK.relayON(0,1) #turn on fan TINK.setDEFAULTS(0) #set all Digital I/O ports to their default states TINK.setMODE(0,1,'temp') #set the mode of Digital I/O port 1 to temperature TINK.openMETER(1) #Create a display meter on the screen TINK.setTITLE('TINKERplate Demo') TINK.setCOLOR((255,0,0)) #Set meter text color to RED to indicate warm range while(True): #start loop temp=TINK.getTEMP(0,1) #collect temperature data TINK.setMETER(temp,d+'F','Temperature:') if (fanON): if (temp<threshold): #if on and temp is below threshold: fanON=False #clear state TINK.relayOFF(0,1) #turn off fan threshold=tripTEMP+hysteresis #set high threshold TINK.setCOLOR((0,0,255)) #Set meter text color to BLUE to indicate cool range print ("Temperature:",temp,", Fan Off") #indicate the state change else: if (temp>threshold): #if off and temp is above threshold: fanON=True #set state true TINK.relayON(0,1) #turn on fan threshold=tripTEMP-hysteresis #set low threshold fanState='ON' TINK.setCOLOR((255,0,0)) #Set meter text color to RED to indicate warm range print ("Temperature:",temp,", Fan ON") #indicate the state change TINK.setMETER(temp,d+'F','Temperature:',1) time.sleep(1) #sleep 1 sec then |
Again the above program can be downloaded from github by typing the following instructions on the command line:
- git clone https://github.com/pi-plates/TINKERplate-Projects.git
- cd TINKERplate-Projects
- python3 TempFan2.py