Python Program 3: Interfacing DHT11 Sensor with Raspberry Pi

OBJECTIVE:

To interface the DHT11 sensor with the Raspberry Pi and write a Python program to print the temperature and humidity readings in real-time.


RESOURCES REQUIRED:

  • Raspberry Pi (any model with GPIO support)

  • DHT11 Sensor

  • Breadboard

  • Jumper wires

  • Python Libraries: adafruit_dht, psutil


THEORY:

DHT11 Sensor Basics:

  • The DHT11 sensor is a low-cost digital sensor used for measuring temperature and humidity.

  • It provides readings for relative humidity (range: 20% to 90% RH) and temperature (range: 0°C to 50°C).

  • The sensor communicates with the Raspberry Pi through serial communication (one-wire protocol).

  • The DHT11 sensor has 4 pins:

    • VCC (power)

    • GND (ground)

    • Data (for data communication)

    • NC (not connected, not used)

Data from the sensor is sent in a digital format where the sensor sends pulses of different durations to represent 1s and 0s. These pulses are decoded by the Raspberry Pi to retrieve temperature and humidity readings.


Circuit Diagram:

  • VCC of DHT11 connects to +5V on Raspberry Pi (or 3.3V, depending on model).

  • GND of DHT11 connects to Raspberry Pi GND.

  • DATA pin of DHT11 connects to GPIO4 (pin 7 on Raspberry Pi) for data communication.

  • NC is unused in the DHT11.


Python Code: Read and Print Temperature and Humidity

import time import board import adafruit_dht import psutil # Kill any previous process that might block GPIO access for proc in psutil.process_iter(): if proc.name() == 'libgpiod_pulsein' or proc.name() == 'libgpiod_pulsei': proc.kill() # Setup DHT11 sensor on GPIO4 (D4) sensor = adafruit_dht.DHT11(board.D4) # Main loop to read temperature and humidity while True: try: # Reading temperature and humidity temp = sensor.temperature humidity = sensor.humidity # Print the results print("Temperature: {}°C Humidity: {}%".format(temp, humidity)) except RuntimeError as error: print(error.args[0]) # Print errors if any time.sleep(2.0) # Wait before trying again continue except Exception as error: sensor.exit() # Clean up sensor if there is any issue raise error time.sleep(2.0) # Delay before the next reading


Explanation of the Program:

Import Libraries:

  • time → Provides sleep() function for pauses between readings.
  • board → Defines the GPIO pin assignments.
  • adafruit_dht → Provides the library to interact with DHT11 sensor.
  • psutil → Monitors and terminates processes that might block GPIO access (specifically, the libgpiod_pulsein or libgpiod_pulsei process).

Sensor Setup:

  • The DHT11 sensor is initialized with adafruit_dht.DHT11(board.D4) — using GPIO4 (D4) as the data input pin.

Reading Temperature and Humidity:

  • sensor.temperature reads the temperature in °C.
  • sensor.humidity reads the relative humidity in %.

Error Handling:

  • The program attempts to fetch the sensor readings in a try block. If an error occurs (like a communication issue with the sensor), a RuntimeError is caught and printed.

  • If a more serious exception occurs (e.g., a hardware problem), the program exits cleanly, resetting the sensor with sensor.exit().

Delay:

  • After each reading, time.sleep(2.0) is used to introduce a 2-second delay before the next reading to avoid overload and allow the sensor to stabilize.


👉 If you found this helpful, don’t forget to like, follow, comment, and share!

Let’s continue learning and building cool sensor projects. Team up with me and let’s complete this together! 💻✨

Comments

Popular posts from this blog

Fundamental of python : 1.Python Numbers