Temperature and Humidity sensing with DHT22 or DS18B20


What we are doing today is somewhat different than a led or a switch.  Instead of just turning a GPIO on and off, or reading the state of a GPIO we are going to connect to and communicate with a "smart" device. 

Image result for dht22 pinout
                   
                  DHT22

  • Or

                                   
                                    DS18B20


DHT22

Download the DHT Library for DHT22

Download the code from github
  1. cd #to ensure that you are in the pi home directory
  2. git clone https://github.com/adafruit/Adafruit_Python_DHT.git
  3. cd Adafruit_Python_DHT

Install the Library and Dependancies

  1. sudo apt-get update
  2. sudo apt-get install build-essential python-dev python-openssl
If you see an error that a package is already installed or at the latest version, don't worry you can ignore it and move on.

Build and Install the Library

  1. sudo python setup.py install


Circuit

foo

GPIO pin is pin 16  which is board pin 36

Code for DHT22

#! /usr/bin/python

import RPi.GPIO as GPIO
import time

import Adafruit_DHT

# Set up parameters
cycleTime = 5 # seconds
sensor = 22 # sensor type
pin = 16 # sensor data pin

# Loop forever
while 1:

    #get the humidity and temperature.
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    print temperature, ", ", humidity
    print('Temp={0:0.1f}*C Humidity={1:0.1f}%\n'.format(temperature, humidity))
    time.sleep(cycleTime)


And when the above is run we get:

$ ./temperature.py
21.2999992371 , 53.0999984741
Temp=21.3*C Humidity=53.1%

21.1000003815 , 52.7999992371
Temp=21.1*C Humidity=52.8%

21.1000003815 , 52.9000015259
Temp=21.1*C Humidity=52.9%


DS18B20

Wire it up and Connect it to the Pi

DS18B20 Connections to the pi

See the reference section  below for pictures of the connections










Test that it works

sudo modprobe w1-gpio        # modprobe adds modules to the Linux Kernel
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls

28-051701206aff  w1_bus_master1

cd 28* (This will take you to the 28 (whatever) directory)
cat w1_slave

75 01 4b 46 7f ff 0c 10 16 : crc=16 YES
75 01 4b 46 7f ff 0c 10 16 t=23312




Code for DS18B20

#! /usr/bin/python

import os

import glob
import time
 
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
 
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
 
def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines
 
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        #temp_f = temp_c * 9.0 / 5.0 + 32.0
        #return temp_c, temp_f
    return temp_c
   
while True:
    print(read_temp())   
    time.sleep(1)

Reference

DHT22


DS18B20




BCM GPIO# 2nd func pin#
pin# 2nd func BCM GPIO#
- +3V3 1
2 +5V -
GPIO2 SDA1 (I2C) 3
4 +5V -
GPIO3 SCL1 (I2C) 5
6 GND -
GPIO4 GCLK 7
8 TXD0 (UART) GPIO14
- GND 9
10 RXD0 (UART) GPIO15
GPIO17 GEN0 11
12 GEN1 GPIO18
GPIO27 GEN2 13
14 GND -
GPIO22 GEN3 15
16 GEN4 GPIO23
- +3V3 17
18 GEN5 GPIO24
GPIO10 MOSI (SPI) 19
20 GND -
GPIO9 MISO (SPI) 21
22 GEN6 GPIO25
GPIO11 SCLK (SPI) 23
24 CE0_N (SPI) GPIO8
- GND 25
26 CE1_N (SPI) GPIO7
(Models A and B stop here)
EEPROM ID_SD 27
28 ID_SC EEPROM
GPIO5 - 29
30 GND -
GPIO6 - 31
32 - GPIO12
GPIO13 - 33
34 GND -
GPIO19 - 35
36 - GPIO16
GPIO26 - 37
38 - GPIO20
- GND 39
40 - GPIO21




Homework

1) Turn the LED on above a set temperature, and off below that temperature.

if temperature > 20:
GPIO.output(pin, 1)
else: GPIO.output(pin, 0)


 

2)  Add the push button into your temperature code.  Each push of the button changes the set temperature by +.05 degrees C.  Up to a predetermined temperature.  Then the next push drops the temperature to a low limit.  E.G.  Start at 18 °C and go up in steps to 22°C.