How to Code Arduino MQ2 Gas Sensor?

How to Code Arduino MQ2 Gas Sensor?

MQ – 2 is one of the Gas Sensors, is capable of sensing the value of LPG, Propane, Hydrogen, CO, Alcohol, and Methane. 

It is one of the most used sensors in the Arduino project for sensing data from these gas.

So in this project, we are going to use the MQ2 Gas Sensor to fetch the analog value from pin A0.

Step 1:  Setup

This first step if you have not set up Arduino IDE then install and setup. Follow the link to set up.

Step 2: Code

int ledPin = 5;
int sirenPin = 6;
int sensorPin = A0;
int value = 200;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
void loop() {
 int sensor_value  = analogRead(sensorPin);
if(sensor_value > 30)
  digitalWrite(ledPin, HIGH);
  digitalWrite(sirenPin, HIGH);
  digitalWrite(ledPin, LOW);
  digitalWrite(sirenPin, LOW);

Setup 3: Wiring

To develop this project you should need some components,required components are:

  • Arduino Uno
  • MQ2
  • Buzzer
  • LEDs
  • Jumper Wire
  • Breadboard

Copyright © 2020-2021 Engineer Jagat All rights reserved. Developed by Ravindra Rana