DHT11 sensor with Raspberry Pi Program
Procedure
Hardware Setup
- Connect the VCC pin of the DHT11 sensor to the 5V pin of the Raspberry Pi.
- Connect the GND pin of the DHT11 sensor to the GND pin of the Raspberry Pi.
- Connect the Data Out pin of the DHT11 sensor to GPIO4, which corresponds to Physical Pin 7 on the Raspberry Pi.
- After completing the circuit connection, the user can modify the temperature using a temperature controller and observe the results accordingly.
Software Setup
- Install the required DHT11 library, Adafruit_DHT, on the Raspberry Pi. To do this, download the library from GitHub using the following link:Adafruit DHT11 Library
- The downloaded files will be stored in a directory named Adafruit_Python_DHT. Navigate to this directory using the command:
cd Adafruit_Python_DHT
- To list the contents of this directory, use:
ls
- Locate the setup.py file and install it using the following command:
sudo python setup.py install
Python Code Implementation
The following Python script reads temperature and humidity data from the DHT11 sensor at regular intervals:
import sys
import Adafruit_DHT
import time
while True:
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
print('Temperature: {0:0.1f} °C Humidity: {1:0.1f} %'.format(temperature, humidity))
time.sleep(1)
This script continuously retrieves temperature and humidity values from the sensor, displaying the readings every second. The user can adjust the temperature using a temperature controller and observe the corresponding changes in the output.