Replacing push button by light barrier, microphone, ...

For a particular sketch I have a simple pushbutton which is connected via a push-up (pull-down?) resistor to an input. The connection is as follows: 5V - pushbutton - bifurcation to input - resistor - GND. By momentarily pushing the button I change the input from low to high and can react on this in the sketch. The other way round also works: If the push button is permanently pushed and then opened, my input changes from high to low and I can again react in the sketch. Basically, this is a simple external cable loop, which by opening or closing triggers an event in my sketch.

My question now ist, how can I just replace the pushbutton by any other device like a light barrier, a microphone, ... which would open or close the loop in just the same way. I think I will have to replace the button by a transistor which will be activated by the device and which will then open or close the loop. Only I do not know how to realize this exactly. If anybody could show or point me to an example circuit to realize this for a light barrier and a microphone (including required connections to power supply for the device), I would appreciate very much.

Thank you very much.

Change your design to use the internal pullup resistor:

pinMode(inputPin, INPUT); digitalWrite (inputPin, HIGH); // enables pullup

and then an NPN transistor to pull the pin Low, and react on the Low.

Part Like 2n2222 or BC547. Collector to the pin, emitter to ground, and whatever driving the base High to trigger the pin.

The input could be driven by the output of a comparator - the + input pin of the comparator connects to your microphone, the - input pin to a voltage level (2 resistor divider between +5 & GND, with outout above the quiet level of your microphone. When the mic gets loud, the comparator output goes high, the transistor turns and pulls the pin low.

Or just drive the pin from the comparator output in this case, no transistor needed.