About:
In this tutorial i will explain how to interface and program DHT11/22 temperature sensor with Raspberry Pi. I have used adafruit python library to program these sensors in python shell.
Things you will need:
Raspberry Pi (any model will work)
DHT11 or DHT22
4k7 Resistor
Circuit Diagram:
Python Code:
#!/usr/bin/python # Name: DHT11/DHT22Interfacing Temperature sensor with raspberry Pi # Author: Pranay Sawarkar # Website: www.etechpath.com # Copyright: Creative Common import Adafruit_DHT # sensor = Adafruit_DHT.DHT22 sensor = Adafruit_DHT.DHT11 # Sensor connected to Raspberry Pi GPIO4. pin = 4 humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print('Temperature={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)) else: print('Failed to capture reading. Please Try again!')
Steps:
- Connect your temperature sensor to GPIO4 of your raspberry pi and don’t forget to connect 4k7 pull up resistor between vcc and data output pin. You can use any 4k7 to 10k range resistor for pull up purpose.
- Power on your raspberry pi and open terminal.
- First we will download and install adafruit library for DHT sensors,
sudo apt-get update git clone https://github.com/adafruit/Adafruit_Python_DHT.git
- Install python essential files. (if you are using it for first time)
sudo apt-get install build-essential python-dev sudo python setup.py install
- Now your raspberry pi is ready to run the adafruit dht python program,
- Copy main python code at home directory in a empty file and name it as dht.py (or download ready dht.py file from download section and copy it to your raspberry pi home directory).
- Now you can use bellow command to check the sensor data by running this python program from your raspbery pi terminal,
sudo python dht.py
You will get output like this,