-1
\$\begingroup\$

I want to use a single GPIO pin as optocoupled input and output (at random discretion). Will the following scheme work for any possible combination of input and output? Especially when optocoupler 1 is open but GPIO output set to low?

GPIO input and output simultaneously

\$\endgroup\$
  • \$\begingroup\$ You don't need R1 as long as the incoming opto is never driven while GPIO is set to be an output. \$\endgroup\$ – DKNguyen Jul 23 at 21:37
  • \$\begingroup\$ Unfortunately, the opposite is true: input is almost always driven high. \$\endgroup\$ – sa7 Jul 23 at 21:55
  • 1
    \$\begingroup\$ Afraid not. Input circuit will need to drive the GPIO pin. Output circuit won't know if the GPIO drove that level or the input circuit. And you can't keep the input circuit off the GPIO pin while the GPIO is trying to drive out. \$\endgroup\$ – TonyM Jul 23 at 22:05
1
\$\begingroup\$

This could work, but whenever the input optocoupler is turned on, the output optocoupler will also turn on (assuming that the current from the first is sufficient to turn the other optocoupler on. Even if the current is not high enough, the LED of the second will be 'slightly' on, which will allow current through the other side of the optocoupler, the CPC1018N only needs ~0.25mA of LED current to turn on.)

It's not worth it to gain an extra gpio. If you need an extra gpio there are better ways to get one than trying to worry about current through an optoisolator.

\$\endgroup\$
  • \$\begingroup\$ You are right. Unfortunately, I limited in budget and parts availability. What if I try R1=15k, R3=50k? \$\endgroup\$ – sa7 Jul 23 at 22:33
  • \$\begingroup\$ Why can't you put the other optocoupler on another port on the orange pi? Have you ran out of ports? \$\endgroup\$ – Voltage Spike Jul 23 at 22:36
  • \$\begingroup\$ Yes. The more devices I control with this orange pi the better. Each device has an input an two outputs (counts for 2 gpio, if I succeed with this question). We plan to deploy 10-15 devices. \$\endgroup\$ – sa7 Jul 23 at 22:43
0
\$\begingroup\$

By definition, you can't use a digital pin as an output pin and also as an input pin at the same time.

Suppose the output circuitry in the pin is trying to drive a given logic level but the external input circuitry wants the pin to be read as the opposite logic level. The input circuitry would have to source or sink a great deal of current to overdrive the output circuitry, and even if that actually happened it would mean that the "output pin" is no longer at its correct logic level.

Most processors will allow you to reconfigure a pin as input or output on the fly, but you can't have a logic '1' and a logic '0' on the same pin at the same time.

\$\endgroup\$
  • \$\begingroup\$ Of course not at the same time, but at my discretion: I want to read optocoupler 1 state when GPIO is set to input mode or to control optocoupler 2 when GPIO is set to output mode. \$\endgroup\$ – sa7 Jul 23 at 22:05
  • 1
    \$\begingroup\$ The phrase "at random discretion" does not suggest that the pin will never be an input and an output at the same time. You go on to say "any possible combination of input and output"...how are we supposed to interpret that? \$\endgroup\$ – Elliot Alderson Jul 23 at 22:11
0
\$\begingroup\$

The problem is dealing with (or avoiding) collisions.

To deal with collisions you need one output for your TX state, and one simultaneously-active RX input to detect when the TX data you sent doesn't match. You would implement a protocol on top of that to deal collisions though retries, acknowledgements and data integrity checks.

If you have control over the TX and RX events - for instance, the other devices don't send data until they're asked (polled) for it, then you don't need to detect collisions and your approach might work.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.