Characteristic | Locus |
---|---|
Inheritance | System.Object > SHT31D |
Implements | IAtmosphericSensor ITemperatureSensor IHumiditySensor |
Namespace | Meadow.Foundation.Sensors.Atmospheric |
Assembly | SHT31D.dll |
Syntax
public class SHT31D : FilterableObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>, IAtmosphericSensor, ITemperatureSensor, IHumiditySensor
Remarks
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.
Purchasing
The SHT31D temperature and humidity is available on a breakout board from Adafruit:
Examples
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.
using System.Threading;
using Meadow;
using Meadow.Foundation.Sensors.Atmospheric;
namespace SHT31D_Sample
{
public class Program
{
static IApp _app;
public static void Main()
{
_app = new App();
}
}
public class App : AppBase<F7Micro, App>
{
public App ()
{
// 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:
using System.Threading;
using Meadow;
using Meadow.Foundation.Sensors.Atmospheric;
namespace SHT31D_Sample
{
public class Program
{
static IApp _app;
public static void Main()
{
_app = new App();
}
}
public class App : AppBase<F7Micro, App>
{
public App ()
{
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);
}
}
}
}
Example Circuit
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.
Constructors
View SourceSHT31D(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
View SourceConditions
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
View SourceRaiseChangedAndNotify(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
View SourceUpdated
Declaration
public event EventHandler<AtmosphericConditionChangeResult> Updated
Event Type
Type | Description |
---|---|
System.EventHandler<AtmosphericConditionChangeResult> |