Q:

Combining Optoisolator and Relay into one I/O pin

I have a project that uses two microcontroller pins to control the input and output of an isolated device.  The LEDs are for status, showing when an input or output is enabled.
I'd like to save some I/O pins and I was wondering how to approach combining these together to use a single microcontroller pin?

A:

It's possible. You must have a very small relay to be able to drive it directly from the uC pin. You should have a Schottky catch diode across the relay coil. If you use a driver transistor you can use a regular diode as below. 
The method would be something like this: 

simulate this circuit – Schematic created using CircuitLab
To measure the input, tristate the MCU I/O pin for (say) 5 microseconds. It will go high or low depending on Q1 (the optoisolator output). Grab the value and return the pin to its desired state. Repeat the polling relatively infrequently (say at 200Hz). The relay will have insufficient time to drop out or pull in. 
Note that the above circuit has the relay energized for output high rather than low. 
Generally though, it's often better to move to a micro with a surfeit of pins rather than trying to cram everything into a few. There is inevitably a new feature or function that could use an extra pin. 

