How to detect simple switch


Another newbie question. I have a mechanical switch that is closed by a cam, and I want to detect when it closes and opens. I have a TB10 Breakout module I am using on a Raptor board.

I have read up on digital inputs, the breakout board and so on, and came up with what I thought was the correct approach, but it is not working.

I connected one of the wires from the switch to the GND terminal, and the other to P3. Here is the code I am using:

            var camSwitch = breakout_TB10.SetupInterruptInput(GT.Socket.Pin.Three, 
            camSwitch.Interrupt += camSwitch_Interrupt;

        void camSwitch_Interrupt(GT.Interfaces.InterruptInput sender, bool value)

The event method camSwitch_Interrupt seems to be firing constantly.
Thanks for the help!

I would say it looks good.
Is it possible that the switch is damaged?
Have you some equipment to measure the voltage between P3 and Gnd?
An oscilloscope would be good, but a simple voltage measurement would also be a good start. If it can do a min/max measurement you could see if the value changes.

@ CodeGreen - Have you tried enabling the glitch filter?

taylorza is right.
If your switch provides no clean high/low signal (which is not very unusual for mechanical switches) you get multiple interrupts when switching.
Because char display is not really fast you might get the impression as if the interrupt is fired constantly.

Hi there CodeGreen

There are really two sections to your problem - hardware and software.

First to hardware. You need to set a known state on your connection before you then detect it going to GND, so this requires you to use a pull-up resistor. You basically need one end of the resistor connected to 3v3, the other end to your input line, which means that you’ll ordinarily read a 1 when the switch isn’t closed. Then, when the switch closes, the least resistance path is now to GND and the value on the input line becomes 0.

Then software. For a task like this, I’d first look to an existing, known working, code for inspiration - so I would look at the existing push button module driver to see how it implements things.

So a couple of things I’d suggest you look at: the interrupt glitch filter is set to ON in the button driver (to help eliminate the noise), and then the validating the state through reading the value back.

Give those things a try and let us know how you go!

Oh and another tip. Add this to your interrupt routine to get more detail:

Debug.Print("Interrupt at “+DateTime.Now.ToString()+” state is "+value.Read());

Edit: And looks like I shouldn’t step away from the keyboard for too long :slight_smile: Glitch filter will have a big difference, you use pullups from the processor so you should be fine without a hardware pullup, and use debug.print to get more feedback. That way you can really see whether it changes state when the switch is activated

@ Brett - When you set GT.Interfaces.ResistorMode.PullUp (as CodeGreen did), I thought a hardware pull up is not necessary anymore!?


Turning on the glitch filter did help, am now using Debug.Print instead of charDisplay, and also instead of reading ‘value’ argument coming in, I changed it to read sender.Read(), which I now have the following result.

When the switch is open, I get one True, this is expected.
When I close the switch, I get intermittent ‘False’ hits, but all False (reading sender), as long as the switch is closed, (however the ‘value’ argument coming in is sometimes True, not sure what that argument is for).

I can work with this in software, watching for the transitions from True-False and back. Is there any harm in this?

I don’t have any resistors, if I were to try adding that, what resistor would I get? And to clarify, the resistor would go from the 3v3 terminal (pin 1 it looks like), to the P3 terminal, and also in the P3 terminal I would put the wire going to the switch. The other wire from the switch still goes directly to gnd.

The read value of the switch is always inverse, as you have figured out I guess.

The value parameter is the value of the input at the moment it has changed.
When you read the value from sender again. it might have changed again.
The event is managed and therefore a bit delayed.
This is why you get different values.

This does mean in fact that the glitch problem is not fully solved.

How fast do you need to react on an incoming switch change?

If not too fast, then you could check if the value is stable for a while in software.

Using the Read value seems to be working, it is always consistent to the state of the switch, then I just ignore any events after that until the value of Read changes. Most times there are only a few extra events now that glitch is enabled. I’m a programmer by trade so the software stuff is easy, it’s the wiring and resistors and such that I have to learn about.


Also, to help us learn the hardware side of things, if I did want to try a ‘Hardware Resistor Pullup’, what type of resistor would I get?

And to wire it up, the resistor would go from the 3v3 terminal (pin 1 it looks like), to the P3 terminal, and also in the P3 terminal I would put the wire going to the switch. The other wire from the switch still goes directly to gnd. And in the code would I change the ResistorMode to Disabled?

yep, that’s all correct in what you’d do.

As to what type of resistor, I’d just get some standard 1/4 or 1/2 watt or whatever is available through-hole (ie with leads) resistor from your local electronics supplier, in a value of say 2.2k ohms. I have a bunch of resistors from a pack like this that I regularly use on a breadboard, before I turn the circuit into a more permanent design and create a PCB for it.