Introduction:
MQ 2 or MQ6 is Gas leakage sensor which consists of 4 pins.
This module is used to detect the gases like butane, propane and LPG.
VCC – 5v DC
GND – ground
D0 – Digital out
A0 – Analog out
Requirements:
Arduino UNO – 1
MQ2/MQ6 – 1
Jumper Wires
Working:
Below is the sample program to read the MQ sensor value:
- #define Gas 8
- void setup()
- {
- Serial.begin(9600);
- pinMode(Gas,INPUT);
- }
- void loop()
- {
- if(digitalRead(Gas))
- Serial.println(“No Gas Detected”);
- else
- Serial.println(“Gas Detected”);
- delay(1000);
- }
Firstly declared Gas macro with the digital pin number.
In setup() function configured the serial monitor with 9600 baud rate and initialized the gas pin as input.
In loop() function sensor value is read by digitalRead() function, it will return 1 when gas is not detected if condition goes to true and printing on the console as No gas detected and it will return 0 on gas detected if condition goes false and printing on the console as Gas detected. This function is an iteration function and it will be continuously read from sensor with delay of 1 second.