optically isolated and debounced 24V switch input

Discussion in 'Digital Circuit Design' started by areslagae, Jun 10, 2017.

  1. areslagae

    Thread Starter New Member

    Jun 10, 2017
    Hi all,

    First post. I am a computer engineer getting into electronics from the software side (Raspberry, Arduino, ...). I only have a basic electronics knowledge. I have had some courses a long time ago but that's it. Please be gentle.

    I am building a optically isolated and debounced 24V swich input for a microcontroller for a home automation application.

    I came up with the circuit below by simply replacing the switch in JG Ganssle's (?) debouncer with an optocoupler. The circuit seems to work. I did experience severe problems initially (Schmitt trigger settling at intermediate voltages) but these seem to have disappeared by grounding all unused inputs and adding a decoupling capacitor (I did not expect that, the 6 triggers seemed independant).

    I am attaching a couple of screenshots that I took with a usb scope. The switch I am using bounces significantly when closing but does not bounce when opening. Red is Schmitt trigger output (OUT in schematic below), blue is switch input (IN in schematic below) or capacitor voltage (top of C1 in schematic below). Switch input is scaled by 10x.

    I have the following questions about this circuit:

    1/ How do I optimally pick values for R1, R2 and C1 in this circuit? The circuit seems to work equally well with e.g. R1 and R2 = 100K and C1 = 47n. This makes sense since the charging / discharging time constants are the same. However, there must be other boundary conditions, like current through the optocoupler (which seems quite small in any case so I do not expect CTR issues) or voltage at the Schmitt trigger input?

    2/ I have added the optional diode because I like symmetry. In this case both time constants are the same. Some sources (e.g. http://www.electronics-tutorials.ws/io/input-interfacing-circuits.html) argue that R2 should be smaller than R1 so that C1 discharges more quickly. However, I do not see why this would be the case. Given that the bounces seem unpredictible it seems to make more sense to handle opening and closing symetrically. Does that make sense or am I missing something?

    3/ Does it matter which 74X14 Schmitt trigger I use (HC, GCT, AHC, AHCT, ...)? I have chosen the HC version because I am hoping to be able to make a 3.3 V version by only changing component values of R's and C's, and HC seems to work for both 3.3 and 5 V. Is that correct? Or am I again missing something?

    4/ Do I need more protection at the optocoupler side for my application? Some sources use a TVS diode. Some sources split R3 in two resistors at both leads. But I am not sure what I (reasonably) need in my application?

    5/ It would probably be possible to move the debouncing components to the 24 V side of the optocoupler. Would that be a better or worse design? Pro's and con's? I did not go that route because it seemed more tricky to get everything right since the same current charges the capacitor and lights the LED. In the circuit below (I think) this is independant.

    Other comments are of course also welcomed!


    switch input (x10) and Schmitt trigger output when closing the switch

    switch input (x10) and Schmitt trigger output when opening the switch

    voltage at capacitor and Schmitt trigger output when closing the switch

    voltage at capacitor and Schmitt trigger output when opening the switch

    switch input (x10) and Schmitt trigger output when "double clicking" the switch

    capacitor voltage and Schmitt trigger output when "double clicking" the switch
  2. AnalogKid

    AAC Fanatic!

    Aug 1, 2013
    Unused logic gate inputs - *especially CMOS* - must be connected to a low impedance point, usually either Vdd or GND. Left floating, they are antennae with a gigaohm input impedance. A non-Schmitt input can detect when a person walks by from 10 feet away, literally sensing a disturbance in the force.

    1. Since the opto output transistor can handle the short circuit current of C1, you can eliminate R2 and D1. R1 x C1 is the R-C time constant, 4.7 ms for the values shown. This is a bit short for a bouncy switch. You want the time constant to be longer than the longest bounce pulse width. The math is simple because a CMOS Schmitt input triggers at approx 33% and 67% of Vdd, and 67% is very close to the 63% voltage rise in one time constant. The tradeoff is that a larger time constant increases the margin against false triggers, but increases the time delay between when the input turns off and the inverter output changes.

    2. Eliminating R2 makes the inverter output transition more quickly on the leading edge of the input. Also, the faster and more deeply C1 is discharged by the first opto transition, the better it ignores the following bounces.

    3. Yes, they matter; no, you're not missing something. Each logic family has unique characteristics that make them viable in a competitive marketplace. More or less propagation delay, greater output currents, lower voltage operation; you pick the family based on your circuit requirements. If you don't care about nanosecond speeds, but need 3.3 V operation, HC or LVC work well. For example, I like AC and ACT because of the big, fat, symmetrical +/-20 mA output currents.

    4. Don't know enough about your application for deep specifics, but a 1N914 reversed across the opto LED is cheap insurance.

    5. Agree, keep the debounce after the opto.

    #12 likes this.
  3. Dyslexicbloke

    Well-Known Member

    Sep 4, 2010
    Sorry I have to ask.... Why do it with hardware at all?
    Wouldn't it be more flexible to look for a sustained state under software control ? Even if you used interupts it would hardly be complex.

    Dont get me wrong I apriciate that debouncing is required where fast switching applications need a consistent signal, speed sensing or fast timing for example, but if this is a manual switch why bother, just check its been in a particular state for some mS, without changing, and then act on the new state.

    For that matter you could impliment a shmit trigger in software on an analouge input if you really need one. One cap to ground from the input pin and that node via a resistor to your opto.

    I do apriciate the learning opportunity, which is entirly valid in its own right, but I dont understand why you would want to complicate your build with no obvious advantage.

    Protection is a good plan though and a TVS is probably a better plan than a zener given that they are generally designed to handle larger pulses.

    Have you considdered pulling up the opto input and connecting the switch to ground / earth?.
    there are several advantages...
    You dont need a dedicated 24V rail anywhere as ground is usually available in a domestic installation.
    Your 24V rail is not extended and thus vaunerable to whatever worries you about the switch lines.
    For a small cost, in current consumption, you could pull up the opto and use a normally closed contact which would result in the same logic, opto on when button pushed, but would have the switch line grounded, and thus safe, whenever it wasn't being pressed.

    You could even put a resistor acros the switch an monitor the current when it opened which would allow you to detect both open and short conditions distinctly from switch activations, if it actually matters of course.
    (Given that the switch is a button a fault, pushed for an unrealistic time, suggesting a fault, could be handled in software.

    Please dont think I am suggesting that you are in any way wrong, I am only trying to offer some sugestions you may not have considdered.

    Personally, in a domestic environment I wouldn't even bother with the opto isolation. lets face it if you accidentally connect it to the 230V supply yiou will take everything out anyway unless you are also designing custom boards with gaps.
    I would buffer inputs, and outputs, but probably use a darlington drier IC, ULN2003A for example for both with my inputs pulled up to some rail that wasn't the same supply as the uP and normally closed low switching as described above.
    Pull up the uP inputs, internally or otherwise and you will see a high when the switch is opened which is easy and makes logical sense.

    Hope that is of some use,
  4. areslagae

    Thread Starter New Member

    Jun 10, 2017
    Thanks for your input to both! I have some follow-up questions and comments for both of you. Of course contributions of others are also appreciated.


    1/ Given a specific time constant, how do I choose between on the one hand a larger resistor and smaller capactor (e.g. 100K and 47n) or a smaller resistor and a larger capacitor (e.g. 10K and 470n) that both result in the same time constant? Ganssle seemed to prefer a 1u capacitor over a 100n capacitor for the same time constant in his circuit but it was not clear to me why. I think it had to do with current flowing into the Scmitt trigger.


    Many thank for your suggestions. I definitely value your comments.

    - about software/hardware debouncing: I did and still do consider software debouncing. However, apart from manual switches I will also connect various other sensors, and I am planning to connect inputs through MCP23017/MCP23S17 IO expanders (up to 128 IO's) and if possible make use of their interrupt functionality. Since I have not tested all of the sensors and the interrupt handling of the IO expanders (software debouncing is not available since they are not programmable and I am not entirely sure how the interrupt handling will be affected by switch bounce) I wanted to have a circuit ready in case I needed it. I also wanted to get a better understanding of the debouncing problem even when I eventually end up using software debouncing. But your point is valid and noted.

    - about alternative configurations: I have not considered a normally closed configuration where the opto is pulled up and the switch is connected to ground. This is definitely interesting. I should have 24 V available everywhere though (where I live all switches are typically cabled to the central cabinet in a star configuration, and in the central cabinet an isolated 24 V power supply is available).

    - about protection: I have experimented with the ULN2003A and related IC's. In my outputs which drive relais that switch 230V I have replaced the ULN2003A driver with separate PNP transistors because I wanted to use high-side switching (to reduce cables in my case). But in essence this is the same I guess. I am not sure however I get what you mean with using the ULN2003A as an *input*. Can you elaborate? Do you mean that you would basically use a transistor in the input to switch the uP inputs? I am curious about what you would use as an input circuit in this settign. I have read threads where people simply use 2 external clamping diodes and two resistors to connect 24V inputs to an uP in an "industrial setting" with "great success", while others simply use a resistor divider, and in the Arduino / Raspberry Pi community everyone seems to recommend an optocoupler.
  5. AnalogKid

    AAC Fanatic!

    Aug 1, 2013
    Several factors come into play. The larger resistor option is a higher impedance network; too high, and the input might start false triggering on noise. Yes, the Schmitt input is designed specifically to prevent this, but nothing's perfect. The smaller resistor option is less susceptible to noise, but increases the switching current through the opto, which increases the current requirement through the input side LED. Other factors are what is in your inventory, and what capacitors already are used elsewhere in the system. There is no one perfect combination.
    Nope. Again, multi-megohm input impedance.

  6. Dyslexicbloke

    Well-Known Member

    Sep 4, 2010
    Buffering inputs with open collector transistor arrays is an easy way to level shift and provide 'some' protection.
    You could use anything that is fast enough for your application but lets assume you are going with ULN2003.. or simmilar.

    Your external,circuit is 24V, which the buffer will accept on an input without damage but you only need about 2.5V to turn it on so a simple divider can be used at the input, better still a zener to ground an a current limit resistor. Ion is inly about 1.3mA at 3.8V but they will turn on below this in my experience.

    Connect the open collector output to the uP and either pull it up externally or enable internal pull-ups, the latter being easiest.
    That gives you negative logic 24V in is a 0 at the uP input.
    Fine but why stop there...
    If you connect your buffer input to the 24V rail using the current limiting resistor as a pull-up and leave the clamp diode, or resistor, in place you can then connect your external input line, via a diode, and potentally another current limiting resistor, to the remote switch which is normally open to ground.

    Switch open - Buffer ON - uP input OFF
    Switch closed - Buffer OFF - uP input ON

    50V on the switch line... No problem as the diode handles it protecting the buffer.
    Shorted switch line ... No problem as its current is inherently limited.

    As a solution it isnt perfect, few are, but it ticks most boxes in an environment that isnt extreme.
    if you want additional protection on the uP input you can pull the same diode and current limit trick and still add a 5V clamp to cope with the buffer failing at the same time as the diode if you are really worried.
    As long as you pull the uP input below 2V you will see a 0 and the diode will prevent it from seeing anything from outside.

    Not withstanding connecting it to the grid by mistake you would have to work pretty hard to break an input protected in this way.
    As I said, why buy opto's without a very good reason.

    Internal pull-up > switching diode > buffer (Collector) > voltage divider (Buffer base) > switching diode > switch > ground.
    I would not use the 5V rail to pull up the external circuit but all grounds need to be common.

    You can even add some degree of de-bounce by adding a capacitor to the buffer input...
    It will charge via the divider when the switch opens so turn on is delayed untill the switch is open for some time and discharge through the current limit so turn off is delayed. Yiou would need to play with the values a little but it works just fine once you are dialed in.

    Hope that helps,
  7. Sensacell


    Jun 19, 2012
    With regard to choosing resistor and capacitor values:

    While opto-isolators are "isolated" - there is still some capacitance between the input and output.
    Choose the pull up resistor based on the CTR of the isolator to keep the impedance low as possible, this means use the lowest resistor value and biggest capacitor that meets requirements for: (a) time constant (b) minimum CTR / reasonable LED current rating.

    This approach minimizes the effects of isolator capacitance.

    I have been bitten by this capacitance in the past, naively assuming total isolation.
  8. andrewmm

    Active Member

    Feb 25, 2011
    For reference,
    One would normally use a pull up resistor on the opto input,
    and the switch would then pull to ground,.

    Regarding the denounce,

    As mentioned above, its normal now days to denounce in the software,

    denounce in the hardware, one can do on the switch side if the switch is wired to ground,
    Switch then grounds out a capacitor via a resistor, gives RC time constant,

    but be careful, as its now a slow edge, and it takes a 'long time' to cross the logic threshold, there is plenty of time for metastability,
    which then has to be cured in software,

    which code is similar to the denounce anyway,, so you might as well denounce in software.
  9. AnalogKid

    AAC Fanatic!

    Aug 1, 2013
    Actually, grounding one side of the input and switching the "plus side" is very common. I see it often in military systems.
    Only if there is something in the system that runs software. Small systems that can't justify the development cost or reliability/upgrade issues still are all hardware.
    Again, the capacitor does not care which way it is charged. Switching to the positive works exactly the same, with the same component values.
    Note that IC1A has a Schmitt trigger input stage, designed specifically for this type of application. Also, the problem with slow input transition times in conventional gates is a noise burst at the output, not metastability.

  10. areslagae

    Thread Starter New Member

    Jun 10, 2017
    @Dyslexicbloke: Is the circuit below what you mean with "Me ..."? I think there is something wrong at the input of T1.

    @Sensacell: That is usefull, thanks, it allows me to fix all values in the opto circuit.

    - Is there any specific reason why several of you would avoid the opto? Is this just about component cost? Or because it is "overkill"?
    - Is there any reason to switch the ground line rather than the supply line? I have always learned/assumed it is better to switch the "hot" line.
    - How will a microcontroller react to a slowly rising edge as it goes through the "logically undefined" zone? Will this result in a single transition or will the microcontroller itself introduce bounce?

  11. AnalogKid

    AAC Fanatic!

    Aug 1, 2013
    If the input system and output system have a common ground potential, then galvanic isolation is defeated by that ground connection. While there are some applications where the fully isolated output transistor can be a design benefit, usually there are lower cost and/or less complex ways to do the same thing. there is nothing explicitly wrong with throwing in an optoisolator, but most designers do not unless it solves a specific condition.

    Switching the high or low side is a design decision. Often it is dictated by other system requirements or features that are not changeable.

    Yes, a uC input can have the same issues as a logic gate, although some uCs have Schmitt inputs. Debouncing in software is relatively simple. When the system detects an input change (either by interrupt or polling), poll the same input periodically until it remains unchanged for a designated period of time.

  12. areslagae

    Thread Starter New Member

    Jun 10, 2017
    Thanks all, I have learned a lot from this thread so far!

    @Dyslexicbloke: Could you please comment on the circuit I posted. Is this what you intended? I have a basic understanding of transistors as switches but I do not know what happens when the base voltage is higher than the collector voltage ...

    @AnalogKid: Agreed about a common ground potential defeating the galvanic isolation. I was planning to use separate ground (note use of GND1 and GND2 in my initial schematic). In our local residential setups there is typically a 24V isolated power supply in the cabinet which is connected to all switches, sensors (e.g. movement detector, light detectorm ...) and actuators (e.g. relais, doorbellm sirene, ...) in the house. I was planning on adding an aditionnal 5V isolated power supply for the microcontroller and then connect switches, sensors and actuators through optically isolated circuits. I was ispired by PLC input cards, which typically seem to feature both optical isolation and a some kind of RC filter (but they do operate in harsher environments). I am not really worried about incorrectly wiring mains to the input modules (I am planning to use separate cabinets for 24/5V and mains) but rather about contamination of the 24V line (which runs near mains lines across the house and even outdoors), transients, glitches, ... I figured the optocoupler was the best solution but this thread has make me doubt that. I also assumed that the optocoupler would block transients, which I am not entirely sure about. However, I am still interested in the circuit that Dyslexicbloke is proposing. I'd like to compare both circuits on some criteria (protection, PCB space, ...) Since this is not a commercial project, the cost the optocoupler (€0,193) is not really an issue.

    Again, I am learning a lot from this thread and I am thankful for your comments.