1uke_
Posts: 68
Joined: Thu Jul 07, 2016 9:19 pm

Looking for help with DHT11 humidity and temperature sensor

Wed Apr 03, 2019 7:34 pm

Hello,

Short version

Can anyone help me to get the DHT11 Sensor running on my Raspbery Pi Model B Rev 2

Longer version

I'd like to be able to use the DHT11 sensor with my Raspberry Pi.

Model of the Pi I am working with

It's the Pi with the blue and yellow outputs, on the PCB it says "Raspberry Pi (c)2011.12"

And when I run $ cat /cat /proc/device-tree/model

It tells me it is a "Raspberry Pi Model B Rev 2"

I believe this model only has 26 GPIO pins as shown in this picture - https://www.raspberrypi-spy.co.uk/wp-co ... sion-2.png

Tutorial I am following

I am following this tutorial on YouTube - https://www.youtube.com/watch?v=KUr8WgSIsfk

The tutorial recommends using this code from GitHub (click through to see code) - https://github.com/szazo/DHT11_Python

I have connected the DHT11 sensor to the RPi and have set up the code, but when I run the code, the command line doesn't return any value, but it also doesn't return an error.

Request for help

Can you help me by telling me
  • What Pins do I attach the sensor to?
  • Do I need to add anything extra to the code?

pcmanbob
Posts: 6224
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Looking for help with DHT11 humidity and temperature sensor

Wed Apr 03, 2019 7:47 pm

There are lots of tutorials on using the DHT11 on a raspberry pi on the web, why not try one of those ?

example
https://www.raspberrypi-spy.co.uk/2017/ ... pberry-pi/

or even the tutorial that your video link seems to have come from
https://www.instructables.com/id/DHT11-Raspberry-Pi/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

1uke_
Posts: 68
Joined: Thu Jul 07, 2016 9:19 pm

Re: Looking for help with DHT11 humidity and temperature sensor

Thu Apr 04, 2019 3:25 pm

pcmanbob wrote:
Wed Apr 03, 2019 7:47 pm
There are lots of tutorials on using the DHT11 on a raspberry pi on the web, why not try one of those ?

example
https://www.raspberrypi-spy.co.uk/2017/ ... pberry-pi/

or even the tutorial that your video link seems to have come from
https://www.instructables.com/id/DHT11-Raspberry-Pi/
Hi @pcmanbob

I've re followed this tutorial that you recommend - https://www.instructables.com/id/DHT11-Raspberry-Pi/

Here is how I wired it - https://www.piddlerintheroot.com/wp-con ... DHT111.png

However when I run python dht11_example.py I am still not getting any data displayed in to the terminal.

Again I'm not sure if it is because of the model of Pi I am using.

I am thinking perhaps the pins don't match the tutorial ..........

Can you advise me further please?

pcmanbob
Posts: 6224
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Looking for help with DHT11 humidity and temperature sensor

Thu Apr 04, 2019 4:59 pm

I suggest the first thing you do is post a picture showing the sensor and the wiring to the pi so we can see how you have it wired.

Secondly is your sensor mounted on a PCB with a resistor or are you using an unmounted version ?
Last edited by pcmanbob on Sat Apr 13, 2019 1:34 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pi9
Posts: 104
Joined: Wed Jun 10, 2015 3:32 pm

Re: Looking for help with DHT11 humidity and temperature sensor

Fri Apr 05, 2019 1:31 am

I have a few of these sensors and other types of sensors (door contacts, motion detector, photoresistors) around the house. They are controlled by a nodemcu module (a little board like arduino). The nodemcu connects to my wifi, collects temperature at regular intervals and sends it wirelessly by calling a php page running on the raspberry pi. I use mysql for the database. It is fun to do and flexible because you don't have to put your sensors next to the pi or run wires. Nodemcu boards are 3 or 4 bucks or so, but you will need a breadboard, some wires, power adapter, resistors, etc
With the data in mysql, you create graphs, stats, etc.

Just something to think about.

knute
Posts: 423
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Looking for help with DHT11 humidity and temperature sensor

Fri Apr 05, 2019 1:58 am

The simplest way is to use the dht11 overlay. You just read the data out of a file. It works fine.

1uke_
Posts: 68
Joined: Thu Jul 07, 2016 9:19 pm

Re: Looking for help with DHT11 humidity and temperature sensor

Sat Apr 13, 2019 12:53 pm

pcmanbob wrote:
Thu Apr 04, 2019 4:59 pm
I suggest the first thing you do is post a picture showing the sensor and the WiFi g to the pi so we can see how you have it wired.

Secondly is your sensor mounted on a PCB with a resistor or are you using an unmounted version ?
Thanks for the reply @pcmanbob, hope you can still help me.

Here is a link to a picture of how the pins are connected - https://i.postimg.cc/Dyk7R81B/rasp-pins.jpg

Hope you can reply back to me and let me know what I need to edit in the code or try next.

Thank you in advance :)

1uke_
Posts: 68
Joined: Thu Jul 07, 2016 9:19 pm

Re: Looking for help with DHT11 humidity and temperature sensor

Sat Apr 13, 2019 12:56 pm

pi9 wrote:
Fri Apr 05, 2019 1:31 am
I have a few of these sensors and other types of sensors (door contacts, motion detector, photoresistors) around the house. They are controlled by a nodemcu module (a little board like arduino). The nodemcu connects to my wifi, collects temperature at regular intervals and sends it wirelessly by calling a php page running on the raspberry pi. I use mysql for the database. It is fun to do and flexible because you don't have to put your sensors next to the pi or run wires. Nodemcu boards are 3 or 4 bucks or so, but you will need a breadboard, some wires, power adapter, resistors, etc
With the data in mysql, you create graphs, stats, etc.

Just something to think about.
Hi Pi9

Could you please link me to the correct board on ebay?

I think calling php and using my sql might be a little beyond my skills at the moment, although you set up does sound great and would be something that I'd like to have running in my own home.

Where did you learn to configure the nodemcu and the sensors with the PHP and mysql?

Do you think this would be hard for a new / novice user?

1uke_
Posts: 68
Joined: Thu Jul 07, 2016 9:19 pm

Re: Looking for help with DHT11 humidity and temperature sensor

Sat Apr 13, 2019 12:57 pm

knute wrote:
Fri Apr 05, 2019 1:58 am
The simplest way is to use the dht11 overlay. You just read the data out of a file. It works fine.
Thanks for the reply knute,

Do you have some instructions you could link me to for using the dht11 overlay?

Thank you :)

pcmanbob
Posts: 6224
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Looking for help with DHT11 humidity and temperature sensor

Sat Apr 13, 2019 1:41 pm

You need to post pictures showing the wiring at both ends no just pi end, and you need to tell use which version of the sensor you are using ,
I.E. sensor on pcb with resistor or just sensor with external resistor on breadboard.

I think this may be the instructions knute was suggesting you try.

viewtopic.php?t=222222
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

knute
Posts: 423
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Looking for help with DHT11 humidity and temperature sensor

Sat Apr 13, 2019 5:03 pm

1uke_ wrote:
Sat Apr 13, 2019 12:57 pm
knute wrote:
Fri Apr 05, 2019 1:58 am
The simplest way is to use the dht11 overlay. You just read the data out of a file. It works fine.
Thanks for the reply knute,

Do you have some instructions you could link me to for using the dht11 overlay?

Thank you :)
Turn off your pi and disconnect the power.

Connect the + lead of your DHT sensor to the 3.3V pin, connect the - lead to a GND pin and the signal/out/data pin to GPIO 4 (you can use others but to keep this simple use GPIO 4 for now). If you are using the sensor on the little PC board you don't need a pull up resistor. If you are using the bare sensor you need a 5.1K ohm resistor between the + and the signal wire. A 4.7K to a 10K will probably work just fine. This overlay works with the DHT11, DHT22 and AM2302 sensors and probably some other similar ones.

Power up your PI

Edit the /boot/config.txt file and add this line:

dtoverlay=dht11

Reboot your Pi

The temperature will be found in the file:

/sys/bus/iio/devices/iio:device0/in_temp_input

and the humidity will be in the file:

/sys/bus/iio/devices/iio:device0/in_humidityrelative_input

There is one caveat with this technique. When you read the temperature file you will occasionally get an error/exception. I think this is because the overlay software is writing to the file but that is only a guess on my part. So you should wrap your reading code in a loop to make sure you successfully read the data. I normally read the temperature first and so if that is successful I rarely get an error reading the humidity but I assume it is possible.

The data you read from the file(s) is a 5 character string that needs to be converted to a decimal value. Convert both strings to a floating point type and divide both the temperature and humidity by 1000.0 to get the actual values. Temperature is in degrees Celsius. If you prefer Kelvin you will have to make the appropriate adjustment.

Even after accounting for the occasional error reading the files I have seen some bad data show up but only every few days of reading the files every few minutes.

I'm not a python guy but here are a Java program and a C program you can use to see how I read the data.

Code: Select all

import java.nio.file.*;

public class dht11 {
    public static void main(String... args) {
        String dir = "/sys/bus/iio/devices/iio:device0";
        String temp = "in_temp_input";
        String hum = "in_humidityrelative_input";
        boolean done = false;

        while (!done) {
            try {
                String temperature = Files.lines(Paths.get(dir,temp)).
                 findFirst().orElse("0");
                String humidity = Files.lines(Paths.get(dir,hum)).
                 findFirst().orElse("0");
                System.out.printf(
                 "Temperature: %.1fC  %.1fF  Humidity: %.1f%%\n",
                 Double.valueOf(temperature) / 1000.0,
                 Double.valueOf(temperature) / 1000.0 * 9.0 / 5.0 + 32.0,
                 Double.valueOf(humidity) / 1000.0);
                done = true;
            } catch (Exception ioe) {
                //System.out.println(ioe);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }
    }
}

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>

FILE *tf;
FILE *hf;

char *temp_file = "/sys/bus/iio/devices/iio:device0/in_temp_input";
char *hum_file = "/sys/bus/iio/devices/iio:device0/in_humidityrelative_input";

char buf[16];

double temp;
double hum;

bool done = false;

int main(int argc,char *argv[]) {
    while (!done) {
        if ((tf = fopen(temp_file,"r")) != NULL) {
            if (fgets(buf,sizeof(buf),tf) != NULL) {
                temp = atof(buf) / 1000.0;
                if ((hf = fopen(hum_file,"r")) != NULL) {
                    if (fgets(buf,sizeof(buf),hf) != NULL) {
                        hum = atof(buf)/ 1000.0;
                        done = true;
                    } else {
                        usleep(500000);
                    }
                    fclose(hf);
                }
            } else {
                usleep(500000);
            }
            fclose(tf);
        }
    }

    printf("Temperature: %.1fC  %.1fF  Humidity: %.1f%%\n",
     temp,temp * 9.0 / 5.0 + 32.0 ,hum);

    return 0;
}

1uke_
Posts: 68
Joined: Thu Jul 07, 2016 9:19 pm

Re: Looking for help with DHT11 humidity and temperature sensor

Fri Apr 19, 2019 1:22 pm

pcmanbob wrote:
Sat Apr 13, 2019 1:41 pm
You need to post pictures showing the wiring at both ends no just pi end, and you need to tell use which version of the sensor you are using ,
I.E. sensor on pcb with resistor or just sensor with external resistor on breadboard.

I think this may be the instructions knute was suggesting you try.

viewtopic.php?t=222222
Hello again @pcmanbob,

I've finally got around to taking pictures of both the Pi and the DHT11(see links below).

I am using the DHT11 that is on a PCB board. I am not using a resistor with with it.

rPi - https://i.postimg.cc/3W5HcGNV/DHT11-2.jpg

DHT11 (on PCB) - https://i.postimg.cc/3JswsyzK/DHT11-1.jpg

If you could let me know what I need to add in to my code to make it work I'd really appreciate it.

Thank you again for your help with this! :)

pcmanbob
Posts: 6224
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Looking for help with DHT11 humidity and temperature sensor

Fri Apr 19, 2019 3:16 pm

So assuming your sensor has a 10 pull up resistor fitter ( I cant see what's actually on your sensor pcb ) then the connection's look good, if you followed the instructions and changed the code to read gpio 17 , I don't see why its not working.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Automation, sensing and robotics”