So the DHT11 is the sensor you could use. That's it:
It has 3 metal pins. VCC, signal and GND. VCC and GND pins are used solely for powering the sensor. Signal pin is used to actually receive information about temperature from it.
Here's a pinout of the raspberry pi:
So the 3 metal pins from the sensor should be connected to the appropriate metal pins on the raspberry pi.
VCC of the sensor -> any of the 3.3V or 5V pins of Raspberry Pi
GND of the sensor -> any of the GND pins of RPi
Signal pin of the sensor -> GPIO 14 of RPi
After connecting it this way you can get the temperature from the sensor by manipulating pin 14 of the Raspberry Pi which is done by the library I mentioned. In this
example code it is shown how it can be done:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import RPi.GPIO as GPIO
import dht11
import time
import datetime
GPIO.setwarnings( False )
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
instance = dht11.DHT11(pin = 14 )
while True :
result = instance.read()
if result.is_valid():
print ( "Last valid input: " + str (datetime.datetime.now()))
print ( "Temperature: %d C" % result.temperature)
print ( "Humidity: %d %%" % result.humidity)
time.sleep( 1 )
|
Notice that it imports
dht11
, so you need to download and put the
dht11.py in the same folder as your main python file will be or put it in the python/Lib/site-packages/ folder.
Here's example of what your code could look like (I didn't test it though because I don't have that sensor).
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 |
import RPi.GPIO as GPIO
import dht11
import time
GPIO.setwarnings( False )
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
instance = dht11.DHT11(pin = 14 )
relay_pin = 4
GPIO.setup(relay_pin, GPIO.OUT)
ON = 1
OFF = 0
def switch_relay(state):
GPIO.output(relay_pin, state)
while True :
result = instance.read()
if result.is_valid():
if result.temperature < = 0 :
switch_relay(ON)
elif result.temperature > = 3 :
switch_relay(OFF)
time.sleep( 1 )
|