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.
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.
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.
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?