I would like to know if there is a way of knowing if a relay is connected/disconnected.

If I try:

GPIO.output( self.PIN , 1 )
            if( GPIO.input( self.PIN ) == 0 )
                    self.value = 'Device not connected'

It returns 1. Even if the relay is disconnected.

Is there any other way to approach this problem?

EDIT: The device that I'm working with is SRD-05VDC-SL-C 2-relay module.

  • Dozens of ways I'm sure, all needing hardware. Why can't you just look? – joan Dec 14 '15 at 10:53
  • I've looked for a solution, but couldn't find anything. – Stef Dec 14 '15 at 11:03
  • 1
    That's because any solution must be tailored to the specific item you want to detect. – joan Dec 14 '15 at 11:07
  • So there is not general way of knowing if a device is connected or not to a certain pin...I guess I'll keep on looking. – Stef Dec 14 '15 at 11:10
  • Could you define 'disconnected'? Is the relay plugged into the pin, or is the relay in the off state? If you are trying to detect if the relay is in an on or off state, provided you are controlling a DC voltage, regardless of voltage, you could set a different pin to input pullup, and then do a reversed biased diode. If this works, I can go into more detail in an answer. – Butters Dec 14 '15 at 20:30

Doesn't this code GPIO.output( self.PIN , 1 ) always set the pin to high? You set the pin high, and then read it. That will always read 1. Try removing that line.

Set the pin to input first: GPIO.setup( self.PIN , GPIO.IN )

Then check if it is high or low. Then change the behavior of your code based on that.

  • Yes, that was a test to see if the pin remains 1. So if I actually connect the relay to a power source I should be able to detect if it is connected to the raspberry. I did not connect the relay to anything yet. Thank you! – Stef Dec 15 '15 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.