烟雾传感器MQ-2实验
实验现象
烟雾传感器把烟雾浓度值转换为模拟电压值输出,arduino通过A0引脚读取模拟电压值,通过串口发送给PC,当模拟电压值超过模块的比较器设定值,模块D0引脚输出低电平。arduino监控到D0引脚拉底信号时,板载LED点亮报警,串口输出Alarm信号。报警的阀值可以通过调节板载的电位器调整大小。
理论学习
VCC和GND之间加上5V电压
等待大概30s传感器预热后,读取模拟输出A0引脚电压
通过调节电位器,改变LED报警阀值
当浓度大于设定值时,模块上LED指示灯点亮,同时D0引脚输出低电平。
原理图
代码编写
#include<Arduino.h>
#define sensor A5
#define led 13
#define sensor_d0 2
unsigned int sensorvalue = 0;
void setup() {
// put your setup code here, to run once:
pinMode(sensor_d0, INPUT);
pinMode(sensor, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println("welcome to use");
}
void loop() {
// put your main code here, to run repeatedly:
sensorvalue = analogRead(sensor);
Serial.print("ad value = ");
Serial.println(sensorvalue);
if (digitalRead(sensor_d0) == LOW) {
Serial.println("alarm");
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
delay(1000);
}