int irSensorPin = 7;// IR sensor pin
int relayPin = 6; //Relay Pin
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(irSensorPin, INPUT); // Set the IR sensor pin as an input
pinMode(relayPin, OUTPUT); // Set the IR sensor pin as an output
}
void loop() {
int irSensorValue = digitalRead(irSensorPin);// Read the state of the IR sensor
if (irSensorValue == 0)
{
digitalWrite(relayPin , HIGH);// if detected switch the relay into ON state
delay(2000); // remain the switching ON for 2 seconds
}
else
{
digitalWrite(relayPin,LOW); // if not detected switch the relay into OFF state
}
delay(500);
}