#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Replace 0x27 with the actual LCD address if needed
const int TRIGGER = 23; // GPIO pin for ultrasonic sensor trigger
const int ECHO = 22; // GPIO pin for ultrasonic sensor echo
const int buzzerPin = 21; // GPIO pin for buzzer
const int ledPin = 19; // GPIO pin for LED (both temperature and ultrasonic)
const float temperatureThreshold = 36.0; // Threshold for temperature alerts
const int REF_VIN = 3.3;
const int lm35Pin = 34; // GPIO pin for LM35 sensor
float getTemperature() {
float voltage = analogRead(lm35Pin);
float tmpVal = (voltage / 1023.0) * REF_VIN * 100;
return tmpVal;
}
float getDistance() {
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
long duration = pulseIn(ECHO, HIGH);
float distance = (duration / 2) / 29.1;
return distance;
}
void setup() {
Serial.begin(115200);
// Initialize I2C with non-standard pins
Wire.begin(18, 5); // Use GPIO 18 for SDA and GPIO 5 for SCL
// Pin mode setup
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2); // Initialize LCD with 16 columns and 2 rows
}
void loop() {
float temperature = getTemperature();
float distance = getDistance();
// Print to serial monitor
Serial.print("Temp: ");
Serial.print(temperature);
Serial.println("C");
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
// Handle temperature alerts
if (temperature > temperatureThreshold) {
// Temperature above threshold
Serial.println("Red Alert!");
lcd.setCursor(0, 1);
lcd.print("Red Alert!");
digitalWrite(ledPin, HIGH); // Blink LED
}
if (temperature < temperatureThreshold) {
// Temperature below or equal to threshold
Serial.println("Door Open");
lcd.setCursor(0, 1);
lcd.print("Door Open");
digitalWrite(ledPin, LOW); // Turn off LED
tone(buzzerPin, 1000, 500); // Ring buzzer
}
// Handle ultrasonic LED
if (distance <= 30) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(2000); // Adjust the delay based on your needs
}