Opto-isolator or opto-coupler are a device that is used to prevent high line voltage from damaging the inner components of a circuit that usually runs on smaller voltages. Normally can be found in the input and output modules of Programming Logic Controller (PLC). There are two type of opto-isolator : Diode opto-isolator and transistor opto-isolator. Basically the external electrical signal from sensors or switches is transferred to the microcontroller by means of light hence providing the following advantages:
a. Block high voltage from entering the low voltage circuits by separating then
b. Prevent transient surge signal from entering
c. Immunity from electromagnetic interference (EMI)
For example in the circuit above the relay will be energized when the switch is close ( turned on).
Transistor opto-isolators are more sensitive than diode opto-isolator because of the higher current gain provided by the transistor type.
Below is the opto-isolator circuit building and simulation using EasyEDA