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
#include <Wire.h>
#include <Adafruit_AHTX0.h> // AHT20 驅動庫
#include <TFT_eSPI.h> // TTGO T-Display LCD
#include <MQ135.h> // MQ-135 驅動庫

#define SDA_PIN 21
#define SCL_PIN 22
#define MQ135_PIN 27 // MQ-135 ADC 腳位

Adafruit_AHTX0 aht;
TFT_eSPI tft = TFT_eSPI();
MQ135 mq135_sensor(MQ135_PIN);

void setup() {
// 初始化螢幕
tft.init();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE, TFT_BLACK);

// 初始化 I2C
Wire.begin(SDA_PIN, SCL_PIN);

// 初始化 AHT20
while (!aht.begin()) {
tft.fillScreen(TFT_BLACK);
tft.setCursor(10, 30);
tft.println("AHT20 INIT FAIL");
delay(5000);
}

tft.fillScreen(TFT_BLACK);
tft.setCursor(10, 30);
tft.println("AHT20 OK");
delay(2000);
}

void loop() {
// 讀取 AHT20 溫濕度
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);
float temperature = temp.temperature;
float humi = humidity.relative_humidity;

// 取得 MQ-135 已校正的 PPM
float correctedPPM = mq135_sensor.getCorrectedPPM(temperature, humi);

// 顯示在 LCD
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0);
tft.printf("Air Quality\nPPM: %.1f\n", correctedPPM);

delay(2000); // 每2秒更新
}