After basic peripheral interfaces with AVR, it's time to interface some sensors with it. In this tutorial we will interface Gas sensor MQ2 with Atmega 32 breakout. We will require a Smoke Sensor (MQ2) Board .
Contents
Basics
The gas sensor MQ2 suitable for detecting of LPG, i-butane, propane, methane, alcohol, Hydrogen, smoke etc. Since It is highly sensitive and gives fast response, we can take measurements as soon as possible. This sensor can be used for gas leakage detection.
At normal condition, sensor resistor will be high so voltage drop across the load will be low and it will be a constant.If sensor senses flammable gases, resistance of sensor will drop. That means more current will flow from load resistor.So the voltage across it increases. This output voltage increases with increase in concentration of gas in air. The sensitivity of the gas sensor can be adjusted using potentiometer.
Refer MQ-2 datasheet for detailed information.
This Smoke Sensor (MQ2) Board has analog as well as digital output. For this tutorial we will use analog output. Analog output pin needs to be connect ADC channel 0 of atmega 32 breakout as shown in hook up.
Hook Up
- As MQ-2 sensor has heater inside, it is prefer to give power to the sensor from separate source.
- For stable operation sensor requires around 24 hour preheating.
- We can use Ultra AVR Developement Board,Starter AVR or Atmega32 Breakout.
Code
Resistance value of MQ-2 is difference to various kinds and various concentration gases. So,When using this components, sensitivity adjustment is very necessary.
The sensor resistor RS and load resistor RL form a voltage divider.
Based on the chart provided in the MQ-2 datasheet , RS in clean air under given temperature and humidity is a constant. The ratio of RS/R0 in clean air is 9.8 as described in datasheet.We will first calibrate the sensor. place sensor in clean air. We will get R0 value by dividing it by RS/R0 value in clean air.
Now once R0 is derived, the targeted gas can be sensed using RS/R0 ratio as shown in below program.
- Initially place the sensor in clean air and reset the controller. It will calibrate the sensor and give the message as calibration done.
- Then place the sensor in smoke affected area, it will detect the smoke and will output a gas concentration in ppm and also sensor LED will glow.
- For calculating the concentration of gas in ppm take two points from the curve of particular gas from the graph shown below. Then calculate a slope of that line.
For eg. Take two points from LPG curve point 1 is (log 200, log 1.6) and point 2 is (log 1000, log 0.26). These points are in logarithmic scale so take log of points and calculate the slope using formula: $$Slope = (y2-y1)/(x2-x1)$$
One point from above and slope is used in format (x,y, slope) in a program.
- In a graph the x-axis is Rs/R0 ratio and y-axis is concentration of gas in ppm.
We have to find (Gas concentration,RS/R0 ratio) point. In clean air we will get a Rs/R0 ratio.Now using above slope, Rs/R0 ratio and first point mentioned above can be used for calculation of concentration of gas using below formula. As it is logarithmic coordinate, power 10 is taken to convert it to non logarithmic value. $$x2=[((y2-y1)/slope)+x1]$$ $$Gas concentration=10^{(((log(Rs/R0)-(y1))/slope)+x1)} ppm$$
Output Demo
As sensor approaches smoke, it will show output like following.
Downloads
Download the complete project folder from the below link:
https://github.com/ExploreEmbedded/AVR-MCU-Breakout-Board/archive/master.zip
Have a opinion, suggestion , question or feedback about the article let it out here!