#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <math.h>
// Define OLED width and height
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Define Thresholds
#define TEMP_HOT_THRESHOLD 25.0 //To detect hotness
#define TEMP_COLD_THRESHOLD 10.0 //To detect Coldness
#define DIST_THRESHOLD 10
// Define SSD1306 OLED display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Define pin connections for sensors and LEDs as constants for safety
const int TEMP_PIN = A0;
const int PIR_PIN = 4;
const int TRIG_PIN = 6;
const int ECHO_PIN = 7;
const int TEMP_LED = 3;
const int PIR_LED = 5;
const int DIST_LED = 8;
// Constants for the thermistor calculation
const float BETA = 3950; // Beta coefficient for thermistor
const int SERIES_RESISTOR = 10000; // 10kΩ resistor
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize pins
pinMode(TEMP_LED, OUTPUT);
pinMode(PIR_LED, OUTPUT);
pinMode(DIST_LED, OUTPUT);
pinMode(PIR_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize the display with address 0x3C for I2C
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Infinite loop to stop execution if display fails
}
// Display a welcome message
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("Welcome to Smart Home");
display.display();
delay(2000); // Pause for 2 seconds
// Read and display the initial temperature value
readTemperature();
display.clearDisplay();
}
void loop() {
readTemperature(); // Modular function for temperature reading
checkMotion(); // Modular function for PIR sensor logic
measureDistance(); // Modular function for ultrasonic sensor
display.display(); // Update the display once after all measurements
delay(5000); // Pause for 5 second before next loop
}
// Modular function for temperature sensor reading using Beta coefficient method
void readTemperature() {
int tempValue = analogRead(TEMP_PIN);
// Guard against divide-by-zero error
if (tempValue == 0) {
Serial.println("Error: Temperature sensor read value 0. Skipping temperature reading.");
return; // Skip temperature processing if invalid reading
}
float resistance = (1023.0 / tempValue) - 1.0;
resistance = SERIES_RESISTOR / resistance;
// Calculate temperature using Beta coefficient
float temperatureC = 1 / (log(resistance / SERIES_RESISTOR) / BETA + 1.0 / 298.15) - 273.15;
// Display temperature on Serial Monitor and OLED Display
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Temperature: ");
display.print(temperatureC);
display.println(" C");
// Temperature LED logic
if (temperatureC > TEMP_HOT_THRESHOLD || temperatureC < TEMP_COLD_THRESHOLD) {
digitalWrite(TEMP_LED, HIGH);
} else {
digitalWrite(TEMP_LED, LOW);
}
}
// Updated modular function for PIR motion sensor logic
void checkMotion() {
static int pirState = LOW; // Maintain state across function calls
int val = digitalRead(PIR_PIN); // Read the input value from the PIR sensor
if (val == HIGH) { // Check if the input is HIGH (motion detected)
digitalWrite(PIR_LED, HIGH); // Turn LED ON
if (pirState == LOW) {
// Motion has just been detected
Serial.println("Motion detected!");
display.setCursor(0, 20); // Set the cursor position on the display
display.println("Motion Detected!");
pirState = HIGH; // Update state
}
} else {
digitalWrite(PIR_LED, LOW); // Turn LED OFF
if (pirState == HIGH) {
// Motion has just ended
Serial.println("Motion ended!");
pirState = LOW; // Update state
}
}
}
// Modular function for ultrasonic distance measurement using HC-SR04
void measureDistance() {
// Start a new measurement:
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2); // Ensure the TRIG pin is low for a stable pulse
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10); // Keep TRIG pin high for 10 microseconds
digitalWrite(TRIG_PIN, LOW);
// Read the duration from the ECHO pin
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in centimeters
int distance_cm = duration / 58;
// Display distance on Serial Monitor and OLED Display
Serial.print("Distance in CM: ");
Serial.println(distance_cm);
display.setCursor(0, 40);
display.print("Distance: ");
display.print(distance_cm);
display.println(" cm");
// Distance LED logic
if (distance_cm < DIST_THRESHOLD) {
digitalWrite(DIST_LED, HIGH);
} else {
digitalWrite(DIST_LED, LOW);
}
}