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 | #include <TFT_eSPI.h> #define MQ2_PIN 12 #define MQ135_PIN 27 TFT_eSPI tft = TFT_eSPI(); void setup() { Serial.begin(115200); // 初始化螢幕 tft.init(); tft.setRotation(1); tft.fillScreen(TFT_BLACK); tft.setTextSize(1); tft.setTextColor(TFT_WHITE); // 設定 ADC 解析度 analogReadResolution(12); // 12-bit (0-4095) Serial.println("=== 氣體感測器除錯測試 ==="); Serial.println("檢查 MQ-2 (GPIO12) 和 MQ-135 (GPIO27)"); } void loop() { // 讀取原始 ADC 值 int mq2_raw = analogRead(MQ2_PIN); int mq135_raw = analogRead(MQ135_PIN); // 計算電壓 float mq2_voltage = mq2_raw * (3.3 / 4095.0); float mq135_voltage = mq135_raw * (3.3 / 4095.0); // 序列埠輸出 Serial.printf("MQ-2 ADC: %4d, 電壓: %.2fV\n", mq2_raw, mq2_voltage); Serial.printf("MQ-135 ADC: %4d, 電壓: %.2fV\n", mq135_raw, mq135_voltage); Serial.println("---"); // 螢幕顯示 tft.fillScreen(TFT_BLACK); tft.setCursor(10, 10); tft.setTextColor(TFT_CYAN); tft.println("氣體感測器除錯測試"); // MQ-2 測試 tft.setCursor(10, 35); tft.setTextColor(TFT_YELLOW); tft.println("MQ-2 煙霧感測器 (GPIO 12):"); tft.setCursor(10, 50); if (mq2_raw > 10) { tft.setTextColor(TFT_GREEN); tft.printf("✓ ADC: %d (%.2fV)", mq2_raw, mq2_voltage); } else { tft.setTextColor(TFT_RED); tft.printf("✗ 無訊號 ADC: %d", mq2_raw); } // MQ-135 測試 tft.setCursor(10, 75); tft.setTextColor(TFT_YELLOW); tft.println("MQ-135 空氣品質 (GPIO 27):"); tft.setCursor(10, 90); if (mq135_raw > 10) { tft.setTextColor(TFT_GREEN); tft.printf("✓ ADC: %d (%.2fV)", mq135_raw, mq135_voltage); } else { tft.setTextColor(TFT_RED); tft.printf("✗ 無訊號 ADC: %d", mq135_raw); } // 除錯提示 tft.setCursor(10, 115); tft.setTextColor(TFT_WHITE); tft.setTextSize(1); if (mq2_raw == 0 && mq135_raw == 0) { tft.println("檢查: 1.接線 2.供電 3.感測器"); } else if (mq2_raw == 0) { tft.println("MQ-2 連接問題"); } else if (mq135_raw == 0) { tft.println("MQ-135 連接問題 (需要5V供電)"); } else { tft.println("感測器正常運作!"); } delay(1000); } |
Direct link: https://paste.plurk.com/show/WjDNMYxNFEIBCntotsy0