How do I wire a port so that it can sense a switch input OR drive a relay?

I want to wire 4 of my ESP32 ports so that they can be connected to a switch port (PIR, reed switch security sensor, high/low water float switch, etc) OR drive a relay to control a pump, fan, lights, etc. via a common terminal strip with screw terminals (for space reasons). Whether the port acts as an input or output would be selectable in software by the user.

My question is how to wire the output so that it’s functional and safe. Here are questions that need answering, from the perspective of ONE of these 4 ports:

  • - Can this be done with a single MC pin or do I need 2 pins for each port - one for input mode and one for output? And how would I wire them? I can devote 8 pins to create 4 dual-mode ports. Using a single pin for each port would be more elegant but I can't think of a safe/easy way to do this.
  • - I think using a relay driver chip to deal with the inrush current makes a lot of sense. ([https://www.digikey.com/product-detail/ ... ND/3028443](https://www.digikey.com/product-detail/en/diodes-incorporated/DRDC3105E6-7/DRDC3105E6-7DICT-ND/3028443)) Driving a relay with a 2N2222 is possible too, but in either case, from what I've seen in best practices the transistor/relay driver is on the GND side of the relay (low side?), which changes things (see below).
  • - How do I protect against, well, users? Short circuits, incorrect wiring, etc.
  • Below is what I had originally intended, but I’m not confident this will work because of relay inrush current, short circuits, etc., and it won’t work with those driver chips which would solve the inrush problems.

    The idea was to use 2 microcontroller pins for each port on the terminal strip - one for inputs, one for outputs. In input mode, I would set the input pin as such and pull it up. I would set the output pin to low, to switch off the transistor. In output mode I would set the input pin to HiZ then turn on the output transistor whenever the relay should be energized.

    https://content.screencast.com/users/Da … -06.57.png

    Ooops - I see that should have been a PNP transistor, not NPN…

    You can’t really do both at the same time, but you might be able to construct a circuit and carefully craft your code to switch back and forth between input and output. If done quickly enough, it might work.

    There’s a forum post in the Arduino Forum that might help. A link to that is below.

  • https://forum.arduino.cc/index.php?topic=85483.0
  • Also, if any other forum users have suggestions, we would love to hear them. I’m sure there are some crafty ways to make something like this happen although you’d probably need to use a single I/O pin on your micro rather than two.

    Yeah, I don’t want to do both at the same time. I want to use the same screw terminals on the terminal strip for either operation. The issue is more about case size at this point, so if I need to use 2 pins on the mcu that’s ok. Frankly, that’s the only way I can see to do it right now.

    The real question is how to construct the circuit so that it’s safe for either operation and not easily subject to shorting out or other problems.

    I believe the circuit below will work, and I’ve included a fake relay tied to the connector to help me visualize it, but I don’t really like putting 5V directly from the power supply out there on every other pin in the connector (1, 3, 5 and 7). Only one circuit is shown here - I would duplicate this 3 more times for a total of 4 dual-mode output ports.

    In any case, in this circuit if I wanted to sense a switch closing (not shown) I would set pin 24 to output low, turning off the transistor, and set pin 23 to input then do a digital read in the loop. The 10K resistor should protect the input port (23).

    If I wanted to drive a relay (shown) I would set pin 23 to HIZ (high impedance) and then use pin 24 to control the relay.

    I’m worried about someone frying the power supply by shorting one of the 5V pins to ground - resettable 2A fuse on the main 5V leg? Or frying a transistor because of a bad relay. Could use some input if you have it…

    https://content.screencast.com/users/Da … -19.41.png