MQ2 is a Gas sensor Module that can be used to sense gases like LPG (Cooking Gas), Smoke, Alcohol, Hydrogen, Methane, and Carbon Monoxide concentrations in air. This type of sensor is suitable in making projects like Air Quality monitoring systems, fire alarms, Gas Leakage detection, Alcohol detection-based Ignition system in vehicles, combining the same with IoT, the applications are infinite. In this tutorial let us dig deep into understanding how this sensor works and how we can use the same for making such projects.
Understanding the MQ2 Sensor
The MQ2 sensor is a Metal Oxide Semiconductor (MOS) based sensor which means just like a Light-dependent resistor (LDR), the MQ2 is equipped with a ChemiResistor (stannic oxide) which means the resistance of the sensing material changes when an appropriate gas comes in contact with the same. Depending on the level of concentration of the gas, the sensor outputs a potential difference, which changes the resistance of the material inside the sensor. This change is used to determine the type of gas as well as the concentration of its presence in the atmosphere in ppm (parts per million) from the Analog signal from the sensor (Analog out pin) while Digital values (Digital out pin) from the sensor can be used to detect the presence of these gases.
Interfacing with Arduino
Connect The MQ2 sensor to Arduino according to the following
Find the code here.
As mentioned previously we can get both Analog (gas concentration in ppm) or digital (presence or absence of the gas) from the sensor using the A0 and D0 pins from the sensor respectively. Connect the Arduino to Pc via USB cable, open up the Arduino IDE and copy-paste the code given below. Verify the code in Arduino and select the appropriate board from the tools section of your Arduino IDE as well as make sure to correctly specify the COM port. Once done, click on upload. Expose the sensor in smoke, you can see that the inbuilt LED in the Arduino turns on. Thus you have successfully made a gas leakage detection system.
Interfacing with Raspberry Pi
Any Raspberry Pi with Raspberry Pi OS installed can be used to make
the setup work. As the Raspberry Pi does not have an ADC (Analog to
Digital Converter) built-in like Arduino, We need to use an IC called
MCP3002. Using this chip, the Raspberry Pi can interpret Analog
signals coming from the sensor.
Connect the Sensor to Raspberry Pi according to the above diagram. I have used a Raspberry Pi Model B same pin configuration can be followed for any Raspberry Pi provided you plug in the correct GPIO Pins.
The output of the sensor goes into the MCP3002 pin CH0, which is pin 2 of the MCP3002. This is one of the Analog input pins of the MCP3002. The MCP3002 needs to convert this Analog signal from the Gas sensor into a digital signal, which is the only type of signal that the Raspberry Pi can understand.
Create a new python file in the Raspberry Pi Desktop and execute the following command in the terminal.
Find the code here.
As I mentioned previously the MQ2 sensor can sense various gases therefore we can make a wide variety of projects and gadgets with the same. It can detect LPG thus we can make an LPG leakage detection system. The sensor can detect Carbon Monoxide thus we can use it to measure the amount of CO released by our vehicles. Have a biogas plant at home? This is an ideal sensor to measure the amount of gas being produced from the plant. The sensor can also be used in Cars and helmets to prevent drunk driving as it also has the capability to sense alcohol.