Molemi IoT
  • Space farming using food computers for sustainable farming
  • Introduction to Arduino
    • Introduction to Electronics
    • The Breadboard
  • Controlling LEDs with an Arduino
    • The Code
    • Adding a button to control the LEDs
      • The Code
  • Introducing Sensors
    • The CO2 and air quality sensor
    • The Code
      • Explaining the Code
    • Light Intensity Sensor
  • Introducing the NodeMCU
    • Getting Started
      • A simple test for ESP8266
        • The Code
    • A light bulb switch using NodeMCU and the Blynk app
      • Setup Blynk on your Smartphone
      • The Code
    • Controlling a Centurion gate Motor with NodeMCU a 2-channel relay
      • Installing Blynk in Arduino IDE
    • Read and display the water flow sensor on Blynk
      • Setting up Blynk
      • The Code
        • Explaining the code
    • Read and Display temperature sensor readings with NodeMCU.
      • Installing libraries for DS18B20 temperature sensor.
      • Displaying the sensor readings on the serial port
        • Getting temperature readings from different DS18B20 sensors.
          • The Code
            • Display sensor readings on a web server
              • Build the web server
                • Designing and building the web page
                • The Code
    • Display the DHT11 sensor reading on a web server using NodeMCU.
      • Monitoring Room Temp & Humidity using Blynk
      • Installing DHT library on the ESP8266
        • Installing the Asynchronous Web Server library
          • The Code
          • Designing and building the web page
  • Data Science for Farming
    • Getting Started with Colaboratory
    • Introduction to Python for DS using Colab
  • Machine Learning for Farming
  • Molemi Personal Food Computer
    • Bill of Materials
  • Setting up WaziGate on a Raspberry Pi
  • Setting up DHT11 on Raspberry Pi
  • Using Telegram To Control Outputs
Powered by GitBook
On this page

Was this helpful?

Setting up DHT11 on Raspberry Pi

The following tutorial is meant to setup the DHT11 humidity and temperature sensor to work on the Raspberry Pi 3

PreviousSetting up WaziGate on a Raspberry PiNextUsing Telegram To Control Outputs

Last updated 4 years ago

Was this helpful?

Step 1: The first thing that you will need to do is to wire the circuit for the DHT11 sensor with the Raspberry Pi. The VCC pin of the sensor will be connected to the 5V pin on the Raspberry Pi, GND will be connected to the GND of the Pi, lastly the DATA pin will be connected to GPIO 4 on the Raspberry Pi.

Step 2: The next following task is to install the dependencies. First you will need to install updates and upgrade as follows:

sudo apt-get update && upgrade -y 

You need to install PIP before you can install the Adafruit_DHT module:

sudo apt-get install python3-dev python3-pip
sudo python3 -m pip install --upgrade pip setuptools wheel

sudo pip3 install Adafruit_DHT

The python code will be as follows:

import Adafruit_DHT
import time
 
DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4
 
while True:
    humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
    if humidity is not None and temperature is not None:
        print("Temp={0:0.1f}C Humidity={1:0.1f}%".format(temperature, humidity))
    else:
        print("Sensor failure. Check wiring.");
    time.sleep(3);