Can a transistor safely switch digital inputs?

I have read numerous topics on detecting high voltages on 5v microcontroller input pins. It all seemed quite complicated, until I remembered how NPN transistors worked: a voltage on the base allowed current to flow from the collecter to the emitter (or something like that).

So, if you can use a microcontroller output pin on the base to control 12v on the other two pins, can you use 12v on the base to control an input with a pullup resistor by tying it to ground? Or will I let the magic smoke escape?

This is an appropriate use of the transistor. This circuit is commonly used. However you need a 10K resistor in the base lead.

Weedpharma

So... something like this?

Yes. Any general purpose NPN transistor can be used.

Weedpharma

Keep in mind that if, for some reason, the transistor junctions are damaged, your MCU can have 12V on its input.
Providing galvanic isolation (with an optocoupler for example) between the digital circuitry and the high voltage circuitry can avoid this.
It is, of course, not needed, but it does reduce the chances of burning the MCU if something goes wrong.

Is the large voltage always 12V, or is that just an example? You only need the transistor if you want a wide range of voltages to appear as 'connected'. Or an optocoupler if there isn't a common ground or large voltage spikes.

The circuit where 12v is either present or absent could just be a potential divider to ground.

Circuit number two is all you need unless you want to isolate the grounds for noise consideration.

A resistor divider is simpler and more robust if the 12V is a well defined 12V. A 15k/10k
divider will take 12V down to 5V nicely.

My concern with R dividers is that if the gnd connection is lost, the full voltage appears on the input.

Weedpharma

No it doesn't you have a relatively high resistance leading to the pin's input protection
diode. You can add a zener across the bottom leg or an extra protection schottky diode
to the +5V rail if you want, but if you limit the divider current down at < 1mA level you
should be OK - certainly not high enough to trigger latch-up in modern devices.

MarkT:
No it doesn't you have a relatively high resistance leading to the pin's input protection
diode. You can add a zener across the bottom leg or an extra protection schottky diode
to the +5V rail if you want, but if you limit the divider current down at < 1mA level you
should be OK - certainly not high enough to trigger latch-up in modern devices.

I wanted to use a transistor instead of an R divider because I prefer to keep the BOM small. Adding a bunch of diodes and unnecessary resistors defeats my entire purpose.

By the way, the +12V is just an example. It just represents something >5V5.

EDIT: By the way, it would be extremely helpful to provide some simple schematics.