#define echoPin 7 // Echo pin of the ultrasonic sensor
#define trigPin 6 // Trig pin of the ultrasonic sensor
#define pirPin 2 // PIR sensor pin
#define tempPin A0 // Thermistor pin
// Constants for temperature calculation
#define RT0 10000 // Thermistor resistance at 25°C
#define B 3977 // B coefficient of the thermistor
#define VCC 5 // Supply voltage
#define R 10000 // Series resistor value
#define T0 25 // Reference temperature in Celsius
// Variables
long duration; // Variable for the duration of sound wave travel
int distance; // Variable for the distance measurement
int pirValue = 0; // Variable to store PIR sensor value
float temperature; // Variable to store temperature
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(tempPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Read PIR sensor value
pirValue = digitalRead(pirPin);
// Read temperature
temperature = readTemperature();
// Read distance from ultrasonic sensor
distance = readUltrasonic();
// Logic to control LEDs based on specified conditions
if (pirValue && temperature >= 20 && temperature <= 30 && distance >= 10 && distance <= 40) {
// Turn on LED1
// Code to turn on LED1
Serial.println("LED1 ON");
}
if (pirValue && temperature >= 30 && temperature <= 40 && distance >= 40 && distance <= 100) {
// Turn on LED2
// Code to turn on LED2
Serial.println("LED2 ON");
}
if (pirValue && temperature >= 30 && temperature <= 50 && distance >= 100 && distance <= 300) {
// Turn on LED3
// Code to turn on LED3
Serial.println("LED3 ON");
}
delay(1000); // Delay for stability and to prevent rapid serial output
}
// Function to read temperature from thermistor
float readTemperature() {
float VRT = analogRead(tempPin) * (VCC / 1024.0); // Convert analog reading to voltage
float VR = VCC - VRT;
float RT = VRT / (VR / R); // Calculate thermistor resistance
float TX = 1 / ((log(RT / RT0) / B) + (1 / (T0 + 273.15))); // Calculate temperature in Kelvin
return TX - 273.15; // Convert Kelvin to Celsius
}
// Function to read distance from ultrasonic sensor
int readUltrasonic() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // Convert duration to distance in cm
}