Just to make sure, i’ve kept it in a closed cupboard with no external interference and wiring is correct and code is also working , i.e.
const int irEmitterPin = 3;
const int irReceiverPin = 4;
int objectDetectionCount = 0;
bool objectPresent = false;
void setup() {
pinMode(irEmitterPin, OUTPUT);
pinMode(irReceiverPin, INPUT);
Serial.begin(9600);
Serial.println(“IR Proximity Sensor Test”);
}
void loop() {
// Turn on the IR Emitter (Infrared LED)
digitalWrite(irEmitterPin, HIGH);
delay(10); // Give some time for the emitter to stabilize its output
// Read the IR Receiver (Infrared Photodiode)
int irReceiverValue = digitalRead(irReceiverPin);
// Check if the IR Receiver detects the emitted IR signal (presence detected)
if (irReceiverValue == HIGH && !objectPresent) {
// Object detected (signal not received)
objectDetectionCount++;
Serial.print("Object detected. Count: ");
Serial.println(objectDetectionCount);
objectPresent = true;
}
// Check if the IR Receiver doesn’t detect the emitted IR signal (no presence)
if (irReceiverValue == LOW) {
objectPresent = false;
}
// Turn off the IR Emitter to save power
digitalWrite(irEmitterPin, LOW);
// Add a small delay to control the sensing rate
delay(100);
}