Home ARDUINO Build Gas sensing system with Arduino, 16X2 LCD and MQ2

Build Gas sensing system with Arduino, 16X2 LCD and MQ2

The MQ2 sensor is a gas module for detecting hydrogen, liquefied petroleum gas, smoke, carbon monoxide and alcohol. It has high sensitivity and fast response.

The MQ2 sensor is a gas module for detecting hydrogen, liquefied petroleum gas, smoke, carbon monoxide and alcohol.

It has high sensitivity and fast response time to measure and acquire data. The potentiometer behind it can adjust sensitivity.

Build Gas sensing system with Arduino, 16X2 LCD and MQ2

What Will I Learn?💁 show

What is the MQ-2 smoke sensor?

The MQ-2 smoke sensor is sensitive to smoke and the following flammable gases:

  • LPG
  • Butane
  • Propane
  • Methane
  • alcohol
  • hydrogen

The resistance of the sensor varies depending on the type of gas.

How does it work?

The voltage output by the sensor varies corresponding to the level of smoke/gas present in the atmosphere. The voltage output by the sensor is proportional to the concentration of smoke/gas.

In other words, the relationship between voltage and gas concentration is as follows:

  • The greater the gas concentration, the larger the output voltage

Low gas concentration and low output voltage

Build Gas sensing system with Arduino, 16X2 LCD and MQ2

The output can be an analog signal (A0) that can be read using the Arduino’s analog input or digital output (D0) and can be read using the Arduino’s digital inputs. Calculate the accuracy of the gas you want to detect.

Things used in this project

Hardware components

Build Gas sensing system with Arduino, 16X2 LCD and MQ2

Build Gas sensing system with Arduino, 16X2 LCD and MQ2

Build Gas sensing system with Arduino, 16X2 LCD and MQ2

Software apps and online services Requirements

 Connections

Build Gas sensing system with Arduino, 16X2 LCD and MQ2

Build Gas sensing system with Arduino, 16X2 LCD and MQ2

Arduino              MQ2

  • AO                       A0
  • 5V                     VCC
  • GND                   GND

Upload source

 

#include <MQ2.h>

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

//I2C pins declaration

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int Analog_Input = A0;

int lpg, co, smoke;

MQ2 mq2(Analog_Input);

void setup(){

Serial.begin(9600);

lcd.begin(16,2);//Defining 16 columns and 2 rows of lcd display

lcd.backlight();

mq2.begin();

}

void loop(){

float* values= mq2.read(true); //set it false if you don’t want to print the values in the Serial

//lpg = values[0];

lpg = mq2.readLPG();

//co = values[1];

co = mq2.readCO();

//smoke = values[2];

smoke = mq2.readSmoke();

lcd.setCursor(0,0);

lcd.print(“LPG:”);

lcd.print(lpg);

lcd.print(” CO:”);

lcd.print(co);

lcd.setCursor(0,1);

lcd.print(“SMOKE:”);

lcd.print(smoke);

lcd.print(” PPM”);

delay(1000);

}

Gas Sensor Arduino Code

Here is a basic Arduino code for a gas sensor:-

int sensorPin = A0; // analog input pin for gas sensor
int threshold = 400; // threshold value for detecting gas

void setup() {
Serial.begin(9600); // initialize serial communication at 9600 bits per second
}

void loop() {
int sensorValue = analogRead(sensorPin); // read analog input from sensor
Serial.print(“Gas level: “);
Serial.println(sensorValue); // print sensor value to serial monitor
if (sensorValue > threshold) { // check if sensor value exceeds threshold
Serial.println(“Gas detected!”); // print message if gas is detected
}
delay(1000); // delay for one second before taking the next reading
}

In this code, we first declare a variable sensorPin to represent the analog input pin where the gas sensor is connected to on the Arduino board. We also set a threshold value, which is the value that the gas sensor reading must exceed in order to detect gas.

In the setup() function, we initialize the serial communication with a baud rate of 9600 bits per second.

In the loop() function, we read the analog input from the gas sensor using the analogRead() function and store the value in the sensorValue variable. We then print the value of sensorValue to the serial monitor using the Serial.print() and Serial.println() functions.

Next, we check if sensorValue is greater than the threshold value. If it is, we print a message to the serial monitor indicating that gas has been detected.

Finally, we add a delay of one second using the delay() function before taking the next reading. This delay is added to prevent the sensor from taking too many readings too quickly, which could overload the Arduino board.

Here’s an advanced code for gas sensor that includes calibration and data smoothing:-

const int sensorPin = A0; // analog input pin for gas sensor
const int numReadings = 10; // number of readings to take for smoothing
int readings[numReadings]; // array to store sensor readings
int index = 0; // index of current reading
int total = 0; // sum of sensor readings
int average = 0; // average of sensor readings
int threshold = 400; // threshold value for detecting gas

void setup() {
Serial.begin(9600); // initialize serial communication at 9600 bits per second
pinMode(sensorPin, INPUT); // set sensor pin as input
calibrateSensor(); // calibrate sensor for baseline reading
}

void loop() {
int sensorValue = getSensorReading(); // get smoothed sensor reading
Serial.print(“Gas level: “);
Serial.println(sensorValue); // print sensor value to serial monitor
if (sensorValue > threshold) { // check if sensor value exceeds threshold
Serial.println(“Gas detected!”); // print message if gas is detected
}
delay(1000); // delay for one second before taking the next reading
}

void calibrateSensor() {
Serial.println(“Calibrating sensor…”);
for (int i = 0; i < numReadings; i++) {
readings[i] = analogRead(sensorPin); // take sensor reading
total += readings[i]; // add reading to total sum
delay(100); // delay for 100 milliseconds between readings
}
average = total / numReadings; // calculate average reading
Serial.print(“Baseline reading: “);
Serial.println(average); // print baseline reading to serial monitor
}

int getSensorReading() {
total = total – readings[index]; // subtract oldest reading from total sum
readings[index] = analogRead(sensorPin); // take new sensor reading
total = total + readings[index]; // add new reading to total sum
index = (index + 1) % numReadings; // increment index, wrap around to 0 if necessary
average = total / numReadings; // calculate new average reading
return average; // return smoothed sensor reading
}

In this code, we first declare the constants sensorPin and numReadings, which represent the analog input pin where the gas sensor is connected and the number of readings to take for smoothing, respectively. We also declare the arrays readings to store the sensor readings and initialize the variables index, total, average, and threshold.

In the setup() function, we initialize the serial communication with a baud rate of 9600 bits per second, set the sensorPin as input, and call the calibrateSensor() function to calibrate the sensor for a baseline reading.

The calibrateSensor() function takes numReadings sensor readings at 100 millisecond intervals, adds them to total, and calculates the average reading. It then prints the average reading to the serial monitor as the baseline reading.

In the loop() function, we call the getSensorReading() function to get the smoothed sensor reading, print it to the serial monitor, and check if it exceeds the threshold value. If it does, we print a message to the serial monitor indicating that gas has been detected.

The getSensorReading() function takes a new sensor reading, subtracts the oldest reading from total, adds the new reading to total, calculates the new average reading, and returns it as the smoothed sensor reading.

By using an array to store multiple sensor readings and calculating the average, we can reduce the effect of noise and fluctuations in the sensor readings. This is known as data smoothing and is often used to improve the accuracy of sensor readings.

The index variable is used to keep track of the current position in the readings array. The % operator is used to wrap the index around to 0 when it reaches numReadings, so that the array acts like a circular buffer.

The delay() function is used to add a pause between sensor readings. This prevents the sensor from taking too many readings too quickly, which could overload the Arduino board or cause inaccurate readings.

The calibrateSensor() function is called only once in the setup() function, and is used to get a baseline reading before taking any actual sensor readings. This is necessary because gas sensors can give different readings based on temperature and humidity, and may require some calibration to get accurate readings.

Overall, this advanced code for gas sensor provides better accuracy and reliability by incorporating calibration and data smoothing techniques.

📗FAQ’s

How to program gas sensor with Arduino?

To program a gas sensor with Arduino, you first need to select the appropriate gas sensor according to your requirements. Once you have selected the sensor, you can connect it to the Arduino board using appropriate wiring.

After that, you can write the code in Arduino IDE to read the sensor output and process it to get the desired results. You can find many tutorials and example codes online to help you program gas sensors with Arduino.

How does a gas detector work with Arduino?

A gas detector with Arduino uses a gas sensor to detect the presence of a gas. The gas sensor outputs an analog or digital signal, which is then processed by the Arduino board to trigger an alert or activate a system.

The gas detector can be programmed to perform various actions depending on the specific application.

What type of gas sensor does Arduino use?

Arduino can work with various types of gas sensors, including MQ series, electrochemical, infrared, and others. The choice of sensor depends on the specific gas that needs to be detected and the sensitivity and accuracy required.

What is the difference between MQ2 and MQ5 gas sensor?

The main difference between the MQ2 and MQ5 gas sensors is the gas they are designed to detect. MQ2 is primarily used for detecting smoke, LPG, propane, methane, alcohol, and hydrogen, while MQ5 is used for detecting natural gas, LPG, and coal gas.

Are gas sensors analog or digital?

Gas sensors can be either analog or digital. Analog sensors output a voltage or current proportional to the gas concentration, while digital sensors output a signal indicating the gas’s presence or absence.

How does MQ 2 gas sensor work?

MQ2 gas sensor works by detecting changes in the conductivity of the metal oxide semiconductor (MOS) when it comes in contact with a gas.

The gas molecules are adsorbed on the MOS, which changes the conductivity of the MOS, resulting in a change in resistance. This change is then converted to a signal that the Arduino board can read.

How much does a gas leakage detector cost with Arduino?

The cost of a gas leakage detector with Arduino depends on various factors, such as the gas sensor’s type and sensitivity, the circuit’s complexity, and the other components’ cost.

A basic gas leakage detector can cost as little as $10, while more advanced systems can cost several hundred dollars.

What is the difference between gas monitor and detector?

A gas monitor is a device that continuously monitors the concentration of a gas in a specific environment, while a gas detector is a device that detects the presence of a gas in the environment and triggers an alarm or activates a system.

What are the advantages of gas leakage detector using Arduino?

The advantages of a gas leakage detector using Arduino include low cost, flexibility, and ease of programming. Arduino-based systems can be easily customized to suit specific requirements, and the code can be modified to add or remove features as needed.

What is the most popular gas sensor?

The MQ series gas sensors are some of Arduino’s most popular gas sensors. MQ2 and MQ5 are this series’ most widely used sensors due to their versatility and low cost.

What sensors detect gas?

Various gas sensors can detect different types of gases, including MQ series sensors, electrochemical sensors, infrared sensors, and others. The choice of sensor depends on the specific gas that needs to be detected and the sensitivity and accuracy required.

What is the most accurate sensor Arduino?

The most accurate sensor for Arduino depends on the specific application. Some sensors, such as electrochemical sensors, are highly accurate but also more expensive. Other sensors, such as MQ series sensors, are less accurate but more affordable.

How far can MQ2 gas sensor detect?

The detection range of MQ2 gas sensor depends on various factors, such as the type of gas being detected, the concentration of the gas, and the environment. In general, the detection range of MQ2 gas sensor can vary from a few centimeters to several meters.

What is an alternative to MQ2 gas sensor?

There are various alternatives to MQ2 gas sensor, including MQ5, MQ7, and MQ9 gas sensors. These sensors are designed to detect different types of gases and offer varying levels of sensitivity and accuracy.

Which type of gas sensor is best?

The best type of gas sensor depends on the specific gas that needs to be detected and the sensitivity and accuracy required. MQ series sensors are a popular and affordable option, while electrochemical sensors offer high accuracy but are more expensive.

Can gas sensors sense CO2?

Yes, gas sensors can sense CO2 using infrared sensors or electrochemical sensors. These sensors detect changes in the concentration of CO2 in the air and output a signal that the Arduino board can process.

Is gas sensor active or passive?

Gas sensors can be either active or passive. Active sensors require an external power source to operate, while passive sensors do not require any power and rely on natural phenomena, such as changes in light or temperature, to detect the gas.

Is gas sensor input or output?

Gas sensors can be either input or output devices. They are input devices used to detect the presence of a gas and output a signal that triggers an action, such as an alarm or the activation of a system.

They are output devices when they are used to measure the concentration of a gas and output a signal that is used for data logging or analysis.

Can MQ2 detect carbon monoxide?

Yes, MQ2 gas sensor can detect carbon monoxide (CO) and other gases such as LPG, propane, methane, alcohol, and hydrogen. However, the accuracy and sensitivity of the detection may vary depending on the concentration of the gas and the environment.

Which is best MQ gas sensor?

The best MQ gas sensor depends on the specific gas that needs to be detected and the sensitivity and accuracy required. MQ2 and MQ5 are popular choices due to their versatility and affordability, while MQ7 and MQ9 are designed for specific gases and offer higher sensitivity and accuracy.

Can MQ2 detect methane?

Yes, MQ2 gas sensor can detect methane along with other gases such as LPG, propane, alcohol, hydrogen, and smoke. However, the accuracy and sensitivity of the detection may vary depending on the concentration of the gas and the environment.

Is there a gadget to detect gas leak?

Yes, there are various gadgets available to detect gas leaks, including gas sensors, gas detectors, and gas monitors. These devices can be used in combination with Arduino to build custom gas detection systems.

What sensor detects gas leakage?

Gas sensors and gas detectors can detect gas leakage. These devices use various sensing technologies, such as electrochemical, infrared, or metal oxide semiconductor, to detect the presence of gas and trigger an alarm or activate a system.

What is the disadvantage of gas monitor?

The main disadvantage of gas monitors is their cost, which can be higher than gas sensors or detectors. Gas monitors are also more complex and require calibration and maintenance, which can add to the cost and complexity of the system.

What 4 gases are measured by gas monitor?

Gas monitors can measure various gases, depending on the specific application. Common gases measured by gas monitors include oxygen, carbon monoxide, hydrogen sulfide, and combustible gases such as methane and propane.

Is it worth getting a gas detector?

Yes, it is worth getting a gas detector if you work with or are exposed to gases that can be harmful or flammable.

A gas detector can alert you to a gas leak, allowing you to take appropriate action and prevent accidents or health hazards. Gas detectors are commonly used in industrial settings, laboratories, and homes with gas appliances.

How to make gas leak alert security alarm using Arduino?

To make a gas leak alert security alarm using Arduino, you will need a gas sensor, Arduino board, buzzer, and LED. Connect the gas sensor to the Arduino board and write the code to read the sensor output.

Set a threshold value for the gas concentration and trigger the buzzer and LED if the concentration exceeds the threshold. You can customize the code to include additional features like data logging or remote alerts.

How does mq135 gas sensor work?

MQ135 gas sensor works by detecting changes in the electrical conductivity of the sensing material when it comes in contact with the gas.

The gas molecules are adsorbed on the surface of the sensing material, which changes the conductivity of the material and results in a change in resistance. The change in resistance is then converted to a signal that the Arduino board can read.

How well do gas leak detectors work?

Gas leak detectors work well when used properly and maintained regularly. The accuracy and reliability of the detector depending on various factors, such as the type of gas being detected, the sensitivity and accuracy of the sensor, and the environment.

Regular calibration and maintenance can ensure that the detector works well and provides accurate results.

What are 3 applications of gas sensor?

Gas sensors have various applications in different fields, including industrial safety, environmental monitoring, and health care.

Gas sensors can be used to detect toxic or flammable gases in industrial settings, monitor air quality in the environment, and measure the concentration of gases in the human breath for medical diagnosis.

What are the two types of gas detection system?

The two types of gas detection systems are fixed and portable. Fixed gas detection systems are installed in a fixed location and continuously monitor the concentration of gases in the environment.

Portable gas detection systems are handheld devices that can be carried around and used for spot checks or in areas where a fixed system is not practical.

What is the alternative of gas sensor?

There are various alternatives to gas sensors, including thermal imaging cameras, sniffer dogs, and electronic noses.

These alternatives use different sensing technologies to detect the presence of gas and can be useful in certain applications where gas sensors may not be practical or effective.

Can ultrasonic sensor detect gas?

No, ultrasonic sensors cannot detect gas as they are designed to detect changes in sound waves caused by the reflection of objects.

Gas sensors use different sensing technologies, such as electrochemical or infrared, to detect the presence of gas.

What is smart gas sensor?

A smart gas sensor is a gas sensor that is integrated with advanced features such as wireless connectivity, data logging, and remote monitoring.

Smart gas sensors can create IoT (Internet of Things) systems that can be remotely monitored and controlled using a smartphone or computer.

Where do you put a gas detection sensor?

The location of a gas detection sensor depends on the specific application and the type of gas being detected. In general, gas detection sensors should be placed where gas leaks are most likely to occur, such as near gas appliances or in areas where gas pipes are installed.

Sensors should be placed 1-2 feet above the ground and away from heat, moisture, or airflow sources.

What is the most advanced Arduino?

The most advanced Arduino board currently available is the Arduino Due, based on the ARM Cortex-M3 processor, with 54 digital I/O pins and 12 analog input pins.

Which Arduino microcontroller is the most widely used?

The Arduino Uno is the most widely used Arduino microcontroller. It is a popular choice among beginners and experienced users alike due to its simplicity, versatility, and low cost.

What is the highest voltage Arduino can read?

The highest voltage Arduino can read depends on the specific board and the analog-to-digital converter (ADC) used. In general, most Arduino boards can read voltages between 0 and 5 volts, while some boards, such as the Arduino Due, can read voltages up to 3.3 volts.

How is MQ-135 different from other gas sensors?

MQ-135 gas sensor differs from other gas sensors in the MQ series as it is designed to detect a wide range of gases, including ammonia, nitrogen oxides, and sulfur dioxide, in addition to common gases such as CO, LPG, and alcohol. MQ-135 also has a high sensitivity and accuracy, making it a popular choice in environmental monitoring applications.

How many gases can a gas detector detect?

The number of gases a gas detector can detect depends on the specific model and the type of sensor used. Some gas detectors can detect a single gas, while others can detect multiple gases simultaneously. Some advanced gas detection systems can detect up to six gases at once.

How far can MQ-135 detect?

The detection range of MQ-135 gas sensor depends on various factors, such as the type of gas being detected, the concentration of the gas, and the environment. In general, the detection range of MQ-135 gas sensor can vary from a few centimeters to several meters.

Is MQ2 bannable?

MQ2 gas sensor is not banned and is widely used in various applications, including gas leakage detection and environmental monitoring.

Which sensor is best for LPG gas detection?

MQ2 gas sensor is a popular choice for LPG gas detection due to its high sensitivity and low cost. However, electrochemical sensors can also be used for LPG gas detection as they offer higher accuracy and reliability.

What is the difference between MQ2 and MQ9 sensor?

The main difference between MQ2 and MQ9 gas sensors is the type of gas they are designed to detect. MQ2 is used for detecting smoke, LPG, propane, methane, alcohol, and hydrogen, while MQ9 is used for detecting carbon monoxide, flammable gases, and methane.

What is the most accurate fuel level sensor?

The most accurate fuel level sensor depends on the specific application and the type of fuel being measured. Popular fuel level sensors include ultrasonic, resistive, and capacitance sensors.

Which material is best for gas sensing?

Metal oxide semiconductors (MOS) are one of the best materials for gas sensing due to their high sensitivity and selectivity to various gases.

Other materials used for gas sensing include carbon nanotubes, graphene, and polymers. The choice of material depends on the specific gas being detected and the sensitivity and accuracy required.