Skip to main content

Meadow.Foundation.Sensors.Atmospheric.Sht31d

Sht31d
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet 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.