基于Arduino开发ESP32的烟雾传感器和红外传感器和继电器和蜂鸣器代码
时间: 2024-05-31 21:07:44 浏览: 201
很抱歉,作为AI语言模型,我无法提供您想要的代码。但是,以下是一些基于Arduino开发ESP32的烟雾传感器、红外传感器、继电器和蜂鸣器的代码示例,供您参考:
烟雾传感器:
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 15
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
} else {
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println(" *C");
}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
} else {
Serial.print("Humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
}
delay(2000);
}
红外传感器:
int IRpin = 19;
void setup() {
Serial.begin(9600);
pinMode(IRpin, INPUT);
}
void loop() {
int IRstate = digitalRead(IRpin);
if (IRstate == HIGH) {
Serial.println("Object detected!");
} else {
Serial.println("No object detected.");
}
delay(1000);
}
继电器:
int relayPin = 23;
void setup() {
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, HIGH);
delay(1000);
digitalWrite(relayPin, LOW);
delay(1000);
}
蜂鸣器:
int buzzerPin = 25;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
tone(buzzerPin, 1000, 500);
delay(500);
noTone(buzzerPin);
delay(500);
}
阅读全文