Pnp photoelectric sensor

I am trying to have my yellow LED light turn on whenever something is in the range of my photoelectric sensor. My code for whatever reason is not working, I was hoping someone had an idea why that was or has some experience with photo electric sensors.
Here are the specs of the sensor:

Here is the code I have so far:

int signal_pin = 2; //set signal pin to 2

int photoeyepin = A0; // set the photo eye to pin A0

int val = 0; //variable to store the value read

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode(photoeyepin,INPUT); //Set photo eye as an input

pinMode(signal_pin,OUTPUT); //Set this signal pin as an output

}

void loop() {
// put your main code here, to run repeatedly:
val = analogRead(photoeyepin);

if(val == LOW) {digitalWrite(signal_pin,HIGH);}

Serial.println(val);

}

P.S I am not very confident on the logic of the IF statement.

Preformatted text

Please post your wiring diagram.

'val' is an analog value. You need to compare it, like 'val > 444'

It's not a diagram but it's better than nothing. See post #3.

Yeah sorry, I am not sure where I can create one.

Oh, wait, that is not an analog sensor, it's digital. So forget what I said about the compare. You can create a diagram with pen and paper. The sensor works on 10-30V, how are you connecting it to the Arduino's 5V compatible pins? How are you powering the sensor? All questions that are best answered in a diagram.


The NC is the normally closed wire

Don’t see a common GND going to the analog sensor :thinking: .

Confirm the output from the sensor is 0-5V ?

The usual way to connect a PNP sensor to a 5V Arduino is with a voltage divider to ground.

For a 10V sensor, 10K + 10K, with the tap connected to a digital input, will work. Connect all the grounds.

Capture

Where would I find the output of the sensor? in the specs it just says that the outputs is PNP

so why would it be a digital input and not analog?

Look in the sensor data sheet for the pin or wire description.

A PNP sensor is a digital output sensor.

I looked in the data sheet (instruction manual) for you, and found this. "Load" would be the resistive divider posted above. Don't forget to connect all the grounds.

Capture

If you have a Volt Meter, measure the range on the output of that sensor.

Note, your output may be open collector.

1 Like

Yeah I saw those diagrams which is how i knew how to wire the sensor to the arduino and the power supply

the resistive divider in post#10??

Yes, of course.

If you have already connected the sensor output to an Arduino input without that divider, you may have destroyed the input.

However, if you forgot to connect the grounds, the input is probably still OK.

Yeah, I have the ground of the arduino connected to the resistor on the breadboard and the ground of the power supply connected to the ground of the sensor. is there something wrong with that?

Sorry, I have no idea what you mean.

Post a different, hand drawn wiring diagram, showing ALL the connections, including the resistor. State the value of the resistor.

Adding an Optocoupler to the sensor output might be safest for you.