Skip to main content

Meadow.Foundation.Sensors.Atmospheric.Htu21d

Htu21d
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Htux1d

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 override Task Initialize()
{
Resolver.Log.Info("Initializing...");

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

var consumer = Htu21d.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
},
filter: result =>
{
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) =>
{
Resolver.Log.Info($" Temperature: {result.New.Temperature?.Celsius:F1}C");
Resolver.Log.Info($" Relative Humidity: {result.New.Humidity?.Percent:F1}%");
};

return Task.CompletedTask;
}

public override async Task Run()
{
if (sensor == null) { return; }

var result = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($" Temperature: {result.Temperature?.Celsius:F1}C");
Resolver.Log.Info($" Relative Humidity: {result.Humidity:F1}%");

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

Sample project(s) available on GitHub

Wiring Example

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

HTU21DMeadow Pin
GNDGND
SCLD08 (SCL)
SDAD07 (SDA)
VCC3V3

It should look like the following diagram: