Push button detection through a very long wire

joeyd999

Joined Jun 6, 2011
5,283
1. DO NOT use a transistor. You're just amplifying noise. There is no need for it, no use for it; it's nothing but trouble. You have a very strong signal already; you don't need any amplification...
I am not going to get into a debate with you....you have your madness, I have mine. But please don't misrepresent the operation of my designs, and I'll refrain from doing the same for yours.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
1. I don't like unnecessary components -- especially when failure of one of those unnecessary components will cause malfunction.
2. I don't like soft edges -- at least confirm the digital input is schmitt trigger -- otherwise, it may "bounce" regardless.
3. I don't like asymmetrical edges.
4. At least put the cap to ground. The cap to Vdd will inject AC transients (on the Vdd bus) into the input during the switch 'on' period.

What, exactly, is wrong with code? In all seriousness, that is the right way.
  • Nothing wrong with code... except that it would be hard (though not impossible) to implement in my application, since several other things will be monitored at the same time and I have to keep track of what is happening at all times in the MCU cycle by cycle.
  • A schmitt trigger is something I hadn't thought of... I doubt my MCU has that sort of input, but I'm glad you mentioned it, I'll look into that.
  • Avoiding unnecessary components is part of the much respected KISS principle... I like that too.
What I'm going to do is prepare the PCB with the cap to ground as you suggest, and install it only if I find it too hard to code the debounce routine.

Thanks again, Joey. Your help is thoroughly appreciated.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
Why not make it an active high and switch pulls it low?
I've done that before... and I get many false detections when I use a 5V signal.... that's why I switched to 12V
Though configuring active high with 12V does not sound like a bad idea. Ideally there would always be current at the wire, and noise would be minimized.
But Joey's setup is something I haven't tried before, and I have the highest respect for his experience.
 

dannyf

Joined Sep 13, 2015
2,197
You may want to add some resistance at the switch end. I have been told that discharging the capacitance of a long cable can burn the contacts on the switch.
capacitance isn't as much an issue as inductance: I have seen inductance from long-wires resetting mcus.

So, yes, adding some resistance on the switch side or the mcu pin side is a good idea.
 
I am not going to get into a debate with you....you have your madness, I have mine. But please don't misrepresent the operation of my designs, and I'll refrain from doing the same for yours.
I apologize for stepping on your toes. Would you explain the operation and the benefit? I see it providing a voltage-based threshold, much the same as what the input logic on the CPU would do, only it would do it with MOSFETs. Didn't seem much different to me, but please explain.
 

joeyd999

Joined Jun 6, 2011
5,283
I apologize for stepping on your toes. Would you explain the operation and the benefit? I see it providing a voltage-based threshold, much the same as what the input logic on the CPU would do, only it would do it with MOSFETs. Didn't seem much different to me, but please explain.
Much better. I'd be happy to provide an explanation.

The primary motivation for the use of this circuit is this: MOS inputs should not be exposed to the world -- they are just simply far too fragile. Static discharge punctures the gates, and over-/under-voltage transients cause latch-up (the current threshold for latch-up has been greatly improved, but the possibility still exists). Bipolar junctions are far more rugged in this respect.

Secondarily, long wire runs are prone to inductively and capacitively coupled noise sources -- sources that can easily overcome the high impedance of lightly biased MOS inputs ("weak pullup"). These can cause false signaling.

Third, as a chassis is normally electrically connected to the circuit common (ground) of the power supply. Running low impedance connections at any voltage other than common is asking for future failure (i.e. +5 or +12 out to the switch). A short circuit fault to chassis or earth ground can disable or destroy the system.

The PN junction of the 2N2222 isolates the MOS gate from the world. All external voltage/current sources see nothing more than a forward biased diode (switch open) or short circuit to ground (switch closed). There is no path from world to MOS gate. This protects the CPU in the primary case.

In the case of coupled noise, the switch-closed case is completely immune (hard short to ground). During switch-open the PN junction looks like low AC impedance and shunts most noise to ground. The worst case would be induction from a long wire run through a varying magnetic field. This can be easily brought within acceptable limits through the use of twisted pair.

Finally, a short-circuit fault, worst case, will cause a false signal. In the schematic shown, both the switch voltage and switch current are limited to very low safe values.

The diode protects against Vbe reverse breakdown.
 

AnalogKid

Joined Aug 1, 2013
11,044
A couple of observations.

There are a lot of examples of how to debounce and otherwise clean up a switch, but the vast majority fall into two categories, feedback and energy storage. Hysteresis and a set/reset latch are two examples of feedback. A symmetrical lowpass or asymmetrical (boxcar) filter are two examples of energy storage.

At its heart Joey's circuit is a linear amplifier. It spends most of its time either saturated or cut off, but it is a gain-of-100 amplifier with no hysteresis and no energy storage. As such, if has the possibility of reducing the effective noise margin of the input it is protecting, because a much smaller input voltage swing will produce an output with valid logic levels. If presented with a slowly changing ramp input, it will produce the noise burst that hysteresis was invented to prevent. If presented with a square wave burst varying randomly between 100 Hz and 4 kHz (switch bounce) or even MHz frequencies, it will pass them through with nice clean edges. It will suppress low amplitude interference on an open line as long as it is below about 0.3 V, a relatively low noise margin, but that is a subset of the range of things that can come down the line.

Joey and I agree about most things, and I completely agree with much of what he says about protecting MOS inputs. In fact I often use something with even more gain in pretty much the same way, although I do add the debounce capacitor on the output - a ULN2003/200x. It has the reverse polarity protection diode at the base, a true resistance to GND so the line never floats, a 50 V rating, transient protection on the output, three optional input attenuators for level shifting - and you get 7 in a box for a quarter. I did a 50 kHz multiplexed data/address bus in a really dirty environment using 2003's as drivers and receivers. Bulletproof.

ak
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
@AnalogKid and @joeyd999, thank you both for your detailed and extensive answers to my seemingly simple question. I now understand far more about this seemingly mundane problem. In fact, I can't recall a previous moment in which I learned so much about a related subject in such a short time. Thanks to you I now feel like a caveman who's been explained the basics on how to properly start a fire Emoji Smiley-91.png Emoji Smiley-54.png .

The fragility of mosfets isn't something new to me, and I've experienced the pains of their not being used correctly firsthand. But I didn't know about the 2N2222 PN junction's ability to isolate its MOS gate, and AnalogKid's explanation of the circuit's hysteresis capability at slow ramp up while also allowing for a clean signal at high frequencies has clarified things further.

A couple of questions though:

How do you connect the the debounce capacitor on the output of the ULN2003? is there a resistor involved? and what should its value be for an ordinary switch?

Also
I did a 50 kHz multiplexed data/address bus in a really dirty environment using 2003's as drivers and receivers.
How long was the bus' length in your circuit?
 

blocco a spirale

Joined Jun 18, 2008
1,546
The input will be less susceptible to false triggering from external noise if you use a normally-closed switch, as the line will have a much lower impedance when the button is not pressed. You will also be able to detect open-circuit line and switch faults.
 
Last edited:

joeyd999

Joined Jun 6, 2011
5,283
...If presented with a slowly changing ramp input, it will produce the noise burst that hysteresis was invented to prevent. If presented with a square wave burst varying randomly between 100 Hz and 4 kHz (switch bounce) or even MHz frequencies, it will pass them through with nice clean edges. It will suppress low amplitude interference on an open line as long as it is below about 0.3 V, a relatively low noise margin, but that is a subset of the range of things that can come down the line.

...I did a 50 kHz multiplexed data/address bus in a really dirty environment using 2003's as drivers and receivers...
Good points, @AnalogKid, except that I made clear that the circuit is for implementing a contact-closure input -- not a high speed data interface. I also made it clear that it did not provide any debouncing whatsoever -- I do that in software.

Your analysis about 'low amplitude interference' is not applicable in the contact-closure case, IMHO (there is no closed loop switch-open, and there is low-impedance ground everywhere for switch-closed). If I am wrong about this, please illustrate a realistic case where such interference will be problematic.

Aside from that, I've no issue using either an NPN or darlington (save that a darlington won't pull as low as NPN -- important for low-voltage digital inputs -- check the input low voltage range!).

Just FYI, I've used this circuit as a contact-closure front end for many years on many different designs in high-interference installations with long cable lengths. Zero failures or false activation.

Also, important: These circuits provide no isolation at all! If actual isolation is required, other methods must be used.
 

joeyd999

Joined Jun 6, 2011
5,283
I take it back. I looked at the ULN200x specs. With a max Vce,sat of about a volt or greater, I do have an issue using it over a standard NPN.
 

AnalogKid

Joined Aug 1, 2013
11,044


There is nothing special about a 2222. In fact it can be any small signal transistor, including a MOSFET like the 7002, although that gets into the issue of protecting *its* gate. Anything in front of an input protects that input. And there is no isolation in the sense of galvanic isolation as with an optocoupler. It is just an input buffer, inverting in this case, with a more robust part, designed to absorb or reduce the effects of some kinds of undesirable input conditions. Also, the points I was making were a) that this circuit has *no* hysteresis to clean up a slow ramp; and b) its ability to amplify and pass through very high speed signals is a disadvantage. Note that both of those value judgements are in the context of debouncing a mechanical switch input, not processing some other kind of input signal.

Using a section of a 2003 instead of an individual transistor doesn't change the overall circuit function. There still is a pullup resistor from the collector to Vcc. Connecting a capacitor from the collector to either power supply rail provides the asymmetrical filtering commonly used to debounce a switch. Where the capacitor is connected determines the power-on condition of the circuit. With the switch normally open, the transistor is off at power on, so the R-C determines the signal at the uC input. If the capacitor is connected to GND, the input starts out low and goes high after approx 0.7RC, then sits there waiting for a switch press. If the capacitor is connected to Vcc, the input starts out high and stays there until a switch is pressed. Which one to use depends on the overall application.

I like the ULN200x as an industrial signal interface. One chip and a SIP resistor network gets you what are essentially seven inverter gates rated for 50 V and 1/2 A. Each has a transition region several volts above GND, can be wire-ORed for some basic combinational logic, and you can circle two gates to get hysteresis.

The little bus system was only about 15 feet long, connecting 16 modules to a controller using 10-wire ribbon cable. The system was deployed in military aircraft maintenance hangars with very poor power quality and some serious EMI. It was a way to simplify greatly the wiring complexity while having the modules appear to be hardwired directly to the system controller. No microcontrollers; everything was done in a small CPLD. The 2003's set the bus bandwidth limit at 50 kHz. I tested the system without them at 50 MHz on the bench, and 1 MHz with the ribbon cable.

ak
 

MaxHeadRoom

Joined Jul 18, 2013
28,688
I moderate a large forum that users implement a lot of 5v logic outboard of a PC and many experience false triggering of limit switches etc, the attempts of fitting debounce capacitors have often failed, most have been cured by the current practice of earth grounding both ends of a shielded cable, there are a few published papers on this.
One very good one in this are is the one by Siemens, see Ch6.
Max.
 

Attachments

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
I moderate a large forum that users implement a lot of 5v logic outboard of a PC and many experience false triggering of limit switches etc, the attempts of fitting debounce capacitors have often failed, most have been cured by the current practice of earth grounding both ends of a shielded cable, there are a few published papers on this.
One very good one in this are is the one by Siemens, see Ch6.
Max.
Thanks for the doc, Max. I had seen it before, but had forgotten about it. I'm going to re-read it now, try to refresh my memory.
 
Top