Remarks

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

Code Example

Sht4x sensor;

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

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

    var consumer = Sht4x.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 is { } old)
            {
                return (
                (result.New.Temperature.Value - old.Temperature.Value).Abs().Celsius > 0.5
                &&
                (result.New.Humidity.Value.Percent - old.Humidity.Value.Percent) > 0.05
                );
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (sender, result) =>
    {
        Console.WriteLine($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Console.WriteLine($"  Relative Humidity: {result.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 Sht4x to your Meadow board, connect the following:

Sht4x Meadow Pin
GND GND
SCL D08 (SCL)
SDA D07 (SDA)
VCC 3V3
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>>> > Sht4x
Implements IDisposable ITemperatureSensor IHumiditySensor
Inherited Members 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 Sht4x.dll

Syntax

public class Sht4x : ByteCommsSensorBase<(Units.Temperature? Temperature, RelativeHumidity? Humidity)>, IDisposable, ITemperatureSensor, IHumiditySensor

Constructors

Sht4x(II2cBus, Byte)

Create a new SHT4x object

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

Parameters

Type Name Description
II2cBus i2cBus

I2cBus

System.Byte address

Sensor address (0x44 or 0x45)

Properties

Humidity

The humidity, in percent relative humidity, from the last reading

Declaration
public RelativeHumidity? Humidity { get; }

Property Value

Type Description
System.Nullable<RelativeHumidity>

ReadPrecision

Precision of sensor reading

Declaration
public Sht4x.Precision ReadPrecision { get; protected set; }

Property Value

Type Description
Sht4x.Precision

Temperature

The curren temperature -from the last reading.

Declaration
public Units.Temperature? Temperature { get; }

Property Value

Type Description
System.Nullable<Units.Temperature>

Methods

GetDelayForPrecision(Sht4x.Precision)

Returns the appropriate delay in ms for the set precision

Declaration
protected int GetDelayForPrecision(Sht4x.Precision precision)

Parameters

Type Name Description
Sht4x.Precision precision

Precision to calculate delay

Returns

Type Description
System.Int32

RaiseEventsAndNotify(IChangeResult<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>)

Raise events for subscribers

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? Temperature, RelativeHumidity? Humidity)> changeResult)

Parameters

Type Name Description
IChangeResult<System.ValueTuple<System.Nullable<Units.Temperature>, System.Nullable<RelativeHumidity>>> changeResult

ReadSensor()

Get a reading from the sensor and set the Temperature and Humidity properties.

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<Units.Temperature>, System.Nullable<RelativeHumidity>>>.ReadSensor()

Events

HumidityUpdated

Humidity changed event handler

Declaration
public event EventHandler<IChangeResult<RelativeHumidity>> HumidityUpdated

Event Type

Type Description
EventHandler<IChangeResult<RelativeHumidity>>

TemperatureUpdated

Temperature changed event handler

Declaration
public event EventHandler<IChangeResult<Units.Temperature>> TemperatureUpdated

Event Type

Type Description
EventHandler<IChangeResult<Units.Temperature>>