Electrical – How to accept switched GND or switched Positive on a 3v3 MCU input

microcontrollerpowerpulldownpullupswitches

I am making a device based on a STM32F105 (3v3) that has to accept inputs that I am not in control of, for example in a vehicle or other wiring that I cannot change.

The device is to have 16 inputs.

I want the input to be selectable between active low and active high. Because sometimes I might come across a switch that is switched to GND and sometimes one that is switched to say 30v.

All inputs have to be 30v tolerant.

I would like to have the option of selecting in firmware whether the input is connected to a switched Ground or switched positive.

How can I go about accepting inputs that could be switched GND or on other occasions be switched 24v?

For example I may come across either of the following so I need my circuit to be able to accept both:

enter image description here

Best Answer

How about:

This assumes a 5V microcontroller, common ground between 5V and 30V circuits and suitably chosen mosfets.

Note that M1 (P-Channel mosfet) is installed with the source facing the 5V rail.

When your microcontroller outputs a logic high, the N-Channel turns on and the P-Channel turns off (simultaneously) which enable the pull-down R3. A logic low will do the opposite and turn on the pull-up R2. D1 and R1 are the protection circuitry to drop the 30V level down to something the micro will be happy with.

Additional info: This circuit can be modified slightly to put the pull-up/down on the high-voltage side if you want that instead, just move D1 & R1 to be on the microcontroller side and use mosfets suitable for 30V switching.