Interfacing MQ-2 Gas Sensor with ESP32 Using Arduino
Interfacing MQ-2 Gas Sensor with ESP32 Using Arduino
MQ-2 Gas
Sensor with
ESP32 Using
Arduino IDE
All about MQ2
• Sensors are an important part of
designing IoT based projects as they
feed the data to the system.
Microcontroller based IoT boards
gained popularity because of their
ability to interface different sensors and
upload data to the cloud or generate an
emergency email.
• Gas sensor is among the widely used
sensors with ESP32 which can detect
fire eruption or gas leakage inside a
room. Let’s find out the possible way of
interfacing MQ-2 gas sensor with
ESP32.
MQ-2 Gas
Sensor
• MQ-2 is one of the widely
available gas sensors with
greater precision compared to
others as it is a MOS (Metal
Oxide Semiconductor) sensor.
Sensors like these are known as
Chemiresistors because their gas
sensing is based upon the
change in resistance value once
exposed to gas particles.
• int LED = 32; /*LED pin defined*/
int Sensor_input = 4; /*Digital pin 5 for sensor input*/
void setup() {
Serial.begin(115200); /*baud rate for serial communication*/
pinMode(LED, OUTPUT); /*LED set as Output*/
}
void loop() {
int sensor_Aout = analogRead(Sensor_input); /*Analog value read function*/
Serial.print("Gas Sensor: ");
Serial.print(sensor_Aout); /*Read value printed*/
Serial.print("\t");
Serial.print("\t");
if (sensor_Aout > 1800) { /*if condition with threshold 1800*/
Serial.println("Gas");
digitalWrite (LED, HIGH) ; /*LED set HIGH if Gas detected */
}
else {
Serial.println("No Gas");
digitalWrite (LED, LOW) ; /*LED set LOW if NO Gas detected */
}
delay(1000); /*DELAY of 1 sec*/
}
Conclusion
• MQ-2 is a gas detection sensor which can sense
the gas leakage and generate signals accordingly.
Using an ESP32 microcontroller board we can
easily interface it and can use it as a fire alarm
detector or can generate an emergency email
notification. Here in this article, we connected
ESP32 with the MQ-2 sensor using the three pins
of the sensor. An LED is used for indication
purposes once gas is detected.
Buzzer
• generate sound alerts,
such as warnings,
alarms, confirmation of
certain actions, or other
reasons.
Differences between passive and
active buzzer
const int buzzer = ; //buzzer to arduino pin
void setup(){
pinMode(buzzer, OUTPUT); // Set buzzer -
pin as an output
}
void loop(){
tone(buzzer, 2000); // Send 1KHz sound
signal...
delay(500); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(250); // ...for 1sec
}
int LED = 5; /*LED pin defined*/
int Sensor_input = 4; /*Digital pin 5 for sensor input*/
const int buzzer = 21 ; //buzzer to arduino pin
void setup() {
Serial.begin(115200); /*baud rate for serial communication*/
pinMode(LED, OUTPUT); /*LED set as Output*/
pinMode(buzzer, OUTPUT); // Set buzzer - pin as an output
}
void loop() {
int sensor_Aout = analogRead(Sensor_input); /*Analog value read function*/
Serial.print("Gas Sensor: ");
Serial.print(sensor_Aout); /*Read value printed*/
Serial.print("\t");
Serial.print("\t");
if (sensor_Aout > 800) { /*if condition with threshold 1800*/
Serial.println("Gas");
digitalWrite (LED, HIGH) ; /*LED set HIGH if Gas detected */
tone(buzzer, 3000); // Send 3KHz sound signal...
delay(500); // ...for 1 sec
}
else {
Serial.println("No Gas");
digitalWrite (LED, LOW) ; /*LED set LOW if NO Gas detected */
noTone(buzzer); // Stop sound...
}
delay(1000); /*DELAY of 1 sec*/
}