Solenoid valve with optocoupler and transistor