-
Notifications
You must be signed in to change notification settings - Fork 0
/
BastWAN_HDC1080_CCS811_demo.ino
174 lines (163 loc) · 5.61 KB
/
BastWAN_HDC1080_CCS811_demo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**************************************************************************************
This is an example for HDC1080 Humidity and Temperature Sensor breakout booard
plus CCS811 digital gas sensor packaged as CJMCU-8118
Adapted from ClosedCube_HDC1080 & SparkFunCCS811 libraries
Hardware connections for Arduino Uno:
VDD to 3.3V DC
SCL to SCL
SDA to SDA
GND to common ground
WAK to 5
INT to 6
MIT License
**************************************************************************************/
#include <Wire.h>
#include <ClosedCube_HDC1080.h>
// Click here to get the library: http://librarymanager/All#ClosedCube_HDC1080
#include <SparkFunCCS811.h>
// Click here to get the library: http://librarymanager/All#SparkFun_CCS811
// #define CCS811_ADDR 0x5B // Default I2C Address
#define CCS811_ADDR 0x5A // Alternate I2C Address
// BastWAN_I2C_Scanner found it at 0x5A
#define PIN_NOT_WAKE 5
#define PIN_NOT_INT 6
CCS811 myCCS811(CCS811_ADDR);
ClosedCube_HDC1080 hdc1080;
#define hdc1080_waitout 3000
double t0;
void setup() {
Serial.begin(115200);
Serial.flush();
delay(3000);
Serial.println("\n\nClosedCube HDC1080 CCS811 Test");
Wire.begin(SDA, SCL);
Wire.setClock(100000);
// Default settings:
// - Heater off
// - 14 bit Temperature and Humidity Measurement Resolutions
/*
typedef union {
uint8_t rawData;
struct {
uint8_t HumidityMeasurementResolution : 2;
00: 14 bit
01: 11 bit
10: 8 bit
uint8_t TemperatureMeasurementResolution : 1;
0: 14 bit
1: 11 bit
uint8_t BatteryStatus : 1;
0: Battery voltage > 2.8V (read only)
1: Battery voltage < 2.8V (read only)
uint8_t ModeOfAcquisition : 1;
0: Temp *or* RH
1: Both
uint8_t Heater : 1;
0: Disabled
1: Enabled
uint8_t ReservedAgain : 1;
uint8_t SoftwareReset : 1;
0: Normal Operation, this bit self clears
1: Software Reset
};
} HDC1080_Registers;
https://www.ti.com/lit/ds/symlink/hdc1080.pdf?ts=1610434595877&ref_url=https%253A%252F%252Fwww.google.com%252F
*/
hdc1080.begin(0x40); // I2C address
Serial.print("Manufacturer ID=0x");
Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
Serial.print("Device ID=0x");
Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
printSerialNumber();
HDC1080_Registers reg = hdc1080.readRegister();
Serial.print("Battery: 0x");
Serial.println(reg.BatteryStatus, HEX);
Serial.print("Heater: 0x");
Serial.println(reg.Heater, HEX);
Serial.print("HumidityMeasurementResolution: 0x");
Serial.println(reg.HumidityMeasurementResolution, HEX);
Serial.print("TemperatureMeasurementResolution: 0x");
Serial.println(reg.TemperatureMeasurementResolution, HEX);
// This begins the CCS811 sensor and prints error status of .beginWithStatus()
CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus();
Serial.print("CCS811 begin exited with: ");
// Pass the error code to a function to print the results
Serial.println(myCCS811.statusString(returnCode));
// This sets the mode to 60 second reads, and prints returned error status.
returnCode = myCCS811.setDriveMode(2);
Serial.print("Mode request exited with: ");
Serial.println(myCCS811.statusString(returnCode));
// Configure and enable the interrupt line,
// then print error status
pinMode(PIN_NOT_INT, INPUT_PULLUP);
returnCode = myCCS811.enableInterrupts();
Serial.print("Interrupt configuration exited with: ");
Serial.println(myCCS811.statusString(returnCode));
// Configure the wake line
pinMode(PIN_NOT_WAKE, OUTPUT);
digitalWrite(PIN_NOT_WAKE, 1); // Start asleep
t0 = millis();
}
void loop() {
if (millis() - t0 > hdc1080_waitout) {
Serial.print("T=");
Serial.print(hdc1080.readTemperature());
Serial.print("C, RH=");
Serial.print(hdc1080.readHumidity());
Serial.println("%");
t0 = millis();
}
// Look for interrupt request from CCS811
if (digitalRead(PIN_NOT_INT) == 0) {
// Wake up the CCS811 logic engine
digitalWrite(PIN_NOT_WAKE, 0);
// Need to wait at least 50 µs
delay(1);
// Interrupt signal caught, so cause the CCS811 to run its algorithm
myCCS811.readAlgorithmResults(); // Calling this function updates the global tVOC and CO2 variables
Serial.print("CO2[");
Serial.print(myCCS811.getCO2());
Serial.print("] tVOC[");
Serial.print(myCCS811.getTVOC());
Serial.print("] millis[");
Serial.print(millis());
Serial.print("]");
Serial.println();
// Now put the CCS811's logic engine to sleep
digitalWrite(PIN_NOT_WAKE, 1);
// Need to be asleep for at least 20 µs
delay(1);
}
delay(1); // cycle kinda fast
}
void printSerialNumber() {
Serial.print("Device Serial Number=");
HDC1080_SerialNumber sernum = hdc1080.readSerialNumber();
char format[12];
sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);
Serial.println(format);
}
// printSensorError gets, clears, then prints the errors
// saved within the error register.
void printSensorError() {
uint8_t error = myCCS811.getErrorRegister();
if (error == 0xFF) {
// comm error
Serial.println("Failed to get ERROR_ID register.");
} else {
Serial.print("Error: ");
if (error & 1 << 5)
Serial.print("HeaterSupply");
if (error & 1 << 4)
Serial.print("HeaterFault");
if (error & 1 << 3)
Serial.print("MaxResistance");
if (error & 1 << 2)
Serial.print("MeasModeInvalid");
if (error & 1 << 1)
Serial.print("ReadRegInvalid");
if (error & 1 << 0)
Serial.print("MsgInvalid");
Serial.println();
}
}