Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.

Characteristic Locus
Inheritance object ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)> SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)> PollingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)> ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)> > Sht31d
Implements 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> II2cPeripheral
Inherited Members ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Init(int, int) ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Dispose(bool) ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Dispose() ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.BusComms ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.ReadBuffer ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.WriteBuffer PollingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.StartUpdating(TimeSpan?) PollingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.StopUpdating() SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.samplingLock SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.ReadSensor() SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.RaiseEventsAndNotify(IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>) SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Read() SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.SamplingTokenSource SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Conditions SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.IsSampling SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.UpdateInterval SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Updated ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.NotifyObservers(IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>) ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Subscribe(IObserver<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>>) ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.CreateObserver(Action<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>>, Predicate<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>>) ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.observers object.Equals(object) object.Equals(object, object) object.GetHashCode() object.GetType() object.MemberwiseClone() object.ReferenceEquals(object, object) object.ToString()
Namespace Meadow.Foundation.Sensors.Atmospheric
Assembly Sht31d.dll

Syntax

public class Sht31d : ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>, 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>, II2cPeripheral

Constructors

Sht31d(II2cBus, byte)

Create a new SHT31D object

Declaration
public Sht31d(II2cBus i2cBus, byte address = 68)

Parameters

Type Name Description
II2cBus i2cBus

I2cBus (0-1000 KHz).

byte address

Sensor address (should be 0x44 or 0x45)

Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.

Properties

DefaultI2cAddress

The default I2C address for the peripheral

Declaration
public byte DefaultI2cAddress { get; }

Property Value

Type Description
byte

Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.

Humidity

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

Declaration
public RelativeHumidity? Humidity { get; }

Property Value

Type Description
RelativeHumidity?

Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.

Temperature

The temperature from the last reading

Declaration
public Temperature? Temperature { get; }

Property Value

Type Description
Temperature?

Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.

Methods

RaiseEventsAndNotify(IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>)

Raise events for subscribers and notify of value changes

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

Parameters

Type Name Description
IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)> changeResult

The updated sensor data

Overrides

Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.

ReadSensor()

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

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

Returns

Type Description
Task<(Temperature? Temperature, RelativeHumidity? Humidity)>

Overrides

Meadow.Foundation.SamplingSensorBase<(Meadow.Units.Temperature? Temperature, Meadow.Units.RelativeHumidity? Humidity)>.ReadSensor()

Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.

Events

HumidityUpdated

Humidity changed event

Declaration
public event EventHandler<IChangeResult<RelativeHumidity>> HumidityUpdated

Event Type

Type Description
EventHandler<IChangeResult<RelativeHumidity>>

Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.

TemperatureUpdated

Temperature changed event

Declaration
public event EventHandler<IChangeResult<Temperature>> TemperatureUpdated

Event Type

Type Description
EventHandler<IChangeResult<Temperature>>

Remarks

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

The SHT31D is a temperature and humidity sensor with a built in I2C interface. The sensor has a typical accuracy of +/- 2% relative humidity and +/- 0.3C.

Code Example

Sht31d sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

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

    var consumer = Sht31d.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 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) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N2}%");
    };

    return Task.CompletedTask;
}

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

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

Sample project(s) available on GitHub

Interrupt Mode

The application below generates and interrupt when the temperature or humidity changes by more than 0.1 °C. The sensor is checked every 100 milliseconds.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // Create a new SHT31D object that will generate interrupts when
        // the temperature changes by more than +/- 0.1C or the humidity
        // changes by more than 1%.
        SHT31D sht31d = new SHT31D(temperatureChangeNotificationThreshold: 0.1F,
            humidityChangeNotificationThreshold: 1.0F);

        // Hook up the two interrupt handlers to display the changes in
        // temperature and humidity.
        sht31d.HumidityChanged += (s, e) =>
        {
            Console.WriteLine("Current humidity: " + e.CurrentValue.ToString("f2"));
        };

        sht31d.TemperatureChanged += (s, e) =>
        {
            Console.WriteLine("Current temperature: " + e.CurrentValue.ToString("f2"));
        };

        // Main program loop can now go to sleep as the work
        // is being performed by the interrupt handlers.
        Thread.Sleep(Timeout.Infinite);
    }
}

Polling Mode

The application below polls the sensor every 1000 milliseconds and displays the temperature and humidity on the debug console:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        SHT31D sht31d = new SHT31D(updateInterval: 0);

        Console.WriteLine("SHT31D Temperature / Humidity Test");

        while (true)
        {
            sht31d.Update();
            Console.WriteLine("Temperature: " + sht31d.Temperature.ToString("f2") + ", Humidity: " + sht31d.Humidity.ToString("f2"));
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

The SHT31D breakout board from Adafruit is supplied with pull-up resistors installed on the SCL and SDA lines.

The ADR line is tied low giving and I2C address of 0x44. This address line can also be tied high and in this case the I2C address is 0x45.