Use another pin like pin 13

Hey guys, I'm using a Photoeletric sensor that send a INPUT high to my digital pin 13.

But if I change to pin 2, or any other it dont work, because the print is constantly, and not only when something hit on the front of the sensor, I just want to know when its HIGH as INPUT but when I plug any wire with nothing on the other side it be HIGH, here is my code:

int inPinX2 = 13; ( i changed here for 2 and it dont work)
int valX2 = 0;

void setup()
{
    Serial.begin(9600);
   pinMode(inPinX2, INPUT); 
}

void loop()
{
   valX2=digitalRead(inPinX2);

    if (valX2 == HIGH) 
    {
        Serial.println(valX2);    
    }

  
}

but when I plug any wire with nothing on the other side it be HIGH

Why are you attaching things to the Arduino while it is running? Don't you like the cute little thing?

Do you have pullup or pulldown resistors attached to the pin, too? If not, you have a floating pin condition.

I wired the sensor too, but nothing works, I'm not using pull up and pull down resistors.

Why the same thing work on pin 13 and dont work on another pins ?

I would guess it's because 13 has the built in LED and maybe that is sort of acting like a weak pull-down.

Either way, it sounds like it's time to learn about pull-up and pull-down resistors.

fredvicentin:
Hey guys, I'm using a Photoeletric sensor...

WHAT photoelectric sensor? Link please, either to a datasheet or a product page.

Jiggy-Ninja:

fredvicentin:
Hey guys, I'm using a Photoeletric sensor...

WHAT photoelectric sensor? Link please, either to a datasheet or a product page.

http://www.optex-fa.com/download/c2series/manual/m_c2.pdf

I tryied to use a 10k resiston on the output and plugged on pin 2 but dont worked

There are 4 types shown on that datasheet.
What is the specific part number you are using?

CrossRoads:
There are 4 types shown on that datasheet.
What is the specific part number you are using?

I'm using C2RP-F400P

fredvicentin:
I tryied to use a 10k resiston on the output and plugged on pin 2 but dont worked

What do you mean? One end of the resistor went to pin 2, got that. Where was the other end of the resistor connected?

Okay, so that's PNP type.
You need a resistor between pins 3 and 4, and pin 2 is tied High or Low to determine whether you get a high output (Light On) or a low output (Dark On) if I'm reading the Operating chart correctly. Looks like you can use a pot there to adjust the sensitivity.
Use a 150 ohm resistor in series so the current does not exceed 100mA.
Pin 1 is +12, and Pin 3 is Gnd.

CrossRoads:
Okay, so that's PNP type.
You need a resistor between pins 3 and 4, and pin 2 is tied High or Low to determine whether you get a high output (Light On) or a low output (Dark On) if I'm reading the Operating chart correctly. Looks like you can use a pot there to adjust the sensitivity.
Use a 150 ohm resistor in series so the current does not exceed 100mA.
Pin 1 is +12, and Pin 3 is Gnd.

The resistor is between pin 3 and 4 of the Sensor ? Or Arduino pins ?

Im using dark On

PIns 3 and 4 of the sensor.

Look at the PNP-type diagram in in Input . Output Circuit Diagrams in the data sheet you linked. The LOAD box shown is the resistor CrossRoads is talking about.

Yup.

That what I've done and don't worked, it still giving print of "1" when its high and when its low, aways

1-The red wires are the 12v
2-Black Cables ar 0v
3-Yellow Cable is the PIN 4 output from Sensor
4-Green Cable is the wire from PIN4 output to pin 2 of arduino
5-Blue cable is the exit of the resistors to 0v (pin3 of sensor)
5-I used 3 resistors of 220ohms
6-The green cable is plugged at the third resistor. The same that go to 0v from pin3 of sensor

Thats my code:

int inPinX2 = 2;

int valX2 = 0;

void setup()
{
   Serial.begin(9600);
  pinMode(inPinX2, INPUT);  
}

void loop()
{
 valX2=digitalRead(inPinX2);

    if (valX2 == HIGH) 
    {
       Serial.println(valX2); 
    }

}

You have nothing there to print 0 if it ever goes low:

if (valX2 == HIGH)
{
Serial.println(valX2);
}

You can add this:
if (valX2 == LOW)
{
Serial.println(valX2);
}

CrossRoads:
You have nothing there to print 0 if it ever goes low:

if (valX2 == HIGH)
{
Serial.println(valX2);
}

You can add this:
if (valX2 == LOW)
{
Serial.println(valX2);
}

I already tried that, it give 0 and 1 aways, its not reading right, if i put of the sensor it still giving 1, its not reading if is high or low, is the wires right ?

The LED works right if I plug one for debug, I just want to read if this led is HIGH or LOW on pin2, I had sucess on pin13 but I will use 6 sensor, so I need more pins doing the same thing to each one read HIGH or LOW

Green cable needs to go to the other side of the resistors, connected to Pin 4 of the sensor. The way you have now, it should only measure 0.

Are you sure the ground cables are plugged into the same row as the resistors? It looks like they're off by one in the pic.

Jiggy-Ninja:
Green cable needs to go to the other side of the resistors, connected to Pin 4 of the sensor. The way you have now, it should only measure 0.

Are you sure the ground cables are plugged into the same row as the resistors? It looks like they're off by one in the pic.

But its plugged with Pin4, yes, the blue cabe conected with 0v

I don't need nothing of that on PIN 13 of arduino, it work perfectly on pin 13, but dont work on the another digital pins, I only want to make it work on another pins like it worked on PIN 13.

I don't used resistors on pin 13, I just pluged a led with output on yellow wire and ground on blue wire conected with the 0v , and plugged green wire on the output positive side of the led.

Why it work on pin 13 and not on another pins ?

fredvicentin:
That what I’ve done and don’t worked, it still giving print of “1” when its high and when its low, aways

1-The red wires are the 12v
2-Black Cables ar 0v
3-Yellow Cable is the PIN 4 output from Sensor
4-Green Cable is the wire from PIN4 output to pin 2 of arduino
5-Blue cable is the exit of the resistors to 0v (pin3 of sensor)
5-I used 3 resistors of 220ohms
6-The green cable is plugged at the third resistor. The same that go to 0v from pin3 of sensor

I’m going with your picture here, and this description.

Your green wire is in the wrong place. It needs to be in the same row as the yellow wire, DIRECTLY connected to pin 4 of the sensor. The resistors are acting as pulldowns for the PNP transistor in your sensor. On Arduino Pin 13, the resistor+LED combo that’s on the board acted as that pulldown.

Also, I don’t see a ground wire coming from your Arduino. Is there one there? If not, you need one.