8

I'm trying to build a system where if a cable gets disconnected from a pin (any pin), an action is executed.

I am wondering how to set this up. Can I simply wire the 5V to a pin and read its status or will this mess up the Arduino? Should I use any resistors? How should I connect this to the GND?

Please bear in mind that I'm pretty new to all of this.

2
  • Please state whether these pins are going to be used for input or output. If output, what are they driving? eg. LEDs. If input, what is the device you are reading from?
    – Nick Gammon
    Aug 29, 2015 at 23:41
  • I am not really sure if I should use them as an input or output. The idea is that it triggers a software event and not a physical event (hardware). All I really need is a signal to the computer (though USB for now) that says that the cable is disconnected. Aug 30, 2015 at 10:37

3 Answers 3

9

The way to do this, since there is no digital value in between HIGH and LOW, is to actively create a signal on the pin you can recognise. Note that if the signal coming in to the pin is a datastream (a sequence of HIGH and LOW) that runs all the time this may not work reliably, but it's good for things like buttons and other simple sensors.

Any signal on a wire has a drive strength. This is basically how powerful the signal is and how good it is at overriding other things happening on the same wire. It is closely related to the output impedance of the source (what is sending the signal). For things like buttons and switches that is basically the value of the pullup resistor used.

To actively sense if a pin is disconnected or not you need to temporarily connect it to a known signal level at the same time as it's connected to the wire (or not connected if it happens to be not connected). This known signal level has to be of a weaker drive than the source signal by several orders of magnitude. This ensures that if you try superimposing your test signal over the incoming signal the incoming signal swamps out the test signal and you don't see it at all. For instance, if your source has 10KΩ impedance (say a button with 10KΩ pullup resistor) then a test signal with source impedance of 1MΩ would be suitable.

Each pin to be tested can be connected to its own 1MΩ (in this example) resistor, and those resistors are all connected to the same extra IO pin:

enter image description here

In this example pin D2 is the test pin, and pins D4-D7 are your inputs.

So under normal circumstances pin D2 is set as an input and completely ignored. When you come to test the pins, though, you set it as an output.

You then set D2 to be HIGH and read each of D4 through D7. Any that read as HIGH are candidates for being not connected.

You then set D2 to be LOW and read the pins that read HIGH before. Any that now read as LOW are disconnected.

You then set D2 back to being an input again so it doesn't interfere with normal operations.


If you just want to know if current is flowing in a wire you can use what is called a shunt resistor. This is a small resistor (maybe 1Ω) that is placed in-line with the wire and you measure the voltage (using Arduino analog inputs) at both sides of the resistor. The voltage dropped across the resistor is directly proportional to the current flowing through it thanks to Ohm's Law:

I=V/R

For instance:

enter image description here

If the voltage read by A1 should be 5V (the voltage the wire is connected to) and the voltage read by A0 should be 4.93V, the voltage dropped by the resistor would be 5-4.93 = 0.07v. With the 1Ω resistor the current would be 0.07/1 = 0.07A, or 70mA.

With the wire at OUT disconnected there will be zero current flowing since there is no circuit. So re-arranging the formula above we can prove that we can tell it's disconnected.

If I=V/R then V = RI. Therefore V = 1Ω × 0A = 0V. So the voltage dropped across the resistor would be 0V, so A0 and A1 would be equal (discounting any noise). A0 and A1 would both read 5V. Similarly, if the wire IN was disconnected instead then A0 and A1 would both read 0V.

For sensing higher currents you use a smaller resistance, and for sensing smaller currents you use a larger resistance - this allows for a voltage drop to be generated that is within a suitable range for the sensitivity of the ADC doing the reading. For the Arduino that's a 10-bit ADC, so it can sense (with a 5V VREF) 5/1024=4.882813mV per LSB.

Note that if you are working with voltages above that at which the Arduino operates you will need to separate or otherwise scale those voltages. There are special devices designed especially for this task called high side current shunt amplifiers. They also allow you to use a smaller resistance than you would otherwise need which is less intrusive to your circuit (less voltage drop).

1

You can connect all pins through a 1M resistor to a rail. Once in a while you set all your outputs to inputs and toggle the rail HIGH and LOW, if the inputs are HIGH and LOW in the sequence you toggled the rail, there is no wire. The same question was asked at Arduino Forums so read this thread.

This question's answer was provided by [email protected].

1
  • 1
    Thank you, but this looks way more complecated than it should be for my purpose. I am not using any external devices and/or sensors. What I am looking for is a way to detect if current is flowing from 5V to GND (cable connected) or not (cable disconnected). You could think of it as a weird electronic necklace that will make sound once it is disconnected. Now how do I determine when to make the noise? Aug 29, 2015 at 9:40
1

Alternatively, if this is a digital input, you can just use a pull-up or pull-down resistor to give the input pin a default value. Let's say you use a pull-up resistor to make the default high. Then you connect a low cable to this input so that the input reads low. Now, if the cable goes high or if it is disconnected, you get a high value in the input pin.

I know this was not quite what you were asking but maybe such a solution could help with your original problem. In my case, I have a flame detection module that gives low output by default and turns high when there is a flame. I don't want to miss the flame because of my cable disconnecting, so I use a pull-up resistor that will make the input high not only when there is a flame, but also when there is a disconnection. This way I make sure that I don't miss a fire because of the sensor cable disconnecting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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