I am writing some logic for a PID controlled catapult (In order to improve precision). That is all fine and well. However, if, for some reason, the encoder wire disconnects, the motor spins continuously in the opposite direction, which breaks my catapult. To solve this, I would like to write a function to catch the failure of the wire, and use that to switch the runmode to not using the encoder. I have both control functions working properly. My issue is the transition. How can I detect when an encoder disconnects.

Note: I have thought about writing a function to checks the return value of the encoder to see if it is disconnected, but I no not know what is returned by the getPosition function when the encoder is disconnected. Is it 0, is it null, or is it something else entirely?

  • $\begingroup$ Normally if you disconnect the encoder and you still read values over the I2C/SPI etc. you get random values. So let's say you are sampling with about a 1khz, then you know how much the motor maximal can turn (maximal rotation change between two sampling periods). If now the absolute value of this rotation change is too large, then the encoder is disconnected. $\endgroup$ May 12, 2017 at 5:31
  • 1
    $\begingroup$ I would say that the solution depends on the type of encoder (relative/absolute) or communication interface you use (QEP, SPI, etc.), and then it depends on the specific wire(s) involved that could disconnect (not to speak of more complicated set-ups that involve daisy chains), so you might want to provide some more information about that. Possbilities also differ for the firmware you are running. For instance, it might be possible to use watchdog timers to monitor input activity. $\endgroup$ May 12, 2017 at 9:12
  • $\begingroup$ In addition, it also depends on electronic circuitry. What happens when you disconnect cables? Do you have pull-up/down resistors? etc. etc. $\endgroup$ May 12, 2017 at 9:18

1 Answer 1


If you have a situation where your motor can break your system, for a small DC motor then rather than relying on a solution which requires the software to detect the condition, instead add limit switches to prevent it running beyond a point by inserting a diode in series with the motor when the switch is activated. For BLDC motors or more powerful motors then have the limit switches feed into the controller instead. If the limit switches are not in series with the motor, then use normally closed ones so a disconnection also trips the fault condition.

If it is a open collector optical encoder, then you will already be using pull-up resistors and still get consistent values when fully disconnected, so detect the case that the motor is running for more than a given timeout with the encoder state not changing. An intermittent disconnection is harder to detect as it may produce valid sequences of pulses ( you don't state whether it's a single or dual pulse encoder; a dual one will have invalid sequences that can be detected), in which case limit switches would again be a reliable answer.

If you don't have a suitable mechanism to use a mechanical limit switch, then optical detectors can be used instead.


Your Answer

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

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