Raspberry Pi GPIO: DHT-11 Temperature Sensor

raspberry pi logo

Monitoring Temperature and Humidity

In this guide, we’ll dive into the exciting world of sensor technology with the Raspberry Pi GPIO and learn how to use the DHT-11 temperature sensor to measure both temperature and humidity. Whether you’re interested in home automation, weather monitoring, or simply want to explore the capabilities of your Raspberry Pi, this project is an excellent starting point. We’ll cover the essential steps, from wiring the sensor to writing Python code, and show you how to display real-time temperature and humidity data. Let’s get started!

Materials Needed:

Before we begin, gather the following materials:

  • Raspberry Pi (any model with GPIO pins)
  • DHT-11 temperature and humidity sensor
  • Breadboard (optional)
  • Jumper wires
  • Power supply for your Raspberry Pi

DHT-11 Sensor Library

We need to install the DHT-11 sensor library so we can have access to its functions. Open the Raspberry Pi terminal and enter the following command:

pip install dht11 -y

The Circuit:

Follow the circuit diagram below, the connections are as follows:

  • Connect the sensor’s VCC (power) pin to a 3.3V pin on the Raspberry Pi.
  • The sensor’s data pin should be connected to GPIO 17.
  • Connect the sensor’s ground (GND) pin to one of the Raspberry Pi’s ground pins.
RPi DHT11

The Code:

import RPi.GPIO as GPIO
import dht11
import time

GPIO.setmode(GPIO.BCM) #numbering convention

myDHT=dht11.DHT11(pin=17) #GPIO17

try:
    while True:
        result=myDHT.read()
        if result.is_valid():
            print('Temperature is: ',result.temperature, 'Humidity is: ', result.humidity)

            
except KeyboardInterrupt: #ctrl + c always end with this
    GPIO.cleanup()
    print('GPIO Cleaned')
Explanation:
  • Import the dht11 library and set the GPIO pin to which the sensor is connected.
  • Create a loop to continuously read temperature and humidity data.
  • Display the data if it’s successfully retrieved; otherwise, indicate a failure.
  • ctrl + c to quit the program and safely exit the GPIO.

Conclusion

With the DHT-11 sensor and your Raspberry Pi, you can easily monitor temperature and humidity for various applications. You can extend this project by logging the data, creating a web interface, or integrating it into a larger home automation system. Have fun exploring the possibilities of sensor technology with your Raspberry Pi.

Happy Tinkering Folks!

You can explore more of our Raspberry Pi guides here: Raspberry Pi for Beginners

Unlock the Possibilities

We have put together a list of some great Raspberry Pi products and deals: Shop Raspberry Pi Goodies Now! 

Luke Barber

Hello, fellow tech enthusiasts! I'm Luke, a passionate learner and explorer in the vast realms of technology. Welcome to my digital space where I share the insights and adventures gained from my journey into the fascinating worlds of Arduino, Python, Linux, Ethical Hacking, and beyond. Armed with qualifications including CompTIA A+, Sec+, Cisco CCNA, Unix/Linux and Bash Shell Scripting, JavaScript Application Programming, Python Programming and Ethical Hacking, I thrive in the ever-evolving landscape of coding, computers, and networks. As a tech enthusiast, I'm on a mission to simplify the complexities of technology through my blogs, offering a glimpse into the marvels of Arduino, Python, Linux, and Ethical Hacking techniques. Whether you're a fellow coder or a curious mind, I invite you to join me on this journey of continuous learning and discovery.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights