Remarks
SHT31D | |
---|---|
Status | |
Source code | 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
public class MeadowApp : App<F7Micro, MeadowApp>
{
SHT31D sensor;
public MeadowApp()
{
sensor = new SHT31D(Device.CreateI2cBus());
sensor.Updated += Sensor_Updated;
sensor.StartUpdating();
}
void Sensor_Updated(object sender, Meadow.Peripherals.Sensors.Atmospheric.AtmosphericConditionChangeResult e)
{
Console.WriteLine($"Temp: {e.New.Temperature}, Humidity: {e.New.Humidity}");
}
}
Sample projects available on GitHub
Purchasing
The SHT31D temperature and humidity is available on a breakout board from Adafruit:
The SHT31D can operate in interrupt or polling mode. The default mode is interrupt mode.
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 | System.Object FilterableChangeObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions> > Sht31D |
Implements | System.IObservable<AtmosphericConditionChangeResult> IAtmosphericSensor ITemperatureSensor IHumiditySensor |
Inherited Members | FilterableChangeObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>._observers FilterableChangeObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>.NotifyObservers(AtmosphericConditionChangeResult) FilterableChangeObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>.Subscribe(IObserver<AtmosphericConditionChangeResult>) System.Object.ToString() System.Object.Equals(System.Object) System.Object.Equals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) System.Object.GetHashCode() System.Object.GetType() System.Object.MemberwiseClone() |
Namespace | Meadow.Foundation.Sensors.Atmospheric |
Assembly | Sht31D.dll |
Syntax
public class Sht31D : FilterableChangeObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>, IObservable<AtmosphericConditionChangeResult>, IAtmosphericSensor, ITemperatureSensor, IHumiditySensor
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). |
System.Byte | address | Sensor address (should be 0x44 or 0x45). |
Properties
Conditions
The AtmosphericConditions from the last reading.
Declaration
public AtmosphericConditions Conditions { get; protected set; }
Property Value
Type | Description |
---|---|
AtmosphericConditions |
Humidity
The humidity, in percent relative humidity, from the last reading..
Declaration
public float Humidity { get; }
Property Value
Type | Description |
---|---|
System.Single |
IsSampling
Gets a value indicating whether the analog input port is currently sampling the ADC. Call StartSampling() to spin up the sampling process.
Declaration
public bool IsSampling { get; protected set; }
Property Value
Type | Description |
---|---|
System.Boolean |
|
Temperature
The temperature, in degrees celsius (°C), from the last reading.
Declaration
public float Temperature { get; }
Property Value
Type | Description |
---|---|
System.Single |
Methods
RaiseChangedAndNotify(AtmosphericConditionChangeResult)
Declaration
protected void RaiseChangedAndNotify(AtmosphericConditionChangeResult changeResult)
Parameters
Type | Name | Description |
---|---|---|
AtmosphericConditionChangeResult | changeResult |
Read()
Convenience method to get the current sensor readings. For frequent reads, use StartSampling() and StopSampling() in conjunction with the SampleBuffer.
Declaration
public Task<AtmosphericConditions> Read()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<AtmosphericConditions> |
StartUpdating(Int32)
Declaration
public void StartUpdating(int standbyDuration = 1000)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | standbyDuration |
StopUpdating()
Stops sampling the temperature.
Declaration
public void StopUpdating()
Update()
Get a reading from the sensor and set the Temperature and Humidity properties.
Declaration
public void Update()
Events
Updated
Declaration
public event EventHandler<AtmosphericConditionChangeResult> Updated
Event Type
Type | Description |
---|---|
System.EventHandler<AtmosphericConditionChangeResult> |