Detect open or closed circuit via USB sensor

raspberry pirelayresistancesensor

I have a relay which I need to detect the state of (similar to this question but no current flow) and would like know what the easiest method would be to detect the state via USB and Raspberry-pi. I assume the simplest method would be to detect the resistance, but my lack of knowledge of what's available out there makes it hard to even know what it is that I'm looking for.

Best Answer

A relay is effectively an electrically operated push-button, so you can use a similar circuit to detect relay contact closure as for a push-button being pressed.

Connect a resistor in series with the relay contacts and apply voltage to the circuit. When the relay contacts are open no current flows, so there will be no voltage across the resistor and full voltage across the contacts. When the contacts are closed they apply full voltage to the resistor and current flows (limited by the resistance) while voltage across the contacts drops to zero. This voltage change can easily be detected by digital logic.

The circuit can produce either a logic '1' or logic '0' when the relay closes, depending on the whether it switches to the positive or negative side of the supply voltage. Switching to ground may be safer and require fewer wires. The resistor then pulls the input voltage up (producing logic '1') when the relay contacts are open.

schematic

simulate this circuit – Schematic created using CircuitLab

The Raspberry Pi's GPIO port has pull-up resistors built in, so you would only need to connect two wires to the relay. Unfortunately it is not quite so simple for USB. You cannot connect relay contacts directly to it. USB is a complex serial protocol which requires an intelligent device to convert the voltage change into a signal that can be transmitted over USB.

You could make a special USB device just for your purpose (and write a device driver to interface with it) but it may be easier to just use an existing USB device which has general purpose I/O (eg. TTL serial or parallel adapter) or even take apart a USB mouse or joystick and connect the relay contacts across one of its buttons.

Related Topic