-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ino
More file actions
53 lines (41 loc) · 1.21 KB
/
main.ino
File metadata and controls
53 lines (41 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <DHT.h>
#define DHTPIN 2 // Pin connected to the DHT11 sensor
#define DHTTYPE DHT11 // Define the sensor type
DHT dht(DHTPIN, DHTTYPE);
// Define the relay pin
const int relayPin = 3;
// Define temperature threshold (in Celsius)
const float tempThreshold = 30;
void setup() {
// Start the serial communication
Serial.begin(9600);
// Initialize the DHT sensor
dht.begin();
// Set the relay pin as output
pinMode(relayPin, OUTPUT);
}
void loop() {
// Wait a few seconds between readings
delay(2000);
// Read the temperature in Celsius
float temperature = dht.readTemperature();
// Check if reading is successful
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the temperature to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Control the fan based on the temperature
if (temperature > tempThreshold) {
// Turn the fan on if temperature exceeds threshold
digitalWrite(relayPin, HIGH);
Serial.println("Fan ON");
} else {
// Turn the fan off if temperature is below threshold
digitalWrite(relayPin, LOW);
Serial.println("Fan OFF");
}
}