15
15
*/
16
16
17
17
#include " SparkFun_SGP30_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_SGP30
18
- #include " SparkFun_Si7021_Breakout_Library .h" // Click here to get the library: http://librarymanager/All#SparkFun_Si7021
18
+ #include " SparkFun_SHTC3 .h" // Click here to get the library: http://librarymanager/All#SparkFun_SHTC3
19
19
#include < Wire.h>
20
20
21
21
SGP30 mySensor; // create an instance of the SGP30 class
22
- Weather hSensor ; // create an instance of the Weather class
22
+ SHTC3 humiditySensor ; // create an instance of the SHTC3 class
23
23
long t1, t2;
24
24
25
25
byte count = 0 ;
@@ -35,13 +35,15 @@ void setup() {
35
35
}
36
36
37
37
// Initialize the humidity sensor and ping it
38
- hSensor.begin ();
38
+ humiditySensor.begin ();
39
+ // Call "update()" to command a measurement, wait for measurement to complete, and update the RH and T members of the object
40
+ SHTC3_Status_TypeDef result = humiditySensor.update ();
41
+ delay (190 );
42
+ // Measure Relative Humidity from the SHTC3
43
+ float humidity = humiditySensor.toPercent ();
39
44
40
- // Measure Relative Humidity from the Si7021
41
- float humidity = hSensor.getRH ();
42
-
43
- // Measure temperature (in C) from the Si7021
44
- float temperature = hSensor.getTemp ();
45
+ // Measure temperature (in C) from the SHTC3
46
+ float temperature = humiditySensor.toDegC ();
45
47
46
48
// Convert relative humidity to absolute humidity
47
49
double absHumidity = RHtoAbsolute (humidity, temperature);
@@ -56,6 +58,10 @@ void setup() {
56
58
// Set the humidity compensation on the SGP30 to the measured value
57
59
// If no humidity sensor attached, sensHumidity should be 0 and sensor will use default
58
60
mySensor.setHumidity (sensHumidity);
61
+ Serial.print (" Absolute humidity compensation set to: " );
62
+ Serial.print (absHumidity);
63
+ Serial.println (" g/m^3 " );
64
+ delay (100 );
59
65
t1 = millis ();
60
66
}
61
67
@@ -72,6 +78,34 @@ void loop() {
72
78
Serial.print (mySensor.TVOC );
73
79
Serial.println (" ppb" );
74
80
}
81
+ if (Serial.available ()) // check if new data is available on serial port
82
+ {
83
+ char ch = Serial.read ();
84
+ if (ch == ' h' || ch == ' H' ) // check if the char input matches either "h" or "H" and if it does, run the compensation routine from the setup
85
+ {
86
+ SHTC3_Status_TypeDef result = humiditySensor.update ();
87
+ delay (190 );
88
+ // Measure Relative Humidity from the SHTC3
89
+ float humidity = humiditySensor.toPercent ();
90
+
91
+ // Measure temperature (in C) from the SHTC3
92
+ float temperature = humiditySensor.toDegC ();
93
+
94
+ // Convert relative humidity to absolute humidity
95
+ double absHumidity = RHtoAbsolute (humidity, temperature);
96
+
97
+ // Convert the double type humidity to a fixed point 8.8bit number
98
+ uint16_t sensHumidity = doubleToFixedPoint (absHumidity);
99
+
100
+ // Set the humidity compensation on the SGP30 to the measured value
101
+ // If no humidity sensor attached, sensHumidity should be 0 and sensor will use default
102
+ mySensor.setHumidity (sensHumidity);
103
+ Serial.print (" Absolute Humidity Compensation set to: " );
104
+ Serial.print (absHumidity);
105
+ Serial.println (" g/m^3 " );
106
+ delay (100 );
107
+ }
108
+ }
75
109
}
76
110
77
111
double RHtoAbsolute (float relHumidity, float tempC) {
0 commit comments