2

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);
}
timemage
5,6391 gold badge14 silver badges25 bronze badges
asked Mar 6, 2021 at 18:44
10
  • 4
    it is difficult to visualize what you are describing ... please draw a schematic diagram or a wiring diagram ... also include the code that you are using Commented Mar 6, 2021 at 19:10
  • To read that detector: get rid of the relay and replace it with an ordinary voltage divider. Commented Mar 7, 2021 at 7:33
  • I tried using 7805 to get 5v output. It did not work, @SimSon Commented Mar 7, 2021 at 7:46
  • @jsotola yes. sure. I'm uploading a schematic and code. Commented Mar 7, 2021 at 7:47
  • use 1kOhm resistor for pulldown Commented Mar 7, 2021 at 8:53

1 Answer 1

2

a == digitalRead(2) must be a = digitalRead(2). You also better change the if clause to if(a) to avoid true/HIGH/1 confusions.

If you had formatted the code properly, you'd have safed some time (you just need to select the code and press the {} button).

You also better replace the relay with a voltage divider (as I mentioned in my comment), because as long as you don't have a datasheet for the sensor you can't be sure that the sensor is designed to source that much current. If the sensor is supposed to drive a TTL logic, it will eventually fail, just like you don't drive relays with GPIOs directly. And beside that you need a freewheeling diode across the relay coil to deal with back EMF which will also harm the sensor.

answered Mar 7, 2021 at 11:44
2
  • I can't forgive myself for this. I have wasted like 3 days on this stupidest syntax typo. Thank you for drawing my attention to it. Thank you for suggestions. Commented Mar 7, 2021 at 11:56
  • oh! I'll give it a try. The freewheeling diode stuff is really helpful for me. thanks. Commented Mar 7, 2021 at 12:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.