logo elektroda
logo elektroda
X
logo elektroda

Problem with ESP32 interrupts originating from the PC817C optocoupler

endriuh28 2859 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 19389968
    endriuh28
    Level 9  
    Posts: 98
    Rate: 13
    Hello, as in the subject, I have a problem with the detection of an interrupt from a PC817C optocoupler.
    The voltage is 3.3V and I have the emitter of the optocoupler (leg 3) connected to the ESP32 with the GPIO35 pin,
    with an external pulldown in the form of a 10K resistor Ω connected to GND.
    I have set this pin as INPUT and the interrupt mode as FALLING.
    The ESP32 detects the interrupt but it is not synchronous, and mostly detects double.

    My ESP32 is a DevKitC ESP-WROOM-32U.

    No problems with the Arduino UNO, it detects correctly,
    But the ESP32 seems to have faster sampling at a higher frequency than the Arduino UNO.

    I think the ESP32 pin voltage of 3.3V is not a problem, as the Arduino UNO has a pin voltage of 5V.

    I tested on my little oscilloscope and at 2V and 10ms the signal is rectangular and varies from 0.16V to 3.2V,
    and when I decrease the time the signal looks more and more like a sine wave,
    when I add a larger resistor the signal sharpens but the voltage floor rises significantly,
    and when I decrease the resistance the signal rounds off and looks more like a sine wave.

    In the function called by the interrupt I have added a check to see if there is a low state on the pin:
    Code: C / C++
    Log in, to see the code
    .
    This caused me to no longer detect interrupts doubly, but they are still not synchronous.

    I ad hoc solved the problem softly with code like this:
    Code: C / C++
    Log in, to see the code
    .

    Is it possible to somehow improve the signal so that at higher sampling frequencies i.e. less time it is more rectangular, and so that there are no problems detecting the interrupt during the descent?
  • ADVERTISEMENT
  • #2 19389986
    yego666
    Level 33  
    Posts: 2175
    Help: 239
    Rate: 564
    It seems to me that you would help yourself a lot by inserting some simple gate ( inverter ) with Schmidt input between the optocoupler and the ESP input. This should improve the shape of the optocoupler signal.
    In general, optocouplers are very slow compared to processors and other modern circuits, so you can expect good results from such a treatment.
  • ADVERTISEMENT
  • #3 19390019
    endriuh28
    Level 9  
    Posts: 98
    Rate: 13
    yego666 wrote:
    I think you would help yourself a lot by inserting some simple gate ( inverter ) with Schmidt input between the optocoupler and the ESP input. This should improve the shape of the signal from the optocoupler.


    And could you give me some schematic of how to connect this and what gate model to give?
    And whether to leave this resistor?
  • Helpful post
    #4 19390036
    Anonymous
    Level 1  
  • Helpful post
    #5 19390056
    yego666
    Level 33  
    Posts: 2175
    Help: 239
    Rate: 564
    Here you go :) .

    I would use a circuit n.p. 74HC14 , which has Schmidt inputs and works from as low as 3V supply.
    A single cube has as many as six inverters, so you can use them for other purposes too.

    Search the internet for "74HC14 pdf" . You will find a description and the layout of the legs of this cube. Cost less than 1 Pln.
    Attachments:
    • Problem with ESP32 interrupts originating from the PC817C optocoupler ESP32.png (11.93 KB) You must be logged in to download this attachment.
  • ADVERTISEMENT
  • #6 19390569
    endriuh28
    Level 9  
    Posts: 98
    Rate: 13
    I'm running out of space on the board and I don't want to enlarge it because it won't fit into my case.
    Is there perhaps a smaller version of this 74HC14 chip, with one input and output?

    khoam wrote:
    This resistor can be left. GPIO35 has no internal pull-up or pull-down.
    .
    Yes I know, I already implied this with the fact that even though it is INPUT_PULLUP it is INPUT.
  • Helpful post
    #7 19390637
    yego666
    Level 33  
    Posts: 2175
    Help: 239
    Rate: 564
    There are single gates n.p. here: Link , but you have to buy min. 5 pcs at 1.25pln/piece. A bit expensive.
  • #8 19390673
    endriuh28
    Level 9  
    Posts: 98
    Rate: 13
    yego666 wrote:
    There are single gates n.p. here: Link , but you have to buy min. 5 pcs at 1.25pln/piece. A bit pricey.


    I found this chip on Alegro for 1zł.
    And is it perhaps in a THT through-hole housing?

    And if I use this gate, when there is theoretically a high state, i.e. some voltage from PC817
    then there will be a low state behind the gate, did I understand correctly?
    If so, I now have to set it to RISING instead of FALLING mode.

    Of course I am going to use the SMD version anyway when I update the board,
    but in order not to bin the five boards I have, I would make an adapter on a universal board.

    Thank you yego666 , you have helped me a lot, many thanks to everyone for participating in this topic of mine.
  • Helpful post
    #9 19390691
    yego666
    Level 33  
    Posts: 2175
    Help: 239
    Rate: 564
    You make a good combination with the polarity :) .
    Unfortunately the tht version they don't produce :( .
    Make a prototype from one gate of the 74hc14 chip in the tht version.
    This will probably be the easiest way.
    Good luck.
  • ADVERTISEMENT
  • #10 19391026
    endriuh28
    Level 9  
    Posts: 98
    Rate: 13
    Ok, in that case that's what I'll do, or I'll somehow solder this SMD chip to a universal double sided board, maybe it will fit.
    I'll buy a couple of pieces of this with THT and a couple of pieces with SMD.

    Thanks again for your help yego666 .
  • #11 19397716
    krzbor
    Level 29  
    Posts: 1731
    Help: 40
    Rate: 1044
    khoam wrote:
    The GPIO inputs on the ESP32 do not have a Schmitt gate on the input (like the AVR),
    .
    I didn't know that (habit from AVR). And how is it in the ESP8266?
  • #12 19397739
    Anonymous
    Level 1  
  • #13 19402407
    endriuh28
    Level 9  
    Posts: 98
    Rate: 13
    Yes, I had a similar problem with the ESP8266 Pro Mini, only there what was different was the source of the PWM signal, so colleague khoam is right.
    At the time I thought the problem was with the circuit I was connecting.

Topic summary

✨ The discussion revolves around issues with detecting interrupts from a PC817C optocoupler connected to an ESP32 (DevKitC ESP-WROOM-32U) via GPIO35. The user experiences asynchronous and double interrupts, which are not present when using an Arduino UNO. Suggestions include using a Schmitt trigger inverter (e.g., 74HC14) to improve signal integrity, as the ESP32 lacks internal Schmitt trigger inputs. The resistor configuration can remain unchanged, and the user is advised to adjust the interrupt mode to RISING if using an inverter. The conversation also touches on the availability of single gate versions of the 74HC14 and the user's plans for board modifications.
Generated by the language model.

FAQ

TL;DR: "Use a 74HC14 with Schmitt inputs"—it has 6 inverters and works at 3 V—to clean PC817C edges causing ESP32 double interrupts. Insert it between the optocoupler and GPIO35; flip the interrupt edge because it inverts. [Elektroda, yego666, post #19390056]

Why it matters: This helps ESP32 builders stabilize noisy optocoupler interrupts (zero-crossing, PWM, sensor edges) without redesigning the whole system.

Quick Facts

Why does my ESP32 detect double or unsynchronized interrupts from a PC817C?

ESP32 GPIOs lack Schmitt inputs. Slow, noisy transitions from the PC817C can linger near the switching threshold. That region causes multiple logic transitions, which the interrupt logic sees as extra edges. Clean up the waveform before it reaches the ESP32 to stop re-triggers. [Elektroda, khoam, post #19390036]

Whats the simplest hardware fix for these false interrupts?

Add a Schmitt-trigger inverter between the optocoupler and the ESP32. "Use a 74HC14 with Schmitt inputs." One 74HC14 gives six channels and works at 3 V, so it fits 3.3 V systems well. This hardens the threshold and squares up slow edges. [Elektroda, yego666, post #19390056]

How do I wire a 74HC14 between PC817C and ESP32?

  1. Power the 74HC14 from the ESP32s 3.3 V and GND.
  2. Feed the PC817Cs transistor output into a 74HC14 input.
  3. Route that inverters output to your chosen ESP32 GPIO. This adds hysteresis, so the ESP sees clean, single transitions rather than slow slopes. [Elektroda, yego666, post #19390056]

Do I need to change the interrupt edge after adding the inverter?

Yes. An inverter flips logic. If you previously used FALLING, youll likely switch to RISING, or vice versa. Adjust your attachInterrupt edge to match your intended event polarity after inversion. [Elektroda, yego666, post #19389986]

Should I keep the 10 ka pull-down on GPIO35?

Yes. GPIO35 lacks any internal pull-up or pull-down, so an external resistor remains necessary. Keeping your existing pull ensures a defined idle level on that input. [Elektroda, khoam, post #19390036]

Is there a tiny single-gate Schmitt inverter to save PCB space?

Yes. A single-gate MC74VHC1G14 in SOT-353 is a compact option. Typical purchase is 5 pieces at about 1.25 PLN each. That lets you condition one signal without adding a full hex inverter. [Elektroda, yego666, post #19390637]

I used INPUT_PULLUP on GPIO35do I get an internal pull-up?

No. On GPIO35, INPUT_PULLUP does not enable an internal pull-up because none exists. Treat it as a plain INPUT and provide an external pull resistor to define the level. [Elektroda, khoam, post #19390036]

Does ESP8266 have Schmitt-trigger inputs like AVR?

No. ESP8266 behaves like ESP32 here. It also lacks Schmitt-trigger inputs on GPIOs, so slow edges can mis-trigger without conditioning. [Elektroda, khoam, post #19397739]

Can software filtering alone fix the double interrupts?

It can help, but its not ideal at higher rates. "You wrote something like a software Schmitt flipper," but external conditioning is better when sampling faster. Hardware hysteresis avoids jitter and missed events. [Elektroda, khoam, post #19390036]

My scope shows rounded or sine-like edges from PC817Cwhats happening?

Optocouplers switch slowly, so their outputs rise and fall with long slopes. That looks rounded on shorter timebases. Adding a Schmitt-trigger gate squares those slow transitions and stabilizes thresholds. [Elektroda, yego666, post #19389986]

Will 74HC14 run from the ESP32s 3.3 V rail?

Yes. The 74HC14 works from around 3 V, so 3.3 V operation is fine. It provides six inverters, letting you condition multiple signals on the same chip. [Elektroda, yego666, post #19390056]

Im tight on spacecan I prototype without respinning my PCB?

Yes. Use a DIP 74HC14 on perfboard and wire one gate inline with the signal. Single-gate THT versions arent produced, so this DIP approach is the simplest bridge solution. [Elektroda, yego666, post #19390691]

Is using CHANGE interrupt with a state variable a valid workaround?

It helps suppress duplicates. The pattern toggles state on rising and confirms on falling before executing. The OP reports it removed double triggers, but timing stayed imperfect. [Elektroda, endriuh28, post #19389968]
Generated by the language model.
ADVERTISEMENT