General interfacing advice using optocouplers

That R? 4.7k ohm resistor is really not required, just enable the internal pull-up resistor for the input pin being wired to.

pinMode(Pin#, INPUT_PULLUP);