# Lập trình cảm biến khí ga/ khói MQ-2 Arduino

Hướng dẫn này chỉ ra cách xây dựng máy dò khói và sẽ phát ra tiếng bíp khi phát hiện khí dễ cháy hoặc khói. Cảm biến khói MQ-2 được hiển thị trong hình sau:\
![](https://making.vn/assets/files/2020-05-04/1588633595-743911-image.png)\
Cảm biến khói MQ-2 nhạy cảm với khói và các loại khí dễ cháy sau: LPG, butan, propan, metan, cồn và hydro. Điện trở trên cảm biến là khác nhau tùy thuộc vào loại khí. Cảm biến khói có chiết áp tích hợp cho phép bạn điều chỉnh ngưỡng đầu ra kỹ thuật số (D0) của cảm biến. Qúa ngưỡng này, đâu ra sẽ có giá trị CAO (HIGH).\
![](https://making.vn/assets/files/2020-05-04/1588633732-212166-image.png)

#### NGUYÊN LÝ LÀM VIỆC

Điện áp mà cảm biến xuất ra thay đổi theo mức khói / khí tồn tại trong khí quyển. Cảm biến xuất ra một điện áp tỷ lệ thuận với nồng độ khói / khí. Nói cách khác, mối quan hệ giữa điện áp và nồng độ khí là:

* Nồng độ khí càng lớn, điện áp đầu ra càng lớn
* Nồng độ khí càng thấp, điện áp đầu ra càng thấp

<figure><img src="https://making.vn/assets/files/2020-05-04/1588633915-711403-image.png" alt=""><figcaption></figcaption></figure>

\
Đầu ra có 2 chân, một là tương tự là giá trị của cảm biến, một là kỹ thuật số đã được so ngưỡng qua chiết áp.\
**Cảm biến khí gas với Arduino**\
Trong ví dụ này, bạn sẽ đọc điện áp đầu ra kiểu tương tự của cảm biến. Khi khói đạt đến một mức nhất định, nó sẽ làm cho âm thanh phát ra và đèn LED màu đỏ sẽ bật. Khi điện áp đầu ra dưới mức đó, đèn LED màu xanh sẽ bật.\ <br>

<figure><img src="https://making.vn/assets/files/2020-05-04/1588634243-929994-image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://making.vn/assets/files/2020-05-04/1588634263-285943-image.png" alt=""><figcaption></figcaption></figure>

* #### Chương trình mẫu

  Bạn có thể đặt lại giá trị ngưỡng cho cảm biến: **sensorThres**

```
// Code mẫu
int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
//Giá trị ngưỡng
int sensorThres = 400;

void setup() {
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
  Serial.begin(9600);
}

void loop() {
  int analogSensor = analogRead(smokeA0);

  Serial.print("Gia tri cam bien - A0: ");
  Serial.println(analogSensor);
  // Kiem tra nếu vượt quá trị ngưỡng sẽ đặt cảnh báo
  if (analogSensor > sensorThres)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    tone(buzzer, 1000, 200);
  }
  else
  {
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
    noTone(buzzer);
  }
  delay(100);
}
```

***

#### #arduino #cảm biến khí ga #mq2 #mp2


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.linhkientot.vn/arduino-products/lap-trinh-cam-bien-khi-ga-khoi-mq-2-arduino.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
