Arduino实现dht11传感器和mq2烟雾传感器以及火焰传感器控制led和蜂鸣器
时间: 2023-06-21 13:07:27 浏览: 480
烟雾传感器程序
以下是使用Arduino实现dht11传感器和mq2烟雾传感器以及火焰传感器控制led和蜂鸣器的代码示例:
```C++
#include <dht.h>
#define DHTPIN 2
#define DHTTYPE DHT11
dht DHT;
const int MQ2PIN = A0;
const int FLAMEPIN = A1;
const int LEDPIN = 9;
const int BUZZERPIN = 10;
void setup() {
Serial.begin(9600);
pinMode(LEDPIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);
}
void loop() {
int chk = DHT.read11(DHTPIN);
float temp = DHT.temperature;
float hum = DHT.humidity;
int smoke = analogRead(MQ2PIN);
int flame = analogRead(FLAMEPIN);
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("C, Humidity: ");
Serial.print(hum);
Serial.print("%, Smoke: ");
Serial.print(smoke);
Serial.print(", Flame: ");
Serial.println(flame);
if (temp > 25) {
digitalWrite(LEDPIN, HIGH);
digitalWrite(BUZZERPIN, HIGH);
delay(500);
digitalWrite(LEDPIN, LOW);
digitalWrite(BUZZERPIN, LOW);
delay(500);
}
if (smoke > 500) {
digitalWrite(LEDPIN, HIGH);
digitalWrite(BUZZERPIN, HIGH);
delay(500);
digitalWrite(LEDPIN, LOW);
digitalWrite(BUZZERPIN, LOW);
delay(500);
}
if (flame > 500) {
digitalWrite(LEDPIN, HIGH);
digitalWrite(BUZZERPIN, HIGH);
delay(500);
digitalWrite(LEDPIN, LOW);
digitalWrite(BUZZERPIN, LOW);
delay(500);
}
delay(2000);
}
```
该代码使用dht11传感器读取温度和湿度,使用mq2烟雾传感器和火焰传感器检测烟雾和火焰,如果温度、烟雾或火焰超过设定值,则LED和蜂鸣器会发出警报。你可以根据自己的需要更改传感器的引脚和阈值。
阅读全文