当前位置:网站首页>Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino
Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino
2022-07-28 06:20:00 【Haile learning】
arduino Read analog voltage _MQ2 Gas / How the smoke sensor works and how it works with Arduino Interface
Use MQ2 Gas sensor module , For your next Arduino The project brings an artificial nose . This is a powerful gas sensor suitable for sensing LPG, smoke , alcohol , propane , hydrogen , Methane and carbon ** carbon monoxide ** Concentration in the air .
If you plan to create an indoor air quality monitoring system ; Breathing detector or early fire detection system ,MQ2 The gas sensor module is a good choice .

The analog output voltage provided by the sensor is proportional to the flue gas concentration . The higher the gas concentration , The higher the output voltage ; And lower gas concentration will lead to low output voltage . The following animation illustrates the relationship between gas concentration and output voltage .

come from MQ2 The analog signal of the gas sensor is further fed to LM393 High Precision Comparator ( Welded at the bottom of the module ), Of course, it is to digitize the signal . There is also a small potentiometer used with the comparator , You can turn it to adjust the sensitivity of the sensor . You can use it to adjust the gas concentration detected by the sensor .
The sensor is sensitive to a variety of gases - But it is impossible to determine which gas it is ! That's normal ; Most gas sensors are like this . therefore , It is best to measure the change in the density of a given gas , Instead of detecting changing gases .
calibration MQ2 Gas sensor module
To calibrate the gas sensor , The gas sensor can be kept at the smoke to be detected / Near the gas , And continue to rotate the potentiometer , Until the red on the module LED Start to glow .
Turn the screw clockwise to increase sensitivity , Turn the screw counterclockwise to reduce sensitivity .

The comparator on the module will constantly check the analog pin (A0) Whether the threshold set by the potentiometer has been reached . When the threshold is exceeded , Digital pin (D0) Will go high , And the signal LED Lighten up . When you need to trigger an action when a certain threshold is reached , This setting is very useful . for example , When the smoke exceeds the threshold , You can turn the relay on or off , Or instruct the robot to blow / Spray water . You have an idea !
MQ2 Pin arrangement of gas sensor module

VCC Supply power to the module . You can connect it to Arduino Of 5V Output .
GND It's a ground pin , Need to connect to Arduino Of GND Pin .
D0 ( or DIN) Provide a digital representation of the presence of combustible gases .
A0 ( or AIN) Provide analog output voltage proportional to flue gas concentration .
connection – take MQ2 The gas sensor module is connected to Arduino UNO
take VCC Pin to Arduino Of 5V Pin , And will GND Pin to Arduino Ground pin of (GND).
Place the on the module D0 The output pin is connected to Arduino Digital pin on 8, Place the on the module A0 The output pin is connected to Arduino Analog pin on A0.

Arduino Code
The code is very simple , Basically only read A0 Analog voltage at pin . When smoke is detected , It also prints a message on the serial monitor .
#define MQ2pin (0)
float sensorValue; //variable to store sensor value
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
Serial.println("Gas sensor warming up! Gas sensor preheating ");
delay(20000); // allow the MQ-6 to warm up
}
void loop()
{
sensorValue = analogRead(MQ2pin); // read analog input pin 0
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
if(sensorValue < 300) // threshold Judge according to the actual situation Greater than 300 Still less than 300 ,300 It's also a variable
{
Serial.print(" | Smoke detected! Smoke detected ");
}
Serial.println("");
delay(2000); // wait 2s for next reading
}The output on the serial monitor is shown below :

My test environment is 100 A little more Indicates smoke , Normally 700 many .( What we tested was A0 Output )
I use lighter After ignition Blow it out again . Methane released Gas . direct Put it in detector On the edge . Detected 100 A little bit more Output
Code instructions :
Defines a name sensorValue To store sensor values .
#define MQ2pin (0)
float sensorValue; // Defining variables In the setting function : We initialize with PC Serial communication , And wait for 20 Seconds to allow the sensor to warm up .
Serial.begin(9600); // sets the serial port to 9600
Serial.println("Gas sensor warming up!");
delay(20000); // allow the MQ-6 to warm upCycle function : The sensor value is determined by analogRead() Function to read and display on the serial monitor
sensorValue = analogRead(MQ2pin); // read analog input pin 0
Serial.print("Sensor Value: ");
Serial.print(sensorValue);When the gas concentration is high enough , The output of the sensor is usually greater than 300 Value . We can use if sentence Monitor this value . When the sensor value exceeds 300 when , We will show “ Smoke detected !”. Information .
if(sensorValue > 300)
{
Serial.print(" | Smoke detected!");
}notes :
One 、println Output one line
Serial.println(" The output string a line "); //println Output one line Two 、 Upload error
Upload error :avrdude: stk500_recv(): programmer is not responding

Solution :
Every time you upload code , Pull it off 0,1 Pin line . Connect it back after uploading . because arduino These two pins will be used when uploading data for interaction

Thank these two bloggers for sharing :
https://blog.csdn.net/weixin_42533120/article/details/112499307
https://blog.csdn.net/weixin_40317528/article/details/88926989
边栏推荐
- PLC的选型
- 硬件电路设计学习笔记1--温升设计
- 关于隔离电源断电瞬间MOSFET损坏问题分析
- (PHP graduation project) based on PHP online travel website management system to obtain
- ESXi社区版网卡驱动再次更新
- Protecting Against DNN Model Stealing Attacks 论文阅读心得
- Cyclic neural network
- 2、 Openvino brief introduction and construction process
- 短跳线DSX-8000测试正常,但是DSX-5000测试无长度显示?
- 8类网线测试仪AEM testpro CV100 和FLUKE DSX-8000哪些事?
猜你喜欢

DSX-PC6跳线模块,何时更换JACK插座?

Fluke fluke aircheck WiFi tester cannot configure file--- Ultimate solution experience

(PHP graduation project) based on thinkphp5 community property management system

Prime_ Series range from detection to weight lifting

EIGamal cryptosystem description

端接电阻详解 信号完整系列 硬件学习笔记7

Ctfshow single dog -- Web

GF(2^8)的加法与乘法计算

2、 Openvino brief introduction and construction process

关于gcc :multiple definition of
随机推荐
(PHP graduation project) based on PHP Gansu tourism website management system to obtain
针对大量数据,MATLAB生成EXCEL文件并进行排版处理的源码
EMC实验实战案例-ESD静电实验
后门攻击与对抗样本攻击的比较研究
The difference and connection between cookies, sessions and tokens
Internet of things interoperability system: classification, standards and future development
物联网互操作系统:分类、标准与未来发展方向综述
如何测试工业以太网线缆(利用FLUKE DSX-8000)?
TCL和ELTCL?CDNEXT和CMRL?
福禄克DSX2-5000、DSX2-8000模块如何找到校准到期日期?
AEM-TESTpro K50和南粤勘察结下的缘分
RS232 RS485 RS422 通信 学习及备忘笔记
用颜色区分PCB品质本身就是一个笑话
File upload vulnerability summary
Basic usage of word2vec and Bert
AEM online product promotion conference - Cable certification tester
基于直方图修改的可逆数字水印方法
硬件电路设计学习笔记2--降压电源电路
VB-ocx应用于Web
四、模型优化器与推理引擎