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
Arduino UNO – 1
MQ2/MQ6 – 1
Below is the sample program to read the MQ sensor value:
- #define Gas 8
- void setup()
- void loop()
- Serial.println(“No Gas Detected”);
- Serial.println(“Gas Detected”);
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.