arduino烟雾火焰报警器代码
时间: 2024-06-11 08:03:48 浏览: 32
Arduino烟雾和火焰报警器是一个常见的电子项目,通常使用传感器检测烟雾(如光敏电阻或激光感应器)和火焰(如红外热释电探测器),当检测到异常时通过蜂鸣器或者LED灯发出警报。以下是一个简单的Arduino烟雾和火焰报警器的基本代码示例:
```c++
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h> // For DHT11/22 temperature/humidity sensor (if used)
#define SMOKE_SENSOR_PIN A0 // Connect smoke sensor to this pin
#define FLAME_SENSOR_PIN A1 // Connect flame sensor to this pin
#define ALARM_PIN 9 // Pin for buzzer or LED alarm
DHT dht; // If using a DHT sensor
void setup() {
Serial.begin(9600);
pinMode(SMOKE_SENSOR_PIN, INPUT);
pinMode(FLAME_SENSOR_PIN, INPUT);
pinMode(ALARM_PIN, OUTPUT);
if (dht.useDefaultSensor()) { // Configure DHT sensor if connected
Serial.println("Using default DHT sensor...");
} else {
dht.setDHTType(DHT11); // Or DHT22
}
}
void loop() {
int smokeLevel = analogRead(SMOKE_SENSOR_PIN); // Read smoke sensor value (0-1023)
int flameLevel = analogRead(FLAME_SENSOR_PIN); // Read flame sensor value
// Check thresholds and trigger alarm if necessary
if (smokeLevel > SMOKE_THRESHOLD) {
digitalWrite(ALARM_PIN, HIGH); // Turn on alarm
Serial.println("Smoke detected!");
delay(2000); // Wait 2 seconds before checking again
digitalWrite(ALARM_PIN, LOW); // Turn off alarm
}
if (flameLevel > FLAME_THRESHOLD) {
digitalWrite(ALARM_PIN, HIGH);
Serial.println("Flame detected!");
delay(2000); // Wait before checking again
digitalWrite(ALARM_PIN, LOW);
}
// Optionally, read and print temperature/humidity if using DHT sensor
if (dht.available()) {
float temperature = dht.temperature();
float humidity = dht.humidity();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
}
delay(500); // Check sensor readings every 500ms
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)