How to detect if a sensor is connected to a digital input

A reliable solution would be to use a second input, connected to GND through a strong pull-down (1-5k). So you have create a connector with three pins: GND, signal and the detection.

Now if you set the pinMode to INPUT_PULLUP the internal pull-up resistor (typically about 30k) is too weak to pull the pin >1.5V ( 0.3 * Vdd according to the data sheet, so 1.5V for a 5V Arduino), so a LOW reading on the pin means sensor connected, a HIGH reading on that pin means sensor not connected.