Detect if sensor is attached

(first post, be gentle =P)

**My problem: **
I have an ATMega2560 where I have attached DHT11 sensor(s). I want my software to detect if the sensor is attached to one of the "approved" digital pins (approved = the pins I've specified in my code, in this case either d-pins 51-54, for a max of 4 possible DHT11 sensors). For my project, it doesn't matter if zero or all four sensors are plugged in, it's dynamic. I do not need to detect what type of sensor - I don't really care only because I assume that there isn't a good way to detect a DHT11 from something else. If there is a way to detect the type of sensor that's a game changer for me, but my research says it's not possible.

I'm using this sensor-detection to drive my TFT screen for displaying a "UI Widget" that shows the sensors values. If 4 sensors are plugged in, I'm going to show 4 widgets. If 1 sensor, then only show 1 widget.

**What I've tried: **
My thoughts were to use digitalRead(). It seems to work in that the simple detection of a "0" (for when no sensor is plugged in) and "1" (for when a sensor is plugged in) is enough for my purposes. I'm just nervous about moving forward with this as I don't know the consequences, risks, or 'gotchas'.

My question:
If not what I'm currently (planning on) doing, what's a good way to detect if "something/anything" is plugged in and using the digital pins? Is what I'm doing 'approved'/safe/a good idea?

What I've read/researched so far:

Thank you in advance and glad to join the forum/community.

  • Define the output.
  • set it to high - then wait 1000ms
  • set flag to 'no sensor'
  • set pin low - wait 25ms - set pin high
  • define pin input_pullup
  • repeat 500 times: read pin. if low: set flag to sensor detected - break loop
    or
    just read the sensor as if it is there.. Take a look at what you've got back
  • Define the output.
  • set it to high - then wait 1000ms
  • set flag to 'no sensor'
  • set pin low - wait 25ms - set pin high
  • define pin input_pullup
  • repeat 500 times: read pin. if low: set flag to sensor detected - break loop

Is this because with the digital pins I can catch it on the 0 bit between digital high/low pulses? And sampling 500 times is a way to eliminate that chances of getting a false positive?

Additionally, why the magic numbers of "1000ms" and "25ms"

  • set it to high - then wait 1000ms

I thought that you could only set output pins high/low. Since this is a digital input pin, does this still work and is it safe my sensor/board?

Thanks for the quick reply.