Credit to this website: https://www.hackster.io/adamgarbo/raspberry-pi-2-iot-thingspeak-dht22-sensor-b208f4
In this page, I will show you on how to program the raspberry pi to read temperature and humidity from DHT22 sensor. The DHT22 sensor is based on a capacitive humidity sensor and a thermistor to measure the surrounding air and output a digital signal on the data pin.
Step 1: Wiring Diagram
The sensor has 3 pins which is:
Pin 1 to be connected to a 3.3V source,
Pin 2 to the desired General-purpose input/output (GPIO) pin on the RPi.
Pin 3 in not used.
Pin 4 to ground (GND). A 10kΩ resistor is placed between Pin 1 and Pin 2.
Figure 1: DHT22 pins
Figure 2: Wiring the DHT22 to raspberry pi
Step 2: Download & Install the Adafruit DHT Library
After the wiring complete, we have to download and install the Adafruit’s DHT library to the RPi. Run this commands on terminal:
sudo apt-get install python-dev
sudo apt-get install python-pip
sudo apt-get install git
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python3 setup.py install
Step 3: Script Creation
Create an empty Python script for example DHT22_test.py:
sudo nano DHT22_test.py
Next, write the code listed below:
#——————————————————————————————————————————-#
from time import sleep from gpiozero import LED import Adafruit_DHT led = LED(17) led.off() DHT_pin = 23 # the sensor is connected in GPIO23 while True: humidity,temp = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22,DHT_pin) print("Temperature = %0.1f Humidity = %0.1f" %(temp,humidity)) sleep(0.5)
#——————————————————————————————————————————-#
Step 4: Save and Run
Done