HELP Please... The right way to use diodes stop backfeeding on uC DIO lines.

Discussion in 'General Electronics Chat' started by ben.scammell, Mar 9, 2015.

  1. ben.scammell

    Thread Starter New Member

    Mar 9, 2015
    8
    0
    Imagine a standard DIO on a microcontroller.

    If this pin is configured as an output, I'd like it to switch a relay using a darlington driver (such as a ULN2803).

    If this pin is configured as an input, I'd like an opto-isolated input.

    I've attached a schematic, and would welcome feedback on how better to do it.

    This is for a mixed IO board for an Arduino running of I2C via a PCA9534.
     
  2. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,969
    744
    if your using a Uln chip, you dont need the opto, just put the input to uln chip via a 1K resistor and diode,to your arduino output.
     
  3. ben.scammell

    Thread Starter New Member

    Mar 9, 2015
    8
    0
    I think i've failed to explain.. the OPTO side is for when the DIO is configured as an input, and the ULN side for when it's an output. I'm trying make a board that can have 8 outputs (as relay) or 8 opto-isolated inputs depending on how you configure the uC pins.

    I am trying to avoid the DIO back-feeding the opto-isolator when it's configured as an output and high, with the two diodes in the middle.

    Thanks in advance for your help.
     
  4. BillB3857

    Senior Member

    Feb 28, 2009
    2,400
    348
    First, I will admit that I'm no Arduino expert. One potential problem I think I see is that when the opto is not active, diode D3, along with resistor R2, will pull the I/O pin higher than 3.3v, which, from what I've read, is a no-no. I would turn D3 around and change the code as needed. If, on the other hand, you want the relay to be sensitive to the opto condition without first being evaluated by the Arduino, an additional diode would be required. Anode toward the opto and cathode tied to the input of the ULN. At least, I think so.??

    After thinking about it a while, the way you had it would be better except for having the R2 pull-up being tied to 5v. Tie it to 3.3v to avoid the overvoltage on the I/O pin. Turning D3 around, like I originally suggested, would cause a different problem.
     
    Last edited: Mar 9, 2015
  5. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,357
    You don't need 5V supply for the opto-coupler load resistor. Use 3.3V.

    Use jumpers to select input or output. Remove the diodes.
     
  6. ben.scammell

    Thread Starter New Member

    Mar 9, 2015
    8
    0
    Thank you.

    Fair enough on 3v3. I'm trying to avoid jumpers, can it be done?
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,357
    If you don't mind the relays clicking on opto-coupler activity, simply connect without the diodes.

    When selected for relay output there ought to be no activity on the opto-coupler.
     
  8. ben.scammell

    Thread Starter New Member

    Mar 9, 2015
    8
    0
    Thanks,

    Is there any issue of the DIO being high (to drive the relay) back-feeding the opto-coupler?
     
  9. ben.scammell

    Thread Starter New Member

    Mar 9, 2015
    8
    0
    If it was an issue... Is there any way of stopping the opto-coupler side driving the relay?
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,357
    Install the relays only when required.
     
  11. ben.scammell

    Thread Starter New Member

    Mar 9, 2015
    8
    0
    Lol, I know I can do that.. but can I do this electronically?
     
  12. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    The only way of retaining the previous output state while using the DIO for input is to put a latch (flip-flop) between the DIO and the relay driver. That way, you can configure the DIO to out, write to the latch (which defines the relay state), turn the DIO around to input and then read the input state. You only have to configure the DIO to output the next time you need to change the relay state.
     
  13. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,357
    You can disable selected relays but then you would need extra ports and circuitry to do that, which defeats the whole purpose.

    Jumpers are still the easiest solution.
     
  14. ben.scammell

    Thread Starter New Member

    Mar 9, 2015
    8
    0
    I'm not worried about latching the outputs.

    I just want it so if the pin is an input, and you connect an input to the opto-coupler it reads a high or low, and if the pin is configured as an output then the relay switches depending on the pin state... and I'd rather that when the pin is being used as an input, it doesn't drive the output side (i.e. the relay clicking as the input changes state).

    Can this be done?
     
    Last edited: Mar 9, 2015
  15. ben.scammell

    Thread Starter New Member

    Mar 9, 2015
    8
    0
    But the design isn't fit for purpose with jumpers, so while easy, it's not the right answer for me. Thanks for your help though.
     
  16. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    Umfortunately, I don't have Eagle and cannot see your schematic. Can you export it to a .PNG or other graphic format? By the way, .JPGs aren't preferred, as they lose detail.

    Are you short of pins on the Arduino and that is why you're using one pin as both input and output?
     
  17. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,676
    899
    I am pretty sure I don't understand your question, because I don't see the problem when a PIC with output latches is used:

    upload_2015-4-23_14-57-1.png

    As an output (TRISx,n =0) the PIC writes to the latch LATx. Changing the pin to input (TRISx,n=1) doesn't change what the latch was set at, regardless of whether the input is high or low. At least that was the way it seemed to work in simulation, which means you could read the latch before changing TRIS to see what the previous output was.

    John
     
  18. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    But he's not using a PIC; he has an Arduino. And I'm not a PIC expert, so I can't translate what you said into "Arduino"ese.
     
  19. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    I think the reference was to an EXTERNAL latch which would hold the output value, while the pin was changed to an input and sampled. FWIW I don't think there is a way to use diodes to do what you want for the simple reason that they are unidirectional and a CMOS input requires both sinking and sourcing in a driver.
     
Loading...