const int irSensorPin = 2; // IR sensor output pin
const int relayPin = 3; // Relay control pin
const int ledPin = 4; // LED pin
// Variables
int objectDetected = 0; // Flag to track object detection
void setup() {
pinMode(irSensorPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(relayPin, LOW); // Turn off the relay initially
digitalWrite(ledPin, LOW); // Turn off the LED initially
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
objectDetected = digitalRead(irSensorPin); // Read IR sensor status
if (objectDetected == HIGH) {
// Object detected
digitalWrite(relayPin, HIGH); // Turn on the relay
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("Object detected!");
} else {
// No object detected
digitalWrite(relayPin, LOW); // Turn off the relay
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("No object detected.");
}
delay(5000); // Add a small delay for stability
}