Skip to content

Commit 1433e51

Browse files
authored
Create Basic.ino
1 parent 9d677db commit 1433e51

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

examples/Basic/Basic.ino

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/***************************************************************
2+
* @file Basic.ino
3+
* @brief Minimal demo for the 7Semi CO2TH I²C driver.
4+
*
5+
* Features demonstrated:
6+
* - Auto-detect I²C address (Begin with address = 0; scans 0x08..0x77)
7+
* - Start continuous measurement
8+
* - Read and print CO₂ (ppm), Temperature (°C), Humidity (%RH), Status (u16)
9+
*
10+
* Requirements:
11+
* - Arduino core with Wire
12+
* - 7Semi driver files: 7Semi_CO2TH.h / 7Semi_CO2TH.cpp
13+
*
14+
* Connections (typical):
15+
* - SDA -> board SDA
16+
* - SCL -> board SCL
17+
* - VDD -> 3.3V / 5V
18+
* - GND -> GND
19+
*
20+
* Author : 7Semi
21+
* License : MIT
22+
* Version : 1.0
23+
* Date : 04 October 2025
24+
***************************************************************/
25+
26+
#include <7Semi_CO2TH.h>
27+
28+
/** - One driver instance */
29+
CO2TH_7Semi CO2TH;
30+
31+
/** - Read period (ms): match sensor frame interval */
32+
static const unsigned long kPeriodMs = 1200;
33+
34+
void setup() {
35+
/** - Serial + I2C */
36+
Serial.begin(115200);
37+
while (!Serial)
38+
;
39+
/** - Init driver (0 = auto-detect address 0x08..0x77) */
40+
// Ardiuno UNO
41+
// err_t e = CO2TH.Begin(Wire, 0x64);
42+
/**
43+
- Initialize I²C and the driver.
44+
- If you *know* the address, pass it (e.g., 0x64). If unknown, pass 0 to auto-scan 0x08..0x77.
45+
*/
46+
err_t e = CO2TH.Begin(/*addr*/ 0x64, /*sda*/ 21, /*scl*/ 22, /*freq*/ 400000, /*port*/ 0);
47+
if (e != NO_ERROR) {
48+
Serial.print(F("Begin failed, err="));
49+
Serial.println((int)e);
50+
while (true) { delay(1000); }
51+
}
52+
53+
/** - Start continuous measurement */
54+
e = CO2TH.StartContinuousMeasurement();
55+
if (e != NO_ERROR) {
56+
Serial.print(F("Start failed, err="));
57+
Serial.println((int)e);
58+
while (true) { delay(1000); }
59+
}
60+
61+
/** - Give the sensor one frame to produce the first result */
62+
delay(kPeriodMs);
63+
}
64+
65+
void loop() {
66+
static unsigned long last = 0;
67+
const unsigned long now = millis();
68+
if (now - last < kPeriodMs) return;
69+
last = now;
70+
71+
/** - Read and print values */
72+
int16_t co2 = 0;
73+
float temp = 0.0f;
74+
float rh = 0.0f;
75+
uint16_t st = 0;
76+
77+
err_t e = CO2TH.ReadMeasurement(co2, temp, rh, st);
78+
if (e != NO_ERROR) {
79+
Serial.print(F("Read failed, err="));
80+
Serial.println((int)e);
81+
return;
82+
}
83+
84+
Serial.print(F("CO2 = "));
85+
Serial.print(co2);
86+
Serial.print(F(" ppm, T = "));
87+
Serial.print(temp, 2);
88+
Serial.print(F(" °C, RH = "));
89+
Serial.println(rh, 2);
90+
}

0 commit comments

Comments
 (0)