This small tutorial will explain how to build a circuit for optocoupling Arduino input PINs…
An optocoupler is an electronic component that transfers a signal between two circuits with a galvanic isolation, i.e. without current flow between them.
The optocoupler usually consists of a LED and a photoresistor/phototransistor: the input signal “powers on” the LED and the photoelement detects incoming light and generates electric energy:
With Arduino sometimes you need to “read” input signals with a voltage higher than the value Arduino’s PINs can tolerate or you want – for safety – to isolate high voltage (for example mains) from the microcontroller.
In this example I chose a widely-used optocoupler, named 6N137 (datasheet).
The schematics is very simple:
The input signal is sent to the PINs of the internal LED (2-3) through the R1 resistor that limits the current. The datasheet says that the recommended maximum input current is 15mA: a resistor of 1Kohm is therefore perfect if the input voltage is 15V.
A pull-up resistor is connected to the output PIN (6) and the IC is powered using PIN 5 (ground) and 8 (VCC).
The circuit above is so simple that I built it on a perfboard:
At the and, a photo with my circuit connected to Arduino: