当前位置:网站首页>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
边栏推荐
- 简述EMD分解、希尔伯特变换、谱方法
- USB Network Native Driver for ESXi更新到支持ESXi7.0.1
- Getting started with latex
- Convolutional neural network
- 使用PowerCli来创建自定义ESXi ISO镜像
- Ctfshow single dog -- Web
- Byte Android post 4 rounds of interviews, received 50k*18 offers, and successfully broke the situation under the layoff
- ESXi Arm Edition version 1.10更新
- (PHP graduation project) based on PHP student daily behavior management system access
- DSX2-8000如何校准?校准流程?
猜你喜欢

Cyclic neural network

File upload vulnerability summary

Differences between processes and threads

Deep learning (I): enter the theoretical part of machine learning and deep learning

EMC实验实战案例-ESD静电实验

如何测试工业以太网线缆(利用FLUKE DSX-8000)?

When to replace jack socket for dsx-pc6 jumper module?

Addition and multiplication calculation of GF (2^8)

DSX2-8000如何校准?校准流程?

Which enterprises are suitable for small program production and small program development?
随机推荐
二、OpenVINO简述与构建流程
Byte Android post 4 rounds of interviews, received 50k*18 offers, and successfully broke the situation under the layoff
ESXi社区版网卡驱动2022年3月更新
Never leave its origin - bluecms1.6 vulnerability of the controller's shooting range
Transformer 自注意力机制 及完整代码实现
Notes on the thinking of software analysis and library acquisition of a Taobao customer
Web滚动字幕(MARQUEE示例)
4、 Model optimizer and inference engine
EIGamal 密码体制描述
ASP.NET 读数据库绑定到 TreeView 递归方式
(PHP graduation design) obtained based on PHP fruit sales store management system
Reversible watermarking method based on difference expansion
浪涌冲击抗扰度实验(SURGE)-EMC系列 硬件设计笔记6
后门攻击与对抗样本攻击的比较研究
关于gcc :multiple definition of
Model inversion attacks that exploit confidence information on and basic countermeasures
Protecting Against DNN Model Stealing Attacks 论文阅读心得
监控安装ESXi on Arm的树莓派4b的CPU温度
深度学习数据窃取攻击在数据沙箱模式下的威胁分析与防御方法研究阅读心得
When to replace jack socket for dsx-pc6 jumper module?