Tutorial Alaram Pendeteksi Gas

Membuat deteksi gas dengan Sensor MQ-2
Untuk memulai siapkan beberap komponen sebagai berikut :
Komponen
| Component | Description |
|---|---|
| Arduino Uno | Microcontroller |
| LCD i2C | Display |
| Buzzer | Output Suara |
| LED | Output Cahaya |
| Resistor 10 K | Penghambar arus |
| Breadboard | Peletakan Component |
| Jumper Cable | Mengubungkan Komponen |
| MQ-2 Sensor | Pendeteksi Gas |
Wiring
Lalu Hubungkan semua komponen tersebut dengan kabel jumper seperti table berikut ini :
| LCD | Arduino |
|---|---|
| VCC | 5V |
| GND | GND |
| SCL | SCL/A5 |
| SD | SDA/A4 |
| MQ-2 | Arduino |
|---|---|
| VCC | 5V |
| GND | GND |
| A0 | A0 |
| LED | Arduino |
|---|---|
| + | D2 |
| - | GND |
| Buzzer | Arduino |
|---|---|
| + | D3 |
| - | GND |
Rangkaian
Rangkaian akan seperti berikuti ini dengan LCD

Code untuk menjalankan
Pastikan sudah terinstall library LiquidCrystal_I2C
mq2-lcd.ino
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define Sensor A1
void setup() {
Serial.begin(9200);
lcd.init();
lcd.backlight();
}
void loop() {
int value = analogRead(Sensor);
lcd.setCursor(0, 0);
lcd.print("Value :");
lcd.print(value);
lcd.print(" ");
if (value > 400) {
Serial.println("GAS Detected!");
lcd.setCursor(0, 1);
lcd.print("GAS Detected!");
} else {
Serial.println("No Gas Detected!");
lcd.setCursor(0, 1);
}
}
Ketika berhasil di jalankan

Update Rangkaian
Tambhakan Buzzer seperti gambar berikut ini

Ganti code sebelumnya dengan code beriut ini
mq2-lcd-buzzer.ino
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define Buzzer 3
#define Sensor A1
void setup() {
Serial.begin(9200);
lcd.init();
lcd.backlight();
pinMode(Buzzer, OUTPUT);
}
void loop() {
int value = analogRead(Sensor);
lcd.setCursor(0, 0);
lcd.print("Value :");
lcd.print(value);
lcd.print(" ");
if (value > 400) {
tone(Buzzer,500);
delay (500);
tone(Buzzer,1000);
delay(500);
lcd.setCursor(0, 1);
lcd.print("GAS Detected!");
} else {
noTone(Buzzer);
lcd.setCursor(0, 1);
lcd.print("No Gas Detected!");
}
}
Jalankan Hasil rangkaian dan update code, apakah buzzer berbunyi ketika gas terdeteksi?
Tambahkan LED
Tambhakan code berikut ini sesuai tempatnya dan tambahakan juga pada rangkaian arduino
Tambahkan LED pada pin 2
mq2-lcd.ino
#define LED 2
void setup() {
....
pinMode(LED, OUTPUT);
void loop() {
....
(value > 400) {
digitalWrite(LED, HIGH);
}
} else {
digitalWrite(LED, LOW);
}
}