Lesson 31 – DHT11 Temperature & Humidity Sensor


Tutorial

1. Introduction

This lesson will directly connect DHT11 Temperature and Humidity Sensor to Keyestudio RPI GPIO-PCF8591 Shield to test the temperature and humidity of current environment, and you can see the corresponding value on the terminal of raspberry pi.

2. Hardware Required

  • DHT11 Temperature and Humidity Sensor*1
  • Keyestudio RPI GPIO-PCF8591 Shield*1
  • USB Cable * 1
  • Raspberry Motherboard * 1
  • Dupont Wires* several

3. Connection Diagram

4. Programming

  • a. Use winSCP to put the lesson30_DHT11 into the lesson folder of raspberry system.
    b. In the terminal, input cd lesson30_DHT11 to enter the folder.
    c. In the terminal, execute make to generate DHT11 executable file inside the folder.
    d. Wiring as the above diagram, then enter the lesson30_DHT11 folder, execute sudo ./DHT11 at the terminal. Then, you can see the corresponding temperature and humidity value of current environment on the terminal.
    e. Ctrl + c can exit the processing program.

5. Sample Code

The first four lines below are –

  • #include <wiringPi.h>
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <stdint.h>
 

#include 
#include 
#include 
#include 
#define MAX_TIME 85
#define DHT11PIN 1
#define ATTEMPTS 5                 //retry 5 times when no response
int dht11_val[5]={0,0,0,0,0};
  
int dht11_read_val(){
    uint8_t lststate=HIGH;         //last state
    uint8_t counter=0;
    uint8_t j=0,i;
    for(i=0;i<5;i++)
        dht11_val[i]=0;
         
    //host send start signal    
    pinMode(DHT11PIN,OUTPUT);      //set pin to output 
    digitalWrite(DHT11PIN,LOW);    //set to low at least 18ms 
    delay(18);
    digitalWrite(DHT11PIN,HIGH);   //set to high 20-40us
    delayMicroseconds(40);
     
    //start recieve dht response
    pinMode(DHT11PIN,INPUT);       //set pin to input
    for(i=0;i<MAX_TIME;i++) { counter=0; while(digitalRead(DHT11PIN)==lststate){ //read pin state to see if dht responsed. if dht always high for 255 + 1 times, break this while circle counter++; delayMicroseconds(1); if(counter==255) break; } lststate=digitalRead(DHT11PIN); //read current state and store as last state. if(counter==255) //if dht always high for 255 + 1 times, break this for circle break; // top 3 transistions are ignored, maybe aim to wait for dht finish response signal if((i>=4)&&(i%2==0)){
            dht11_val[j/8]<<=1; //write 1 bit to 0 by moving left (auto add 0) if(counter>16)                          //long mean 1
                dht11_val[j/8]|=1;                  //write 1 bit to 1 
            j++;
        }
    }
    // verify checksum and print the verified data
    if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))){
        printf("RH:%d,TEMP:%d\n",dht11_val[0],dht11_val[2]);
        return 1;
    }
    else
        return 0;
}
  
int main(void){
    int attempts=ATTEMPTS;
    if(wiringPiSetup()==-1)
        exit(1);
    while(attempts){                        //you have 5 times to retry
        int success = dht11_read_val();     //get result including printing out
        if (success) {            //if get result, quit program; if not, retry 5 times then quit
            break;
        }
        attempts--;
        delay(2500);
    }
    return 0;
}

 

Prerequisites

  1. This development track is based on the Rasbperry Pi and the Ultimate Starter Kit for the Raspberry Pi.
  2. You will need access to both the Raspberry Pi SBC and the electronics components part of the Ultimate Starter Kit for the Raspberry Pi kit to be able to work on these tutorials.
  3. If you haven’t purchased the Raspberry Pi 4 B yet please head over to our store and purchase one now. You can pick up the Ultimate Starter Kit for the Raspberry Pi from OztoyLib.
  4. Depending on where you live you might also be able to pick up the Raspberry Pi and Ultimate Starter Kit for the Raspberry Pi at your local electronics hobby store.

About the Ultimate Starter Kit for the Raspberry Pi

The Ultimate Starter Kit for the Raspberry Pi comes packed with ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.) including tutorials to get started. You will learn how to connect up the ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.), create circuits using the Raspberry Pi including learning to write code in C. As you progress through the different tutorials you will explore the different capabilities of the Raspberry Pi including the intricacies of integrating the Raspberry Pi with the different electronic bits i.e. sensors, LED’s, switches, servos, etc. included in the starter kit.

The Ultimate Starter Kit for the Raspberry Pi comes along with detailed tutorials including code samples. Ultimate Starter Kit for the Raspberry Pi walks you through the basics of using the Raspberry Pi in a hands-on way. You’ll learn the fundamentals of electronics, programming in C and gain hands on experience through working on the Raspberry Pi building creative projects. The kit includes a selection of the most common and useful electronic components with an ebook of 37 projects. Starting the basics of electronics, to more complex projects, the kit will get you interacting with the physical world using sensor and actuators. Along with the kit you get access to detailed tutorials and wiring diagrams.


About the Raspberry Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.

The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.

You can read more about the Raspberry Pi here – RaspberryPi.org.

Questions