checking if a sensor is available

Hi

i have a project in which i must use android to read temperature from arduino uno using dht11 sensor

What i want to do, is adding a test to check if the sensor is connected or not, because sometimes the sensor is not connected and i would like to send a message back saying that dht11 sensor is not connected

How can i do this with code ?

The dht11 sensor has 3 pins. Two of them are for the power source. If the sensor isn't connected properly, then there is no valid value that you'll get from the sensor?

}else{
Serial.println("The sensor is not connected");
}

but i don't know how to check if the sensor is connected
i'm missing the if :confused:

i don't know how to check if the sensor is connected

What does reading the sensor return if it is not connected ?

actually i just connected the dht11 and i haven't wrote any sketch yet

i don't know how the arduino will react if i try to use the dht library and get temperature before even connecting the sensor

i don't know how the arduino will react if i try to use the dht library and get temperature before even connecting the sensor

I would expect that the sensor will return a value indicating that it has not performed the reading, perhaps not a number. Try it and see.

If you're using the library from the Playground, the .read() method only returns a 0 for a successful read. If there's a timeout (like when the sensor isn't there) it returns -2.

So check the return value of .read() before using the .humidity and .temperature values.

I just tried an idea I found on the web: I have a soil humidity sensor connected to pin A0. I also connected pin D2 to A0 with a jumper wire (no resistor needed). Then I wrote a HIGH signal to D2 OUTPUT, that can be AnalogRead at A0 as a value of 1023. If the sensor senses a "normal" soil humidity it will usually be lower than 1023 and it kinda "overrides" the HIGH signal from D2.

If the sensor works/is connected you get values lower than 1023, if it is disconnected you get 1023 at A0. For me it worked well.