#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
// Constants for DHT22
#define DHTPIN 15
#define DHTTYPE DHT22
// Constants for PIR motion sensor
#define PIRPIN 13
// Constants for LEDs
#define LED_TEMP_PIN 2 // LED for high temperature indication
#define LED_MOTION_PIN 4 // LED for motion detection indication
// Constants for Relay
#define RELAYPIN 14
// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
// High temperature threshold
#define HIGH_TEMP_THRESHOLD 30
// Create instances
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the DHT22 sensor
dht.begin();
// Initialize the PIR sensor pin
pinMode(PIRPIN, INPUT);
// Initialize the LED pins
pinMode(LED_TEMP_PIN, OUTPUT);
pinMode(LED_MOTION_PIN, OUTPUT);
// Initialize the relay pin
pinMode(RELAYPIN, OUTPUT);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.display();
}
void loop() {
// Read temperature and humidity from DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if the readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Read motion sensor state
int motionState = digitalRead(PIRPIN);
// Control the LED for high temperature
bool isHighTemp = temperature > HIGH_TEMP_THRESHOLD;
digitalWrite(LED_TEMP_PIN, isHighTemp ? HIGH : LOW);
// Control the LED for motion detection
bool isMotionDetected = motionState == HIGH;
digitalWrite(LED_MOTION_PIN, isMotionDetected ? HIGH : LOW);
// Control the relay based on temperature and motion detection
digitalWrite(RELAYPIN, (isHighTemp && isMotionDetected) ? HIGH : LOW);
// Display temperature, humidity, motion state, and LED statuses on OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print(F("Temp: "));
display.print(temperature);
display.print(F(" C"));
display.setCursor(0, 10);
display.print(F("Humidity: "));
display.print(humidity);
display.print(F(" %"));
display.setCursor(0, 20);
display.print(F("Motion: "));
display.print(isMotionDetected ? "Detected" : "None");
display.setCursor(0, 30);
display.print(F("Temp LED: "));
display.print(isHighTemp ? "On" : "Off");
display.setCursor(0, 40);
display.print(F("Motion LED: "));
display.print(isMotionDetected ? "On" : "Off");
display.display();
// Print values to Serial monitor (for debugging)
Serial.print(F("Temperature: "));
Serial.print(temperature);
Serial.print(F(" C, Humidity: "));
Serial.print(humidity);
Serial.print(F(" %, Motion: "));
Serial.print(isMotionDetected ? "Detected" : "None");
Serial.print(F(", Temp LED: "));
Serial.print(isHighTemp ? "On" : "Off");
Serial.print(F(", Motion LED: "));
Serial.println(isMotionDetected ? "On" : "Off");
// Wait a bit before the next loop
delay(2000); // Increase delay if needed for sensor stabilization
}