OK, looking at my circuit in post#3.
The input to the CPU has a 10k pull-up to 5v.
When the opto is off, there will be 5v at the CPU input. (lets call this the "1" state")
In the "0" state, lets say we want 0.5v at the CPU input, so we must drop 4.5 volts across the 10k resistor.
4.5v across 10k means that 0.45mA must flow in the resistor, and through the transistor side of the opto.
Looking at the datasheet for the opto, it has a current transfer ratio (CTR) between 80% and 600% depending on the exact model of opto.
Let us assume a CTR of 100%. So to have 0.45mA flowing in the transistor, we must have at least 0.45mA flowing in the diode.
In my circuit I just set the resistor in the diode side of the opto at 560 Ohm.
There is a 12v supply, so when the input is short circuited by the switch the current flowing in the diode will be about (12 - 1.5)/.56 = 18.8mA. (The 1.5v in the calculation is the diode forward voltage drop).
The opto will be well and truely saturated.
We could increase the value of the 560 Ohm resistor quite considerably and still be well saturated.
Lets make it 4.7k so we get (12 - 1.5)/4.7 = 2.2mA in the diode.
The circuit should still work quite well, the opto will be saturated.
Noise on your 50 meters of wire.
There has to be a circuit, so we need two wires, one going out to the switch and one returning from the switch.
Are these two wires running together in the same cable?
If not, why not?
One of the most basic ways of minimising stray coupling to magnetic and RF fields is to run the go and return wires closely together.
If the wires must be separated for whatever reason, a simple re-arrangement of the circuit and adding a capacitor should fix it.
See the attachment below.
JimB