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.
Specifics:
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.
Thanks!
edit
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.
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...
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.
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.
Leo..