Circuit Assistance: Simple microswitch getting advanced

Hi again

I am trying to get exactly ONE event for each press of a microswitch, I remember having this problem before, interrupts only firing 90% of the time, or firing too many times.

I think i might need some debouncing circuit or another parameter in the code.

The code used is:

        private readonly static InterruptPort _GateAtTopSwitch = new InterruptPort(G120.P0_12, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLevelLow);
static int count =0;
void ProgramStarted()
    Debug.Print("Program Started!");
    _GateAtTopSwitch.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);

static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
    Debug.Print("Button Pressed nr " + count.ToString());

The circuit currently used is attached.

With the current setup I get two events most of the time. But also one or zero events at the press…

EDIT: The GHI Button circuit attached for convenience (left on drawing).

Debounce is non-trivial, but Jack gives you the best hope of picking something that suits your specific microswitch and needs. Debouncing Contacts and Switches

try to add a 10nF capacitor connected to the micro switch pins

Thanks Brett, very interesting read, indeed! But too sophisticated to me, the dimensioning and the structure of the circuit is beyond my skills. I was looking for some rule of thumbs, or simple “normally I just put a cap at the bla bla which works 90% of the times…”.

I was also hoping that there was some software based debouncing tricks that might be used.

Ok, sound simple, do you mean between the two pins of the microswitch?

yes :slight_smile:

The following is an image of a pretty much tried and tested configuration for switches.

The values of R1, R2, and C1 can be altered in order to change the desired output.

For a typical button or switch you could probably start with R1 = 47K, R2 = 4K7, and C1 = 100nF.

1 Like

@ Oldevel - I will save this for me next switch board… Thanks buddy :slight_smile:

@ RobvanSchelven - it works with a 1pF ceramic cap between the legs of the switch it seems… will go for this short term - thanks to you as well :slight_smile:

1pF sounds a bit small to me … don’t you have a larger capacitor?

Thanks for checking that. I am not good at capacitor values…trial and error here based on my stock of capacitors.

Not sure about the two caps on the pic, which one is biggest?

EDIT: I assume the “1” means one 1pF … and the other one says “.103” which I assumed was 1/10 of the other…

103 means 10 + 3 zero’s 10.000 pf = 10nF

Great, so thats the one I will try now! :whistle:

@ RobvanSchelven - I have been thinking and looking sheets like this [url][/url] but there is no help to help me understand what it means when there is only a “.1” on the stamp…the leftmost on the pic…?? :think:

Further to my tests, I am seeing strange artifacts, unwanted fires when I start the system. I am considering to buy a module to read a simple switch - annoying!

Did you set you interrupt type to negative edge?

If you need to do this in hardware, have a look at the MAX812 as this will debounce your switch nicely for you and is nice and small. It is designed for micro reset but will work fine for your application. I generates a HIGH pulse when you ground the input.

The only downside is that you will not see the negative edge if you need to see this. This is purely to clean up a switch to see the active state.

Sounds like a meaningful approach. I have searched, but cannot find alternatives that is would suited for generic debounce on both edges. Anyone?

Check out the MAX6816 as this does both edges.

Hmm, based on new input from Dave (thanks again!), I have come through this [url][/url] which lead me to find this: [url][/url]

I already have the pull-up circuit with a cap, and applying a schmitt trigger seems to be able to assist me finding some nicer edges.

(And btw the schmitt chip with 8 ports cost 1/10 of the switch debouncer from Maxim).

@ njbuch, RobvanSchelven asked a question that I did not see an answer for. He asked "Did you set you interrupt type to negative edge? "

In all my years detecting switch inputs I have never had to do more than use a 4.7k pullup resistor and 0.1uf cap.
I would also say anything less that 0.1uf would not be of much help debouncing a switch.

There are some tricks that you can use in software to also help with the debounce a IRQ input.
After the first negative edge is detected and you jump to you IRQ routine, the first thing you would do is disable that IRQ. That will prevent any ringing on that switch line from triggering any consequence IRQ’s. Do what you need to do in the IRQ and just before you leave the IRQ clear the IRQ register and turn it back on. This will give it a bit of a software debounce.

If what you are doing in the IRQ is not that long, then dont enable it before you exit the IRQ. But rather whatever event is to happen from that IRQ event being fired when the code jumps there you can then first clear the IRQ, and re-enable it. that will give it even more time to get around any debounce issues.