Contents
MQ-2 is a sensor used to detect gases such as LPG (Liquefied Petroleum Gas), CO (Carbon Monoxide), and smoke. However, this sensor is often used as a gas leak detector both at home and in the industry. The MQ-2 sensor comes as a ready-to-use module as shown in the picture below
The MQ-2 sensor is made of an aluminum tube surrounded by silicon and there are electrodes made of gold. When detecting gas, a heating process occurs so that the ceramic SnO2 becomes a conductor so that it releases electrons to produce an analog voltage output. The MQ-2 sensor module has 4 pins, namely: VCC, GND, DO, and A0. To better understand the MQ-2 sensor let’s do a simple experiment. In this experiment, prepare the following tools and materials:
Tools and materials MQ-2 – NodeMCU Experiment
- NodeMcu Lolin
- MQ-2 Sensor
- Male to female jumper cables as needed
Make a circuit like in the picture below with configuration:
• The VCC pin of the MQ-2 is connected to the 5V pin of the NodeMcu.
• The MQ-2 GND Pin is connected to GND pin of the NodeMcu
• Pin A0 MQ-2 (Analog Output) is connected to A0 pin of the NodeMcu
MQ-2 – NodeMCU Source Code
You need to install MQ2.h library first. Open the Library Manager on your Arduino IDE, choose the Tools-Manage Libraries menu, then type ‘mq2’ on the search bar. Make sure you was covered by internet to download the library
Next, open your Arduino IDE Code Editor, then copy the code as follows:
#include <MQ2.h> // MQ2 Gas Sensor Library int pinAout = A0; // pin A0 of NodeMcu is connected to pin A0 of MQ-2 int LPG, Co, Smoke; // Variable values of lpg, CO-gas, and Smoke MQ2 mq2(pinAout); //Declare pin A0 connected to A0 pin Arduino void setup(){ Serial.begin(115200); // Baudrate to display data on serial monitor mq2.begin(); // Instructions to start the MQ2 sensor pinMode(pinAout,INPUT); //Declare pin A0 as input } void loop(){ /*Reading all three gases at once in PPM * No.1 = LPG Gas * no.2 = CO gas * n0.3 = SMOKE */ float* values= mq2.read(true); //if set to "false" will not appear on serial monitor LPG = mq2.readLPG(); Co = mq2.readCO(); Smoke = mq2.readSmoke(); delay(1000); }
Upload the code above by pressing the upload button or via the hotkey CTRL+U. Make sure that you choose ‘NodeMCU 1.0 (ESP-12E Module)’ on the Tools-Board menu .
Then activate the ‘Serial Monitor’ tool in the Arduino IDE via the Tools – Serial Monitor menu. Make sure your USB cable between the laptop and Arduino remains plugged in. After the program has been uploaded, try detecting gas using a lighter and pay attention to the data on the Serial Monitor window.
If the Serial Monitor window shows data as shown in the picture above, your experiment has been successful. These data indicate that the MQ-2 sensor can detect gas as well.
a Technopreneur – writer – Enthusiastic about learning AI, IoT, Robotics, Raspberry Pi, Arduino, ESP8266, Delphi, Python, Javascript, PHP, etc. Founder of startup Indomaker.com