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

DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 11:09 am

The move to the Linux 4 kernel brings the availability of built-in kernel drivers for the DHT11 (and I think DHT22) temperature and humidity sensors.

See this post for some background.

I'm interested in how well the kernel drivers perform relative to the existing userland solutions.

Unfortunately I can't get the kernel driver to work.
  • connected a DHT11 to 3V3, ground, and gpio 25.
  • upgraded the kernel to 4.0.5 using rpi-update.
  • added the line dtoverlay=dht11,gpiopin=25 to /boot/config.txt
  • Reboot
Result
  • the iio (industrial io) device is created as /dev/iio:device0
  • entries dev, in_humidityrelative_input, in_temp_input, name, subsystem, and uevent in /sys/bus/iio/devices/iio\:device0/
  • lsmod shows dht11 and industrialio
Unfortunately cat /sys/bus/iio/devices/iio\:device0/in_humidityrelative_input or cat /sys/bus/iio/devices/iio\:device0/in_temp_input produces the error message "Connection timed out".

The cat does trigger a reading which appears well formed.
dht11.gif
DHT11 reading
dht11.gif (39.46 KiB) Viewed 7860 times
I can read the sensor without problem from userland.

Any idea what I need to change to get the kernel driver to work?

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

Re: DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 11:13 am

It's glitchy.

I get about 75% success rate for reads, the driver alternately responds with input/output error if it can't decode things correctly. There's one definite bug that results in the GPIO interrupt getting disabled.

Can you post a dmesg?
Rockets are loud.
https://astro-pi.org

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

Re: DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 11:19 am

jdb wrote:It's glitchy.

I get about 75% success rate for reads, the driver alternately responds with input/output error if it can't decode things correctly. There's one definite bug that results in the GPIO interrupt getting disabled.

Can you post a dmesg?
I guess this indicates the problem.

Code: Select all

[  432.364096] iio iio:device0: Only 79 signal edges detected
[ 1233.287453] iio iio:device0: Only 78 signal edges detected
[ 1240.997593] iio iio:device0: Only 80 signal edges detected
[ 1246.117683] iio iio:device0: Only 79 signal edges detected
[ 1272.498153] iio iio:device0: Only 81 signal edges detected
[ 1303.018775] iio iio:device0: Only 80 signal edges detected
[ 1330.649188] iio iio:device0: Only 81 signal edges detected
[ 1336.479292] iio iio:device0: Only 79 signal edges detected
[ 1356.729654] iio iio:device0: Only 81 signal edges detected
[ 1362.189754] iio iio:device0: Only 80 signal edges detected
[ 1391.110268] iio iio:device0: Only 80 signal edges detected
[ 1401.350458] iio iio:device0: Only 80 signal edges detected
[ 1403.610610] iio iio:device0: Only 81 signal edges detected
[ 1411.710636] iio iio:device0: Only 81 signal edges detected
[ 1478.061893] iio iio:device0: Only 81 signal edges detected
[ 1488.752075] iio iio:device0: Only 79 signal edges detected
[ 1492.882085] iio iio:device0: Only 79 signal edges detected
[ 1808.798881] iio iio:device0: Only 81 signal edges detected
[ 3221.681593] iio iio:device0: Only 79 signal edges detected
[ 3224.741647] iio iio:device0: Only 79 signal edges detected
Do you want anything else from dmesg? Anything I can grep on?

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

Re: DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 11:23 am

Which model are you using - BCM2835 or BCM2836?
Rockets are loud.
https://astro-pi.org

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

Re: DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 11:28 am

It's plugged in to a B+ (2835) at the moment. I'll try a Pi2 (2836) and see if it performs better.

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

Re: DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 11:49 am

I have no more success on a Pi2.

I just get a different error message (Input/Output error) but nothing at all in dmesg.

Code: Select all

paul ~ $ sudo pigpiod
paul ~ $ dmesg|grep dht
paul ~ $ dmesg|grep iio
paul ~ $ cat /sys/bus/iio/devices/iio\:device0/in_temp_input  
cat: /sys/bus/iio/devices/iio:device0/in_temp_input: Input/output error
paul ~ $ dmesg|grep dht
paul ~ $ dmesg|grep iio
paul ~ $ cat /sys/bus/iio/devices/iio\:device0/in_temp_input  
cat: /sys/bus/iio/devices/iio:device0/in_temp_input: Input/output error
paul ~ $ dmesg|grep dht
paul ~ $ dmesg|grep iio
paul ~ $ cat /sys/bus/iio/devices/iio\:device0/in_temp_input  
cat: /sys/bus/iio/devices/iio:device0/in_temp_input: Input/output error
paul ~ $ cat /sys/bus/iio/devices/iio\:device0/in_temp_input  
cat: /sys/bus/iio/devices/iio:device0/in_temp_input: Input/output error
paul ~ $ dmesg|grep dht
paul ~ $ dmesg|grep iio
paul ~ $ /code/dht11.py
1 40 21
2 40 21
3 40 22
4 40 21
5 40 21
6 40 21
^CTraceback (most recent call last):
  File "/code/dht11.py", line 162, in <module>
    time.sleep(1)
KeyboardInterrupt
paul ~ $ dmesg|grep iio
paul ~ $ dmesg|grep dht
paul ~ $ cat /sys/bus/iio/devices/iio\:device0/in_temp_input  
cat: /sys/bus/iio/devices/iio:device0/in_temp_input: Input/output error
paul ~ $ cat /sys/bus/iio/devices/iio\:device0/in_temp_input  
cat: /sys/bus/iio/devices/iio:device0/in_temp_input: Input/output error
paul ~ $ cat /sys/bus/iio/devices/iio\:device0/in_temp_input  
cat: /sys/bus/iio/devices/iio:device0/in_temp_input: Input/output error
paul ~ $ dmesg|grep iio
paul ~ $ dmesg|grep dht
paul ~ $ 

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

Re: DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 12:49 pm

You may have a variant with "different" timing parameters.

The protocol is quite simple - it's
- Host drives DAT low for ~some ms
- Host lets DAT return high, after 20uS the DHT11 becomes the bus master.
- An initial ACK is sent - 80uS space / 80uS mark
- Data stream follows
- Data is 50uS space/28uS mark for a '1'
- Data is 50uS space/70uS mark for a '0'

A final space-mark transition terminates the transfer.

Can you record what timings you get on the logic analyzer?
Rockets are loud.
https://astro-pi.org

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 1:08 pm

jdb wrote:- Data is 50uS space/28uS mark for a '1'
- Data is 50uS space/70uS mark for a '0'
Sensor datasheet speaks about:
- 50us low, 26-28us high for logic 0
- 50 us low, 70us high for logic 1

http://www.micropik.com/PDF/dht11.pdf
(figure 4 and 5)

am i wrong?

This is exaclty the same of DHT 22
https://www.sparkfun.com/datasheets/Sen ... /DHT22.pdf

my dth22 is working like a charm with these timings

Edit to add: could you please specify also these timings?
- Host drives DAT low for ~some ms -> it's at least 18
- Host lets DAT return high, after 20uS the DHT11 becomes the bus master. -> it's 20-40

I'm reporting this because 75% is a very low success rate. I can't remember my last fail on dht22..

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

Re: DHT11/DHT22 Linux 4 iio device driver

Wed Jun 24, 2015 1:54 pm

These are one set of timings from the DHT11. 15 HIGH indicates 15µs high. The readings seem fairly standard.

Code: Select all

28606 LOW
15 HIGH
80 LOW
90 HIGH
50 LOW
25 HIGH
55 LOW
25 HIGH
55 LOW
70 HIGH
50 LOW
25 HIGH
54 LOW
70 HIGH
56 LOW
25 HIGH
50 LOW
25 HIGH
54 LOW
25 HIGH
50 LOW
26 HIGH
55 LOW
25 HIGH
49 LOW
25 HIGH
55 LOW
25 HIGH
50 LOW
25 HIGH
55 LOW
25 HIGH
55 LOW
20 HIGH
56 LOW
25 HIGH
55 LOW
24 HIGH
50 LOW
25 HIGH
56 LOW
25 HIGH
50 LOW
74 HIGH
50 LOW
26 HIGH
55 LOW
69 HIGH
55 LOW
20 HIGH
56 LOW
70 HIGH
54 LOW
25 HIGH
55 LOW
20 HIGH
56 LOW
25 HIGH
55 LOW
19 HIGH
55 LOW
25 HIGH
56 LOW
20 HIGH
55 LOW
25 HIGH
54 LOW
25 HIGH
51 LOW
25 HIGH
55 LOW
25 HIGH
55 LOW
70 HIGH
55 LOW
70 HIGH
49 LOW
75 HIGH
51 LOW
70 HIGH
54 LOW
25 HIGH
55 LOW
66 HIGH
60 LOW

raich
Posts: 2
Joined: Sun Oct 09, 2016 5:49 pm

Re: DHT11/DHT22 Linux 4 iio device driver

Fri Dec 30, 2016 1:54 pm

Hi,
The problems seems to be interrupts that come in when you read the DHT11 pulse train.
I wrote a very simplistic Linux device driver with only an open, close and read function. In the read function I initiate a DHT11 measurement and before switching the GPIO pin from output to input, used to initiate the data transfer and to read the pulse train from the DHT11, I disable the interrupts. I sample the state of the GPIO pin every 2 us for the length of the transfer and allow interrupts only once the complete data transfer is finished. The read function then returns a char array of zeros and ones corresponding to the pulse train.
In addition to the driver I have a small shared library wrapping up the driver calls and extracting temperature and humidity from the pulse train.
I had a test program running for some 2 hours taking a measurement every 5s and producing no errors. If there is interest I can upload the sources.
Uli

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

Re: DHT11/DHT22 Linux 4 iio device driver

Fri Dec 30, 2016 2:44 pm

The userland versions seem to be as reliable.

Perhaps compare your results with my http://abyz.co.uk/rpi/pigpio/examples.html#pdif2_DHTXXD

raich
Posts: 2
Joined: Sun Oct 09, 2016 5:49 pm

Re: DHT11/DHT22 Linux 4 iio device driver

Sat Dec 31, 2016 11:05 am

Thanks for the link. I will have a look. The programs that came with the DHT11 all had some code saying

if (the answer is bad)
try again;

and when I tried to access the DHT11 from user land using the wiringpi library I experienced the same problems (the pulse train read from the DHT11 had errors in some 10%-20% of readings.
However, I am rather new to the raspberry and I did not know the pigpio library and examples you pointed me to.
This will help me quite a bit when trying other devices.
Thanks again and a happy new year to you
Uli

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

Re: DHT11/DHT22 Linux 4 iio device driver

Sat Dec 31, 2016 11:17 am

If you disable interrupts and spin on the timer/sample a GPIO inside a loop then of course you will get accurate results.

The problem is that doing this completely wrecks latency for the rest of the system, which causes issues as several peripherals (USB, SD) require timely servicing of interrupts. Thus your options are
a) Spin in a loop until you get a valid response back (the checksum matches) or
b) Disable interrupts and accept that sometimes USB and/or SD will break.

I have toyed with the idea of using 1 data pin of the SMI interface as a 1-wire-esque controller and using DMA to do register writes to enact bus turnarounds between read and write phases - this would guarantee timing as well as hand-off any CPU involvement. This would work for 1-wire as well as DHT-style sensors.
Rockets are loud.
https://astro-pi.org

Return to “Troubleshooting”