# DHT11/22 Temp and Humidity Sensors



## Phishfry (Nov 4, 2022)

I never realized we have a GPIO driver for DHT11/22 sensor.





						gpioths(4)
					






					www.freebsd.org
				




I was having problems compiling the DT Overlay.
The FreeBSD manpage gives this:

```
gpios = <&gpio5 15 GPIO_ACTIVE_HIGH>;
```
The GPIO_ACTIVE_HIGH setting is faulty.

```
# dtc -O dtb -o rockpro64-gpioths.dtbo -@ rockpro64-gpioths.dts
Error at rockpro64-gpioths.dts:12:33: Expected numbers in array of cells
                gpio = <&pio 1 4 GPIO_ACTIVE_HIGH>;
                                 ^
Error at rockpro64-gpioths.dts:12:33: Expected ; at end of property
                gpio = <&pio 1 4 GPIO_ACTIVE_HIGH>;
                                 ^
Error at rockpro64-gpioths.dts:12:33: Failed to find root node /.
                gpio = <&pio 1 4 GPIO_ACTIVE_HIGH>;
                                 ^
Failed to parse tree.
```

Simply changing it to 0 does the trick.(Sets the pin high)

/boot/dtb/overlays/rockpro64-gpioths.dts

```
/dts-v1/;
/plugin/;

/ {
    compatible = "rockchip,rk3399";
    fragment@0 {                   
        target-path = "/";
        __overlay__ {
                dht0 {
                compatible = "dht11";
                pinctrl-names = "default";
                gpios = <&gpio1 4 0>;
                status = "okay";
            };
        };
    };
};
```

Add to /boot/loader.conf :

```
gpioths_load="YES"
fdt_overlays="rockpro64-gpioths.dtbo"
```


`dmesg | grep gpioth`

```
gpioths0: <DHT11/DHT22 Temperature and Humidity Sensor> on ofwbus0
```

`sysctl -a | grep gpioths`

```
gpioths0: <DHT11/DHT22 Temperature and Humidity Sensor> on ofwbus0
    value:    /boot/kernel/gpioths.ko
    value:    /boot/dtb/overlays/rockpro64-gpioths.dtbo
dev.gpioths.0.fails: 0
dev.gpioths.0.humidity: 70
dev.gpioths.0.temperature: 24.1C
dev.gpioths.0.%parent: ofwbus0
dev.gpioths.0.%pnpinfo: name=dht0 compat=dht11
dev.gpioths.0.%location:
dev.gpioths.0.%driver: gpioths
dev.gpioths.0.%desc: DHT11/DHT22 Temperature and Humidity Sensor
dev.gpioths.%parent:
```


----------



## ralphbsz (Nov 6, 2022)

On the DHT11/22, you need a separate data wire for each sensor, right? I'm using a 1-wire temperature/humidity sensor, it's called the WallTH. Problem is (and I think the DHT shares that, as do most inexpensive humidity sensors): If installed outdoors, they will occasionally get condensation on them. And that kills the humidity sensor: if you do it occasionally, they just get inaccurate; if you do it repeatedly, they report insane values. For example, right now it is raining outdoors, and the good weather station reports 94% humidity (which I believe). The two WallTH sensors report 101% and 145%. I've also recently seen them report negative humidity.

I'm still looking for an inexpensive embeddable humidity sensor that survives outdoors, which is easy to wire (no more than 3 wires, and ideally not dedicated wires). Not a high-priority project, just for curiosity.


----------



## Phishfry (Nov 6, 2022)

ralphbsz said:


> On the DHT11/22, you need a separate data wire for each sensor, right?


Just doing that now. Adding additional sensors.
Yes it appears each sensor needs a GPIO pin versus One Wire Temp DS1820 which uses a daisy chained OW bus.
The DHT22 is advertised as more accurate version of DHT11.
I found my additional sensors in a tub, AM2302. These should be usable.

It does appear that DHT11/22 offers a 4 pin version which works with I2C.
I do imagine with that you could daisychain. I don't think the driver supports i2c from my source code reading.
/usr/src/sys/dev/gpio/gpioths.c
I really appreciate the exquisitely commented code.
I see this too in the source:


> *  AM2301: Same as DHT21, but also supports i2c interface.
> *  AM2302: Same as DHT22, but also supports i2c interface.


My AM2302 modules are only 3 wire.
I see nothing in the code body about i2c.


----------



## Phishfry (Nov 6, 2022)

For a rugged outdoors temperature-only sensor the stainless encapsulated ds1820 probes fit the bill.








						DS18B20 temperature probe temperature sensor DS1820 Stainless steel Waterproof   | eBay
					

Find many great new & used options and get the best deals for DS18B20 temperature probe temperature sensor DS1820 Stainless steel Waterproof  at the best online prices at eBay! Free shipping for many products!



					www.ebay.com


----------



## ralphbsz (Nov 6, 2022)

Temperature is easy. I use various DS182x sensors, some outdoors (lots of heatshrink and coating, carefully protected from direct rain). Humidity is hard, once condensation happens.


----------



## Phishfry (Nov 6, 2022)

We have lift-off:

```
# dmesg | grep gpioth
gpioths0: <DHT11/DHT22 Temperature and Humidity Sensor> on ofwbus0
gpioths1: <DHT11/DHT22 Temperature and Humidity Sensor> on ofwbus0

# sysctl dev.gpioths
dev.gpioths.1.fails: 0
dev.gpioths.1.humidity: 61
dev.gpioths.1.temperature: 22.5C
dev.gpioths.1.%parent: ofwbus0
dev.gpioths.1.%pnpinfo: name=dht0 compat=dht11
dev.gpioths.1.%location:
dev.gpioths.1.%driver: gpioths
dev.gpioths.1.%desc: DHT11/DHT22 Temperature and Humidity Sensor
dev.gpioths.0.fails: 0
dev.gpioths.0.humidity: 67
dev.gpioths.0.temperature: 22.2C
dev.gpioths.0.%parent: ofwbus0
dev.gpioths.0.%pnpinfo: name=dht1 compat=dht11
dev.gpioths.0.%location:
dev.gpioths.0.%driver: gpioths
dev.gpioths.0.%desc: DHT11/DHT22 Temperature and Humidity Sensor
dev.gpioths.%parent:
```

So my approach in this case was to complicate my overlays with additional details.
Long filenames but descriptive.
rockpro64-gpioths-dht1-pin18.dtbo
I added an incremented dht* name and GPIO header pin number.
That way I can add and remove individual sensors.
The other approach is to jam them all into one overlay.
But when you want to make a change you must recompile.
I like prebuilt dtbo so I can pick and choose.
All it takes that way is add your precompiled binaries as multiple overlays to /boot/loader.conf

```
fdt_overlays="rockpro64-gpioths-dht0-pin16.dtbo,rockpro64-gpioths-dht1-pin18.dtbo,rockpro64-w1-gpio-pin15.dtbo"
```

Raspberry Pi has that excellent overlay port misc/raspberrypi-userland that offers pre-compiled binary overlays.
Its is a great way to get to work quickly..I try and emulate it on all boards.


----------

