Remarks
Sht31d | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
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 =>
{
//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) =>
{
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.
Syntax
public class Sht31d : ByteCommsSensorBase<(Units.Temperature? Temperature, RelativeHumidity? Humidity)>, ISamplingSensor<(Units.Temperature? Temperature, RelativeHumidity? Humidity)>, ISamplingSensor<(Units.Temperature? Temperature, RelativeHumidity? Humidity)>, IDisposable, ITemperatureSensor, IHumiditySensor
Constructors
Sht31d(II2cBus, Byte)
Create a new SHT31D object.
Declaration
public Sht31d(II2cBus i2cBus, byte address = null)
Parameters
Type | Name | Description |
---|---|---|
II2cBus | i2cBus | I2cBus (0-1000 KHz). |
System.Byte | address | Sensor address (should be 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> |
Temperature
The temperature, in degrees celsius (°C), from the last reading.
Declaration
public Units.Temperature? Temperature { get; }
Property Value
Type | Description |
---|---|
System.Nullable<Units.Temperature> |
Methods
RaiseEventsAndNotify(IChangeResult<(Nullable<Units.Temperature> Temperature, Nullable<RelativeHumidity> Humidity)>)
Raise events for subcribers and notify of value changes
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 | The updated sensor data |
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
Events
HumidityUpdated
Humidity changed event
Declaration
public event EventHandler<IChangeResult<RelativeHumidity>> HumidityUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<RelativeHumidity>> |
TemperatureUpdated
Temperature changed event
Declaration
public event EventHandler<IChangeResult<Units.Temperature>> TemperatureUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Units.Temperature>> |