Interfacing a microcontroller digital IO
It's simple enough.







simulate this circuit &ndash; Schematic created using CircuitLab

Figure 1. Various output options.


If the required load current and voltage is &lt;= the microcontroller's output capability then drive directly from the GPIO pin. (Figure 1a.)
If a higher voltage or current is required then use an external transistor to do the heavy duty work. (Figure 1b.)
Opto-isolators are used when


Electrical isolation is required between the controller and the load.
"High-side" switching is required.
Some other odd circumstances.



Figure 1c shows the microcontroller controlling a mains lamp via a (simplified) opto-triac control circuit. Note that there is no electrical connection between the microcontroller and the mains voltage.