Control other circuit with a transistor.

Thanks majenko, I'll check that out.

MarkT, do we understand each other correctly? The PD2 is in INPUT mode... I can't control anything with that pin...

The optocoupler is used to control the motor movement. If the switch is pressed, I want to light up the LED and stop the motor movement.