Detection using only the input line can be hard. First, you want to limit the scope of the problem. Consider the non-existent state of the sensor as it is not connected, the sensor is connected but not responding, or the sensor is uncharacteristic.
So, if it is a digital sensor, then communication with the sensor may be sufficient to determine if it is present or not (especially if checksums or parity bits are used). Some analog sensors also have specific characteristics of how they behave when triggered. You can use the deviation from these specifications to determine if there is a sensor.
If you have a digital sensor without any error checking on it, where you synchronize data (so that all 0 or all 1s are valid) or it is just binary 1 or 0 for output, then you will need external help. The same goes for most analog sensors.
This external help would be something when you put the system in a known controlled state, press a button, and then check the sensors for output in a specific range. To be absolutely sure, you would like at least two different states so that your digital or analog inputs are not stuck in the correct state for your test.
Almost any other method will be external to the system. Using an additional IO to “detect” the sensor can help increase confidence that the sensor exists, but you can get false positives when all you know is that “something” is not the expected sensor.
source share