Skip to main content
2 of 3
Schematic diagram and code added.

digitalRead() function of Arduino doesn't read Input value sent by sensor after detecting the object

Schematic diagramI'm trying to turn ON the LED(inbuilt/PIN13) when photoelectric proximity sensor(12v) senses any object. Sensor is connected on pin 2. Assigned pin 2 as INPUT, I've got a 12v spdt relay for switching. 12v output from sensor is fed to coil, on the other end 5v supplied to COM pin and "Normally Open"(NO) pin to Arduino (with pull-down resistor). When 'NO' connected to loose LED it works properly (shows 5V on voltmeter when object is detected). But when connected to Arduino digitalRead(), it always returns LOW/0.

//code int a=0; void setup() { pinMode(2,INPUT); pinMode(13,OUTPUT); Serial.begin(9600); }

void loop() { a == digitalRead(2); Serial.println(a); if(a==HIGH){ Serial.println("Object Detected"); digitalWrite(13,HIGH);
}else{ Serial.println("There's no object");
digitalWrite(13,LOW);
} delay(1000); }