0
\$\begingroup\$

I want to read the state of a switch sensor (magnetic contact). I'm thinking to connect the switch between an interrupt pin of my micro and ground, listening for CHANGE status. In this way I have debounce problems, so I wonder what's the best way to deal with this problem:

1 - Disable the interrupt listening when it fires and enable it after the program has read the value (0 ->ground->switch closed OR 1->floating->switch opened) I don't think a floating state could be good, but if I set the interrupt input pin as INPUT_PULLUP it's no more floating and could be good, am I right?

2 - Connect the switch to an interrupt pin and also to a digital input pin through a transistor as a switch. In this way, when the interrupt is triggered I read the value of the digital input pin. On the other hand, in this way I complicate the circuit

3 - To use an hardware debounce circuit

Are these three solutions valid? Are there any more better than these?

MORE CONTEXT: I'm using these switches as limit switch for a garage door. In my code I'm using a timer to raise the garage door for ten seconds OR until the limit switch interrupt is triggered

EDIT: with a change in my code I solved the problem using interrupts. However you suggest to use polling since it's not a real-time application, so I wonder: isn't better the same to use interrupts so that I can use a power safe mode for my micro and wake-up it only when an hardware interrupt is triggered? If i use a polling solution the system would not be suitable for battery power supply; isn't it?

\$\endgroup\$
1
  • \$\begingroup\$ It's usually more headache than it is worth to use pin-change interrupts for debouncing. However, circumstances matter, so I cannot make a blanket statement about it. I will usually prefer to set up a "heartbeat" interval timer used to poll and debounce the input, posting out current-state and debounced values for the switch, which can then be examined by application code as necessary. There are times when hardware debouncing can achieve things that no amount of software can achieve, too. So again, circumstances and details matter. Provide more context here? \$\endgroup\$
    – jonk
    Commented Nov 6, 2018 at 20:31

3 Answers 3

3
\$\begingroup\$

The real answer is that you shouldn't use interrupts for this at all. You should connect the switch to an input pin and poll the pin periodically.The period of the polling should be greater than the time interval during which the switching transient (bouncing) subsides.

Also, you need to pull up your pin with a pull-up resistor. Simply connecting the switch in-between GND and the pin is not good enough; Some microcontrollers have internal pull-up resistors which you can activate from software, so you don't need to use an external one.

\$\endgroup\$
6
  • \$\begingroup\$ Now that you have given more context, I don't even understand why you are worried about the bouncing in the first place. If you turn of the motor 5 times in 10ms, than what? It gets turned off either way, and that's the point. Just poll the pin with 100ms and you will be fine; Put a 100ms sleep in a while cycle which cycles while the pin is HI and your timer hasn't overflown. Or if you are using an OS than use the scheduler; Create a periodic timer with a period of 100ms and schedule a task to run after 10s which sets a bool. Check the pin and the variable in the periodic timer. \$\endgroup\$
    – Cerike
    Commented Nov 6, 2018 at 22:12
  • 1
    \$\begingroup\$ You are commenting on your own answer. Hit the edit link under the question and add it in as an update instead. \$\endgroup\$
    – Transistor
    Commented Nov 6, 2018 at 22:21
  • \$\begingroup\$ You say " It gets turned off either way, and that's the point.", In theory when the magnetic contact is closed I should read LOW and vice versa; due to debouncing I noticed that some times the logic is inverted an the last value that I read continue to be LOW even if the switch is open again \$\endgroup\$
    – Nicola
    Commented Nov 6, 2018 at 23:13
  • \$\begingroup\$ Your design is active LO, right? The switch becomes a short when the door should stop, right? Have you put in the pull up resistor yet, because it would cause the exact problem what you just described if you haven't. \$\endgroup\$
    – Cerike
    Commented Nov 6, 2018 at 23:23
  • \$\begingroup\$ Yes, the input interrupt pin is declared as INPUT_PULLUP \$\endgroup\$
    – Nicola
    Commented Nov 7, 2018 at 13:18
0
\$\begingroup\$

I'm using these switches as limit switch for a garage door. In my code I'm using a timer to raise the garage door for ten seconds OR until the limit switch interrupt is triggered.

This is a low-speed application and an interrupt is not required to give adequate stop. The ramp down time of the motor will be hundreds of times longer than a simple input polling and debounce circuit could handle comfortably.

Set the limit switches back a little from the end of travel and adjust them so that the door stops at the correct open and closed positions.

\$\endgroup\$
0
\$\begingroup\$

I think of these few points

  • Do you have to care about debouncing when you simply wait until the door touch the sensor for the first time?
  • Hardware circuit could be as easy as RC filter. RC Filter helps big time and it is very simple.
  • If your sensor wire is longer than one feet, I'd recommend external pull-up, to make at least 10mA. It is much more robust against EMP caused by storms etc.
  • The interrupt is better for very short events, like miliseconds or less. Raising your garrage door will most certainly take more time. I'd recommend pin reading every, let's say, 20ms. Good old ZX Spectrum does the same. At least it is a good start.

The RC filter with external pull up and software debounce is the best option here. If you read the same value after 20ms you can be sure it is correct

Back to your questions:

  1. I think you will spend lot of time tuning this
  2. As you say, this makes things complicated. You also waste more pins.
  3. RC Filter with Schmidt gate is the HiFi option, but unnesecesary in this case.

Anyway, you gain experience by failing. That is, after all, what counts.

\$\endgroup\$

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.