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 | #include <Wire.h> #include <Adafruit_AHTX0.h> // AHT20/AHT10 驅動庫 #include <TFT_eSPI.h> // TTGO T-Display LCD // 定義 I2C 腳位,避免與 RGB 衝突 (25/26/27) // TTGO T-Display 預設 I2C 腳位 (SDA=21, SCL=22) #define SDA_PIN 21 #define SCL_PIN 22 Adafruit_AHTX0 aht; // AHT20 物件 TFT_eSPI tft = TFT_eSPI(); // LCD 物件 void setup() { // 初始化 LCD 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 if (!aht.begin()) { tft.println("AHT20 不存在"); while (1) delay(10); } tft.println("AHT20 OK"); delay(2000); tft.fillScreen(TFT_BLACK); // 清除螢幕 } void loop() { // 讀取溫度與濕度 sensors_event_t humidity, temp; aht.getEvent(&humidity, &temp); float temperature = temp.temperature; float humi = humidity.relative_humidity; // 在 LCD 顯示 tft.fillScreen(TFT_BLACK); tft.setCursor(10, 30); tft.print("Temp: "); tft.print(temperature, 1); tft.println(" C"); tft.setCursor(10, 70); tft.print("Humi: "); tft.print(humi, 1); tft.println(" %"); delay(2000); // 每2秒更新一次 } |
Direct link: https://paste.plurk.com/show/3jGarWr9hBb8hqOd5A5q