เครื่องเตือนแก๊สและควันด้วย ESP32 และ MQ-2
โปรเจกต์นี้ใช้โมดูลเซ็นเซอร์ MQ-2 ตรวจจับแก๊สไวไฟ (LPG มีเทน และโพรเพน) กับควัน แล้วส่งเสียงไซเรนผ่านบัซเซอร์แบบพาสซีฟ ต่อขาเอาต์พุตดิจิทัล (DO) ของเซ็นเซอร์เข้ากับ GPIO 4 ของ ESP32 เมื่อระดับแก๊สหรือควันเกินเกณฑ์ DO จะเป็น LOW และส่งสัญญาณเตือน ปรับความไวด้วยโพเทนชิโอมิเตอร์บนโมดูล และใช้ tone() ขับบัซเซอร์สลับ 2 ความถี่ให้เกิดเสียงไซเรน

วิดีโอ
แผนผังวงจร

ซอร์สโค้ด
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// ESP32 MQ-2 Gas/Smoke Detector Alarm
4// ─────────────────────────────────────────
5// Reads the digital threshold output (DO) of the MQ-2 module. When gas or
6// smoke passes the threshold, the warning LED turns on and the passive
7// buzzer plays a siren switching between 800 and 1200 Hz every 0.2 s. Set
8// the threshold with the trimpot on the module (Arduino Core 3.0+).
9//
10// Pins:
11// GPIO 4 -> MQ-2 DO (digital threshold output)
12// GPIO 18 -> Buzzer + leg
13// GPIO 5 -> Red LED (with a 220 ohm resistor)
14// MQ-2 VCC -> MQ-2 VCC -> 5V (VIN), GND -> GND
15
16// Pin definitions
17const int MQ2_DO_PIN = 4; // MQ-2 D0 (digital threshold out)
18const int BUZZER_PIN = 18; // Passive buzzer
19const int LED_PIN = 5; // Warning LED + 220R
20
21// Warm-up time (ms): the MQ-2 heater needs minutes to warm up. In Wokwi 2000 is enough.
22const unsigned long WARMUP_MS = 60000;
23bool warmedUp = false; // Is the warm-up over
24
25void setup() {
26 pinMode(MQ2_DO_PIN, INPUT);
27 pinMode(BUZZER_PIN, OUTPUT);
28 pinMode(LED_PIN, OUTPUT);
29 digitalWrite(BUZZER_PIN, LOW);
30 digitalWrite(LED_PIN, LOW);
31}
32
33void loop() {
34 // Warm-up: the DO reading is ignored, the LED blinks slowly without blocking
35 if (!warmedUp) {
36 if (millis() < WARMUP_MS) {
37 digitalWrite(LED_PIN, (millis() / 500) % 2); // 0.5 s on, 0.5 s off
38 return;
39 }
40 warmedUp = true;
41 }
42
43 // MQ-2 D0 is normally HIGH and goes LOW above the threshold
44 int gasState = digitalRead(MQ2_DO_PIN);
45
46 if (gasState == LOW) {
47 // Alarm: the LED turns on, the buzzer plays a two-tone siren
48 digitalWrite(LED_PIN, HIGH);
49 tone(BUZZER_PIN, 800); // Low tone
50 delay(200);
51 tone(BUZZER_PIN, 1200); // High tone
52 delay(200);
53 } else {
54 // Idle: LED and buzzer off
55 digitalWrite(LED_PIN, LOW);
56 noTone(BUZZER_PIN);
57 delay(50);
58 }
59}
60
FAQ
MQ-2 ตรวจจับแก๊สอะไรได้บ้าง
MQ-2 ตอบสนองต่อ LPG โพรเพน มีเทน ไฮโดรเจน ไอแอลกอฮอล์ และควัน แต่แยกชนิดแก๊สไม่ได้ ตรวจจับได้เพียงความเข้มข้นโดยรวมที่เพิ่มขึ้น
ต่อเอาต์พุตแอนะล็อกของ MQ-2 เข้ากับ ESP32 โดยตรงได้ไหม
MQ-2 ใช้ไฟเลี้ยง 5 V และเอาต์พุตแอนะล็อกอาจสูงถึง 5 V แต่ขาอินพุต ADC ของ ESP32 รับได้สูงสุด 3.3 V จึงต้องใช้วงจรแบ่งแรงดัน (เช่น 10 kΩ + 20 kΩ)
ทำไมเซ็นเซอร์อ่านค่าได้สูงตอนเพิ่งเปิดเครื่อง
ฮีตเตอร์ของเซ็นเซอร์ตระกูล MQ ใช้เวลาสักครู่กว่าจะคงที่ ระหว่างนี้ค่าที่อ่านได้จะสูง การปิดระบบเตือนไว้ก่อนสักสองสามนาทีหลังเปิดเครื่องจะช่วยป้องกันการเตือนผิดพลาด
สร้างเสียงไซเรนอย่างไร
ใช้ `tone()` ขับบัซเซอร์แบบพาสซีฟให้สลับระหว่าง 2 ความถี่อย่างรวดเร็ว (เช่น 800 Hz และ 1200 Hz) การสลับนี้เลียนแบบเสียงสัญญาณเตือนที่คุ้นเคย
