Remarks

Htu21d
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Htu21d

The HTU21D is a low-cost, easy to use, highly accurate, digital humidity and temperature sensor controlled via the I2C bus.

Code Example

Htu21d sensor;

public MeadowApp()
{
    Console.WriteLine("Initializing...");

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

    var consumer = Htu21d.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 += (sender, result) =>
    {
        Console.WriteLine($"  Temperature: {result.New.Temperature?.Celsius:F1}C");
        Console.WriteLine($"  Relative Humidity: {result.New.Humidity?.Percent:F1}%");
    };

    ReadConditions().Wait();

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

async Task ReadConditions()
{
    var result = await sensor.Read();
    Console.WriteLine("Initial Readings:");
    Console.WriteLine($"  Temperature: {result.Temperature?.Celsius:F1}C");
    Console.WriteLine($"  Relative Humidity: {result.Humidity:F1}%");
}

Sample project(s) available on GitHub

Wiring Example

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

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

It should look like the following diagram:

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>>> Htux1dBase > Htu21d
Implements IDisposable ITemperatureSensor IHumiditySensor
Inherited Members Htux1dBase.TemperatureUpdated Htux1dBase.HumidityUpdated Htux1dBase.DEFAULT_SPEED Htux1dBase.Temperature Htux1dBase.Humidity Htux1dBase.SerialNumber Htux1dBase.RaiseEventsAndNotify(IChangeResult<ValueTuple<Nullable<Units.Temperature>, Nullable<RelativeHumidity>>>) 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<ValueTuple<Nullable<Units.Temperature>, Nullable<RelativeHumidity>>>) 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<ValueTuple<Nullable<Units.Temperature>, Nullable<RelativeHumidity>>>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>, System.Nullable<RelativeHumidity>>>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>, System.Nullable<RelativeHumidity>>>.CreateObserver(Action<>, System.Nullable<Predicate<Meadow.IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Atmospheric
Assembly Htux1d.dll

Syntax

public class Htu21d : Htux1dBase, IDisposable, ITemperatureSensor, IHumiditySensor

Constructors

Htu21d(II2cBus, Byte, Nullable<TimeSpan>)

Create a new Htu21d temperature and humidity sensor

Declaration
public Htu21d(II2cBus i2cBus, byte address = null, TimeSpan? updateInterval = null)

Parameters

Type Name Description
II2cBus i2cBus

I2CBus (default to 100 KHz)

System.Byte address

Sensor address (default to 0x40)

System.Nullable<TimeSpan> updateInterval

Update interval, defaults to 1 sec if null

Properties

FirmwareRevision

Firmware revision of the sensor.

Declaration
public byte FirmwareRevision { get; }

Property Value

Type Description
System.Byte

Methods

Heater(Boolean)

Turn the heater on or off

Declaration
public void Heater(bool heaterOn)

Parameters

Type Name Description
System.Boolean heaterOn

Heater status, true = turn heater on, false = turn heater off.

Initialize()

Initialize HTU21D

Declaration
protected void Initialize()

ReadSensor()

Declaration
protected override Task<(Units.Temperature? Temperature, RelativeHumidity? Humidity)> ReadSensor()

Returns

Type Description
Task<System.ValueTuple<System.Nullable<Units.Temperature>, System.Nullable<RelativeHumidity>>>

Overrides

Meadow.Foundation.SensorBase<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>, System.Nullable<RelativeHumidity>>>.ReadSensor()