Remarks

Dht12
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Dhtxx

The DHT12 is a low-cost humidity and temperature sensor that communicates over the I2C bus. It measures humidity from 20 to 95% with an accuracy of +/- 5% relative humidity with a resolution of 0.1%. Temperature range is from -20 to 60 degrees celcius with an accuracy of +/- 0.5 degrees and a resolution of 0.1.

Code Example

Dht12 sensor;

public override Task Initialize()
{
    Console.WriteLine("Initializing...");

    sensor = new Dht12(Device.CreateI2cBus());

    var consumer = Dht12.CreateObserver(
        handler: result =>
        {
            Console.WriteLine($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            //c# 8 pattern match syntax. checks for !null and assigns var.
            if (result.Old?.Temperature is { } oldTemp &&
                result.Old?.Humidity is { } oldHumidity &&
                result.New.Temperature is { } newTemp &&
                result.New.Humidity is { } newHumidity)
            {
                return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
                        (newHumidity - oldHumidity).Percent > 0.05);
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (object sender, IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)> e) =>
    {
        Console.WriteLine($"  Temperature: {e.New.Temperature?.Celsius:N2}C");
        Console.WriteLine($"  Relative Humidity: {e.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    var conditions = await sensor.Read();
    Console.WriteLine("Initial Readings:");
    Console.WriteLine($"  Temperature: {conditions.Temperature?.Celsius:N2}C");
    Console.WriteLine($"  Relative Humidity: {conditions.Humidity?.Percent:N2}%");

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub

Wiring Example

To wire a DHT12 to your Meadow board, connect the following:

DHT12 Meadow Pin
GND GND
VCC 3V3
SCL D08 (SCL Pin)
SDA D07 (SDA Pin)

Characteristic Locus
Inheritance System.Object ObservableBase<System.ValueTuple<System.Nullable<Units.Temperature>, System.Nullable<RelativeHumidity>>> SensorBase<System.ValueTuple<System.Nullable<Units.Temperature>, System.Nullable<RelativeHumidity>>> SamplingSensorBase<System.ValueTuple<System.Nullable<Units.Temperature>, System.Nullable<RelativeHumidity>>> ByteCommsSensorBase<System.ValueTuple<System.Nullable<Units.Temperature>, System.Nullable<RelativeHumidity>>> DhtBase > Dht12
Implements IDisposable ITemperatureSensor IHumiditySensor
Inherited Members DhtBase.TemperatureUpdated DhtBase.HumidityUpdated DhtBase.Temperature DhtBase.Humidity DhtBase.WasLastReadSuccessful DhtBase.RaiseEventsAndNotify(IChangeResult<ValueTuple<Nullable<Units.Temperature>, Nullable<RelativeHumidity>>>) DhtBase.ReadSensor() ByteCommsSensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.Peripheral ByteCommsSensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.ReadBuffer ByteCommsSensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.WriteBuffer ByteCommsSensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.Init(Int32, Int32) ByteCommsSensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.Dispose(Boolean) ByteCommsSensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.Dispose() SamplingSensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.StopUpdating() SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.Updated SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.samplingLock SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.SamplingTokenSource SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.Conditions SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.IsSampling SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.UpdateInterval SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.ReadSensor() SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.Read() ObservableBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.observers ObservableBase<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Units.Temperature>, System.Nullable<RelativeHumidity>>>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Units.Temperature>, System.Nullable<RelativeHumidity>>>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Atmospheric
Assembly Dhtxx.dll

Syntax

public class Dht12 : DhtBase, IDisposable, ITemperatureSensor, IHumiditySensor

Constructors

Dht12(II2cBus, Byte)

Create a new Dht12 object

Declaration
public Dht12(II2cBus i2cBus, byte address = null)

Parameters

Type Name Description
II2cBus i2cBus

I2C bus (default = 100 KHz)

System.Byte address

Address of the Dht12 (default = 0x27)