# How to read /debounce an illuminated switch using only two wires?

I'm using the switch below as a replacement boor bell switch. The LED requires 12V AC/DC and the switch has change over contacts. Datasheet.

For aesthetic reasons I have to reuse the existing wiring which is only two cores and ~11m long. So the way I see the switch operating is:-

1. Normal case - LED lit and current (~10mA) flowing through wires.
2. Button pressed - LED off and no current flow.

The above means of operation is possible as you can wire the LED via the switch terminals. Clearly though it's not just a simple case of the traditional pull up resistor on the input pin to a micro controller. I'd like the circuit to be resistant to EMI spikes as well as hardware debounced. I think that I can handle over voltage spikes with a couple of Schottky diodes plus resistor. I looked through a guide on debouncing (Fig.2) and some application specific debounce ICs but they don't seem appropriate for a two core LED style switch. Plus I can't do surface mount. Does this require some form of current detection? An on-board ADC is available. A belt and braces approach will keep my granny happy not having to run to the door every time a car passes by or it thunders.

I think that this might be an original question as others utilise enough cores to separately light up the LED such as:-

Read the state of a 12v illuminated switch from an Arduino Uno

Using a 12V lighted toggle switch with an Arduino

• it would help if you gave us a clue of what this switch is supposed to be driving. – Trevor_G Dec 22 '17 at 1:50
• @Trevor It's just a front door bell button. So it feeds into a micro controller that's ~11m away through an old house. It doesn't drive anything like a bell. – Paul Uszak Dec 22 '17 at 2:07
• Ya but is that something you are putting together or an existing package? – Trevor_G Dec 22 '17 at 2:12
• @Trevor I'm sorry, I don't understand your question. What's an existing package? Do you mean am I programming the micro controller? Yes, I am. It's all from scratch. Except the wiring which I have to reuse unfortunately... – Paul Uszak Dec 22 '17 at 2:18
• Then do the debounce in code. It's a lot easier and more reliable than messing around with filters and things. @Transistors solution is one way to hook it up, I'll post another. – Trevor_G Dec 22 '17 at 2:20

simulate this circuit – Schematic created using CircuitLab

Figure 1. Illuminated switch, opto-isolator and GPIO with internal pull-up enabled. Note separate ground symbols: the remote circuit can be completely isolated from the micro-controller for added noise immunity.

In button released position the opto-isolator will be on and the GPIO will be pulled low.

D2 will be infra-red type and will decrease the voltage to the LED by about 1.4 V and about 1.4 mA or so. It shouldn't be noticeable without a direct comparison.

Debounce can be done in software or by adding a capacitor between GPIO and ground. $C = \frac {\tau}{R}$ where $\tau$ is the desired debounce time and R is the internal pull-up value.

• +1 Might need a little ESD protection in there too. – Trevor_G Dec 22 '17 at 2:11

You should do your debounce in code. It is much easier to do it that way especially for something as slow as a doorbell. You can also filter out a lot of noise in code too.

The following circuit will work though does not have the isolation that Transistor's answer does.

Basically as long as the switch is closed current will flow through the LED back through the base of Q1 which pulls the GPIO low. When the button is pressed Q1 will be turned off by R5 and the GPIO line is pulled high.

R2 and R3 give a little isolation to the long line and the XSORBS give you some ESD protection.

simulate this circuit – Schematic created using CircuitLab

Button monitoring is best done using a timer interrupt with two states, waiting for press and waiting for release. The following flowchart shows a typical de-bounce and noise ignoring method.

• Do you see the soft approach more reliable than hardware debounce (even like the MAX6816)? You say simpler, but code still requires effort to research, architect, write, test and debug. It's a soldering iron /keyboard trade off. I realise that this isn't for NASA, but I'd like to impress gran. – Paul Uszak Dec 22 '17 at 4:31
• @PaulUszak if the switch is not driving as a clock edge there is no benefit to adding cost and reducing reliability using extra external components, especially with such a slow function. Granted you add in a coding effort to get the code right, but that is a one time effort. For multiple higher speed switches, or in a micro that is already busy controlling a servo or something, it might be a different story though. "Horses for courses" and all that. – Trevor_G Dec 22 '17 at 13:56