Meadow.Foundation.Sensors.Atmospheric.Htu21d
Htu21d | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
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:
HTU21D | Meadow Pin |
---|---|
GND | GND |
SCL | D08 (SCL) |
SDA | D07 (SDA) |
VCC | 3V3 |
It should look like the following diagram:
Class Htu21d
Provide access to the Htu21d(f) temperature and humidity sensors
Assembly: Htux1d.dll
View Source
public class Htu21d : Htux1dBase, IObservable<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>>, ISamplingSensor<(Temperature? Temperature, RelativeHumidity? Humidity)>, ISensor<(Temperature? Temperature, RelativeHumidity? Humidity)>, IDisposable, ITemperatureSensor, ISamplingSensor<Temperature>, ISensor<Temperature>, IHumiditySensor, ISamplingSensor<RelativeHumidity>, ISensor<RelativeHumidity>, ISensor, ISamplingSensor, II2cPeripheral
Inheritance: System.Object
-> Meadow.Foundation.ObservableBase<UNIT>
Implements:
Expand
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>>>>
, Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>>>
, Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>>>
, System.IDisposable
, Meadow.Peripherals.Sensors.ITemperatureSensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Temperature>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Temperature>
, Meadow.Peripherals.Sensors.Atmospheric.IHumiditySensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.RelativeHumidity>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.RelativeHumidity>
, Meadow.Peripherals.Sensors.ISensor
, Meadow.Peripherals.Sensors.ISamplingSensor
, Meadow.Hardware.II2cPeripheral
Properties
FirmwareRevision
Firmware revision of the sensor.
View Source
public byte FirmwareRevision { get; }
Methods
Initialize()
Initialize HTU21D
View Source
protected void Initialize()
ReadSensor()
Reads data from the sensor
View Source
protected override Task<(Temperature? Temperature, RelativeHumidity? Humidity)> ReadSensor()
Returns
System.Threading.Tasks.Task<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>>>
: The latest sensor reading### Heater(bool)
Turn the heater on or off
View Source
public void Heater(bool heaterOn)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | heaterOn | Heater status, true = turn heater on, false = turn heater off. |
Implements
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>>>>
Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>>>
Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>>>
System.IDisposable
Meadow.Peripherals.Sensors.ITemperatureSensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Temperature>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Temperature>
Meadow.Peripherals.Sensors.Atmospheric.IHumiditySensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.RelativeHumidity>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.RelativeHumidity>
Meadow.Peripherals.Sensors.ISensor
Meadow.Peripherals.Sensors.ISamplingSensor
Meadow.Hardware.II2cPeripheral