[noob] Using optocoupler for long-distance input


I'm wanting to detect a switch's logic state over a long distance (20ft). I've read a couple of solutions, but I believe the simplest is using optocouplers. I have a schematic (don't roast me too bad on it.) and was hoping I could get some insight.

There are four switches. The ground doesn't need to be separate. The input voltage will be 5v and will be powered by USB.

Any help is greatly appreciated.


The coupler used is a 6N138.

IP[1-4][+-] is the switches leads. So one end is connected to 5V, the other is connected to a designated coupler. And the coupler's output goes to OP[1-4], which is passed into the Arduino.

The Arduino will be using the internal pull-up resistor.



Sorry. I failed to mention that the switches will be closed very momentarily. They are actually pressure pads; ones that when you step on closes the circuit.

That is a high speed opto used for speed communication.
To detect manual switches a lot cheaper optos would do.

1 Like

Thanks. I wasn't sure. Do you have any specific model in mind?

Detecting a switch open/closed over 20ft doesn't need anything. Just wire them up to the arduino. I've had switches further away than that without issue.

I just updated the post, but the switches will be closed for a very short period of time and my main concern is that there will be interference from outside factors and the Arduino may detect a closed state when there isn't one.

Using a "good" pullup on the signal and switches connect to GND quite some distance can be handled. If the cable must pass an electric welder station measures might have to be taken like shielded cable.
I hope OP is not a prepper preparing for an EMP from an atomic bomb...

Okay. I had read that too, but had also read that using an opt-coupler or even differential line drivers might be preferred.

By "good" do you mean strong? As in:

A low resistor value is called a strong pull-up (more current flows), a high resistor value is called a weak pull-up (less current flows). source

Yes, a low resistor, really lower than the 10 kOhm offered by Arduino INPUT_PULLUP.

1 Like

The opto-isolator circuit may help but ONLY because you have a lower value resistor on the inputs... Try it directly into the inputs with the built-in pull-ups. If there is interference (false triggers) add a lower value pull-up, maybe 1K.

Or, if you can use normally-closed switches that will also kill any interference.

You'll probably be OK, but a digitalRead() only happens for an instant every time your program loops. This is called "polling" and if your code is doing "other things", or if there are any delay()'s, or if the switch just isn't closed long enough you can miss the temporary switch closure.

If that's a problem you may have to use interrupts. With interrupts the input is constantly monitored and the interrupt routine runs with triggered.

1 Like

AVR pull-up resistors are 20k to 50kOhms (quite weak)

OP is using a SAMD21 board.

Just use twisted pair for that distance.
1k pull up (to 3.3volt) is good, but that can't even keep RF out.
If RF interference is a problem then also add a 10n ceramic cap from pin to ground, close to the pin.

1 Like

Ended up using a 2.2k ohm resistor and 1µf ceramic cap. Thanks!