Electrical – Fluctuating digital input on arduino uno r3 when connected to Photocoupler

arduinoopto-isolator

I am trying to use a Photocoupler 4N-25 with an arduino uno r3.

I connected the collector to +5volts and the emitter to ditital pin 0.

Now, since there is no voltage applied to the gate and I haven't even connected the other side of the photocoupler (diode) with anything. Shouldn't it have an indication of LOW in the serial monitor? Instead it fluctuates between 0's and 1's.

I also gave it a try with the gate grounded, nothing…

I tried with a resistor to limit the current to the arduino, nothing…

Any suggestions?

EDIT

By trying out the circuit of @Passerby the input pin was high.
when i turned the gate High the pin went low.
All is good so far.

what i want now is this depicted below.

enter image description here

when i replace the opto-isolator with a simple led (D1) it works. i tried it out.

The problem now is that the led in the opto-isolator is not bringing the gate high. why is this happening.

keep in mind that i only want to use this opto-isolator for this circuit.

and correction the capacitor is 150nF

Best Answer

Typical wiring for a on/off digital signal from an optocoupler is this:

schematic

simulate this circuit – Schematic created using CircuitLab

Pull-up to VCC, most likely 5V for most Uno R3. The pull-up gives it a known state of HIGH when the optocoupler's LED isn't on. When the LED is on, the optocoupler's transistor turns on, pulling the collector towards ground, giving a LOW signal.

Alternatively, wire the collector to the GPIO with the pinMode(x,INPUT_PULLUP) mode enable, using the internal ~47kΩ pull-up resistor. Essentially the same thing.

This is a typical open-collector setup for digital logic.

The Way you had it connected, with the emitter to the GPIO, is a emitter follower, and will show either 5V when the LED is ON or FLOATING when the LED is OFF. A Floating input will not have a fixed state, and will float between states, like an antenna. Use the wiring above instead.