internal pull-up with optocoupler problem

Hello.
I need to connected 24V laser proximity sensor to arduino. To keep power lines separately I used optocoupler(4N32). For the schematic, see the attached pic. I want to use only internal pull-up. But a problem came up.
I wrote this simple test code:

  int S1= 2;

// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);   
  Serial.print("started");
  pinMode(S1,INPUT_PULLUP); 

}

// the loop routine runs over and over again forever:
void loop() {
   if(digitalRead(S1) == LOW){
     Serial.println(millis());
     Serial.println("pin LOW");
     delay(500);  
  }
}

And as a result it allways prints to me "pin LOW". Voltage on that pin is 4,2V. Which should be "high". Why doesnt it work? Why it has a voltage drop, eventhough sensor output is 0V?

Hi, you are only testing and sending the LOW state, you will have to test for HIGH and send high .

Tom..... :slight_smile:

TomGeorge:
Hi, you are only testing and sending the LOW state, you will have to test for HIGH and send high .

Tom..... :slight_smile:

But how can it execute "Serial.println("pin LOW") command, when the IF sentence condition is not true?

I tried testing high state also. It works most of the time, but still sometimes it says "pin low" when the sensor is not activated. (see the pic).

I would suspect that it is not wired up like you think.
How about a photo correctly sized?

Grumpy_Mike:
I would suspect that it is not wired up like you think.
How about a photo correctly sized?

You mean that i wired it up wrongly? I dont think so. If i add external pull-up it works perfectly. Just cant figure out, why not with the internal pull-up.
What is the max pohoto size? :* I just used win snipping tool...sorry

If i add external pull-up it works perfectly

If you withhold vital information you can not expect to have a correct reply. So what value of external pull up did you use? It is important.

What is the max pohoto size

Don't post anything greater than 1000 pixels wide.
There is a "How to use this forum" sticky post please read it.

Hi, please look at.

http://forum.arduino.cc/index.php/topic,148850.0.html

Gives you ll the hints on our to get the most out of your post.

Tom... :slight_smile:

I suppose it's possible that the internal pullup is stuffed; have you tried a different pin?

JimboZA:
I suppose it's possible that the internal pullup is stuffed; have you tried a different pin?

It is indeed, that was going to be my next point. 8)

Grumpy_Mike:

JimboZA:
I suppose it's possible that the internal pullup is stuffed; have you tried a different pin?

It is indeed, that was going to be my next point. 8)

Or as The IT Crowd fans would know... have you tried turning it off and on again mate?

Sorry for delayed answer. The problem was due to using breadboard. After I made soldered connection everything worked ok. :roll_eyes: