kehall
Posts: 26
Joined: Thu Dec 05, 2013 9:52 pm

device-tree for DHT11 Temperature/Humidity sensor

Wed Feb 04, 2015 10:33 am

I've created a device tree overlay for the popular DHT11 Temp/Humidity sensor, below:

Code: Select all

// Definitions for dht11 module
/dts-v1/;
/plugin/;

/ {
        compatible = "brcm,bcm2708";

        [email protected] {
                target-path = "/";
                __overlay__ {

                        dht11: [email protected] {
                                compatible = "dht11";
                                pinctrl-names = "default";
                                pinctrl-0 = <&dht11_pins>;
                                gpios = <&gpio 4 0>;
                                status = "okay";
                        };
                };
        };

        [email protected] {
                target = <&gpio>;
                __overlay__ {
                        dht11_pins: dht11_pins {
                                brcm,pins = <4>;
                                brcm,function = <0>; // in
                                brcm,pull = <0>; // off
                        };
                };
        };

        __overrides__ {
                gpiopin =       <&dht11_pins>,"brcm,pins:0",
                                <&dht11>,"gpios:4";
        };
};
This requires the kernel to be compiled with IndustrialIO (iio) modules (the humidity dht11 module by Harald Geyer who thankfully pointed me at device tree as I was completely lost until then).

Would it be possible to have these distributed as modules as part of the rpi-update kernel?

The overlay can be compiled with overlay capable dtc compiler:

Code: Select all

/usr/local/bin/dtc -I dts -O dtb -o dht11-overlay.dtb -b 0 [email protected] dht11.dts
The compiled dht11-overlay.dtb file (attached to this post for convenience) goes in /boot/overlays

Add the overlay to the /boot/config.txt file:

Code: Select all

dtoverlay=dht11,gpiopin=4
The gpio pin can be adjusted from the default of 4

It may not look like much, but its a result of hours of messing around with a pretty steep learning curve on device tree from zero knowledge ;)

Hope this helps someone, and if there's anything incorrect please let me know!

Keith
Attachments
dht11.zip
device tree overlay file for /boot/overlays
(542 Bytes) Downloaded 395 times

lihgong
Posts: 2
Joined: Mon Apr 27, 2015 12:49 am

Re: device-tree for DHT11 Temperature/Humidity sensor

Mon Apr 27, 2015 2:03 am

Hello, your post inspired me a lot.

I tried to use RPi + DHT11 with Python+GPIO. However, I don't think that's the best way to interact with sensor. Linux kernel's IIO should be the proper way to interact with the sensor.

I would like to ask more detail about how to use the sensor. Here's what I've done:

1. Recompile 3.8.11+ kernel with IIO/DHT11 as module. I've tested that I can run Pi with the self-built kernel
2. Put your dht11-overlay.dtb @ /boot/overlays/
3. Add "dtoverlay=dht11,gpiopin=4" in /boot/config.txt
4. Connect DHT11 sensor with 3.3V, GND, and GPIO4 on Pi
5. Reboot Pi, and use "insmod" to load following modules
- industrialio.ko
- kfifo_buf.ko
- dht11.ko
6. I can see the iio folder appeared at "/sys/bus/iio", but it seems no DHT-11 related file within
7. I read the DHT11 driver, and it had a probe() function. It seems that I should triggers IIO subsystem to make the dht11::probe() to run. But don't have clear idea now.

Could you provide some hints? thanks in advance :P

kehall
Posts: 26
Joined: Thu Dec 05, 2013 9:52 pm

Re: device-tree for DHT11 Temperature/Humidity sensor

Mon Apr 27, 2015 9:41 am

Hi lihgong,

You shouldn't need to insmod anything as it will grab automatically, IIRC.

I presume /proc/device-tree exists? DT needs to be enabled on the kernel...

can you stick dtdebug=1 in to config.txt, reboot and see what the output of "sudo vcdbg log msg" says? It's also possible you haven't 'enabled DT' by not adding the kernel trailer with mkknlimg

See section 4.1 here: https://www.raspberrypi.org/documentati ... ce-tree.md

the only other time I've not seen this work when everything should be ok, is when the device cant be probed and found...

Is the device definitely on gpio pin 4? have you got a pull-up resistor between 3.3v and data?

Keith

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2093
Joined: Thu Jul 11, 2013 2:37 pm

Re: device-tree for DHT11 Temperature/Humidity sensor

Mon Apr 27, 2015 10:36 am

Thanks for following the expected flow for getting a "new" device working with DT overlays and Raspberry Pi. The next step would be to submit a pull request to https://github.com/raspberrypi/linux with the following things in it:

- Addition of whatever kernel config options to the defconfig required to make this work (preferably all as modules, or as compiled-in options with justification)
- Addition of a tested devicetree overlay that can be added to /boot/overlays
- A boot log with your changes applied showing the probe messages and output from the sensor.

Because your overlay claims a pin, consideration needs to be given as to how this would interact with other consumers if multiple overlays were enabled. The Pinctrl framework prevents you from loading two conflicting overlays, so the overlay/associated drivers should use that interface.
Rockets are loud.
https://astro-pi.org

lihgong
Posts: 2
Joined: Mon Apr 27, 2015 12:49 am

Re: device-tree for DHT11 Temperature/Humidity sensor

Fri May 01, 2015 3:00 pm

Hi Keith,

Thanks for your help. Finally I found the self-built kernel didn't enable device tree support.

To enable that, I ran "mkknlimg" utility mentioned in the document:
https://www.raspberrypi.org/documentati ... ce-tree.md

The discussion here provides excellent guides to build device-tree enabled kernel
viewtopic.php?f=29&t=98009

----

Let's go back to DHT11 sensor.

After adding the line "dtoverlay=dht11,gpiopin=4" in /boot/config.txt and reboot, the DHT11 sensor appeared in
/sys/bus/iio/devices/iio:device0/

The files within this folder
- dev, 253:0, Looks loke the ID of the device
- name, [email protected], Device name, and sequence number
- in_temp_input, Trigger point, use "cat in_temp_input" could read temperature
- in_humidityrelative_input, Trigger point, use "cat in_humidityrelative_input" could read humidity

Dig the sensor driver is quite interesting, which is located in
linux/drivers/iio/humidity/dht11.c

At boot time, the function "dht11_probe()" is invoked to find the sensor. If found, then the function registers the iio device. When I read the trigger point, the function "dht11_read_raw()" is invoked where the driver interacts with sensor. The things originally done in user-space is now moved to kernel space, where we have more real-time IRQ support.

----

One more thing I still don't understand is the stability of reading the temperature/humidity. I wrote a script to check temperature every seconds. As I "cat" the trigger-point, sometimes I got following errors (~10% error rate)
- cat: /sys/devices/[email protected]/iio:device0/in_temp_input: Input/output error
- cat: /sys/devices/[email protected]/iio:device0/in_temp_input: Connection time out

My DHT11 module is from "Keyes" which contains pull-up resistor. I tried to connect VDD to either 3.3v/5v, but the stability remains the same. Do you have similar problem?

Lihgong

Tim Barker
Posts: 40
Joined: Sun Oct 05, 2014 5:02 pm

Re: device-tree for DHT11 Temperature/Humidity sensor

Wed Feb 03, 2016 11:16 am

Keith/lihgong/jdb,

Apologies for bringing this 'back from the dead', so to speak.

Have been very successful using a number of DS18Bxx temperature sensors in several locations to record the inside/outside/cellar/attic temperatures etc. Plenty of people doing this and the various 'tutorials' helped a lot. One or two problems but these concerned changing environments or software (ie at first moving to Pi B+, then trying linux 4.x, device tree, then even trying B2s).

Generally I always moved back one step when something wouldn't work then tried advancing later when I had the time to spend on finding a solution. Such was the device tree. Finally after an upgrade I managed to get the DS18 sensors working on the B2 under 4.x.

I then decide to add a humidity sensor, and as luck had it the only ones that seems to be everywhere are the DHT11 variants (including the DHT22 and AM2302). On my, older, test machine (an A+) it was relatively easy to get it going using the Adafruit tutorials and some others (http://docs.gadgetkeeper.com/pages/view ... Id=7700673 for example). So off I went modifying the perl software to include the AM2302 sensor I was using on my B2. That's where I hit another wall.

And that's where this thread helped me after much Googling for answers. As always it was 'how does one frame the search?' that provided the eventual solution and helped with the device-tree which appeared to be the stumbling block between the 2 machines.

But I am only part of the way there. I'm trying to avoid using python (learning physical languages are more important to me at the moment than software) and sticking with perl and bash. Some would say a succour for punishment. At first I thought I might need to use a python programme to get the data but then on reading this thread I discovered I could read a physical location on the system in much the same way as I read a location to get the data from a DS18 sensor. This proved a turning point after getting the device tree to work (damn those hyphens that seem to creep in unbidden in the overlay names).

Now all I need to do is get the perl programme that reads "/sys/bus/iio/devices/iio:device0/in_temp_input" going properly. Theoretically it should be as simple as reading the DS sensors (ie using...

Code: Select all

cat /sys/bus/iio/devices/iio:device0/in_temp_input
but I would have to say it is proving more elusive due to the error messages that keep coming back.

But thanks everybody for getting me on the right track... I might try to post the final solution for completion's sake when I have it.

Tim Barker.

User avatar
joan
Posts: 14262
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: device-tree for DHT11 Temperature/Humidity sensor

Wed Feb 03, 2016 12:07 pm

Tim Barker wrote: ...
Now all I need to do is get the perl programme that reads "/sys/bus/iio/devices/iio:device0/in_temp_input" going properly. Theoretically it should be as simple as reading the DS sensors (ie using...

Code: Select all

cat /sys/bus/iio/devices/iio:device0/in_temp_input
but I would have to say it is proving more elusive due to the error messages that keep coming back.
...
I tried the iio kernel module when it was added to the Pi kernel.

I gave up as all I get from it are error messages.

I understand at least one poster has had some success.

adam van gaalen
Posts: 32
Joined: Sun Jul 01, 2012 11:51 am

Re: device-tree for DHT11 Temperature/Humidity sensor

Fri Jun 24, 2016 1:30 pm

I too had all kinds of problems reading the TEMP and RH values.
If you read them you get I/O-errors, timeouts etc...

I found out however, that when you read the sensor-values a lot of times in a small loop, you will get good-looking values.

Here is an example in a tcsh script:

#!/bin/tcsh
#
@ NTRIES = 0
echo '' >& TEMP_readings
echo '' >& RH_readings
while ( ${NTRIES} < 100 )
/bin/cat /sys/devices/platform/[email protected]/iio:device0/in_temp_input >>& TEMP_readings
/bin/cat /sys/devices/platform/[email protected]/iio:device0/in_humidityrelative_input >>& RH_readings
@ NTRIES = ${NTRIES} + 1
end
#
set TEMP = "`/bin/cat TEMP_readings |& /bin/grep -v [email protected] | /bin/grep -v -- '-' | /usr/bin/uniq -c | /usr/bin/sort -n | /usr/bin/tail -1 | /usr/bin/tr -s ' ' ' ' | /bin/sed -e 's/^ //' | /usr/bin/cut -f2 -d' '`"
set RH = "`/bin/cat RH_readings |& /bin/grep -v [email protected] | /bin/grep -v -- '-' | /usr/bin/uniq -c | /usr/bin/sort -n | /usr/bin/tail -1 | /usr/bin/tr -s ' ' ' ' | /bin/sed -e 's/^ //' | /usr/bin/cut -f2 -d' '`"
#
echo "TEMP = ${TEMP}, RH = ${RH}"
#
exit 0


The script reads the values 100 times and uses the value that occurs most in the output.

User avatar
joan
Posts: 14262
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: device-tree for DHT11 Temperature/Humidity sensor

Fri Jun 24, 2016 2:07 pm

adam van gaalen wrote: ...
The script reads the values 100 times and uses the value that occurs most in the output.
Or just read it once from userland, e.g. http://abyz.co.uk/rpi/pigpio/examples.html#pdif2_DHTXXD

adam van gaalen
Posts: 32
Joined: Sun Jul 01, 2012 11:51 am

Re: device-tree for DHT11 Temperature/Humidity sensor

Fri Jun 24, 2016 2:19 pm

OK, from a C-program you could do that.

I wrote my stuff because people complain that when they 'cat' de DHT11-file they keep getting lots of errors.
Usually I get > 90 good values when reading the sensor 100 times.

Of course one could decide to read 10 times only, probably still producing about 8 to 10 good values!

User avatar
joan
Posts: 14262
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: device-tree for DHT11 Temperature/Humidity sensor

Fri Jun 24, 2016 2:26 pm

adam van gaalen wrote:OK, from a C-program you could do that.

I wrote my stuff because people complain that when they 'cat' de DHT11-file they keep getting lots of errors.
Usually I get > 90 good values when reading the sensor 100 times.

Of course one could decide to read 10 times only, probably still producing about 8 to 10 good values!
I'll give your script a run when I next connect a DHT11/22. I don't know why the kernel module appears to be so unreliable, but it has not worked at all for me so far.

adam van gaalen
Posts: 32
Joined: Sun Jul 01, 2012 11:51 am

Re: device-tree for DHT11 Temperature/Humidity sensor

Fri Jun 24, 2016 9:37 pm

Instead of:
while ( ${NTRIES} < 100 )

I have been going as far down as:
while ( ${NTRIES} < 5 )

and the code still appears to be rocksolid on my RPi model B.

Doing only 5 reads instead of 100 saves time and may have a positive effect on the lifetime of the sensor.

jaccol
Posts: 1
Joined: Thu May 25, 2017 5:09 pm

Re: device-tree for DHT11 Temperature/Humidity sensor

Fri May 26, 2017 8:53 am

Hi,

I'd like to use this overlay to read multiple (at least two) DHT22 devices.
I could not find how to do this.

Can you tell me if this is supported and, if so, how to go about this?

Thanks,

Jacco

Return to “Advanced users”