I have an ESP-07 with NodeMCU firmware connected to a dimming module with a zero-crossing pin.

The current frequency should be 50Hz, which means that the time between two zero crossing should be 10ms, ~10000 microseconds.

However the time between two interrupts is sometimes correct, but often is about ~450 microseconds.

What's the catch? I use tmr to measure time

Here is the code:

gpio.mode(2, gpio.INPUT)

gpio.mode(5, gpio.OUTPUT)

t = tmr.now();

counter = 0;

local function zc_callback(level, pulse)

    counter = counter + 1;
    counter = counter % 200;
    if(counter == 1) then
        print(tmr.now() - t);

    t = tmr.now();


gpio.trig(2, "up", zc_callback)

Thank you, I try to figure this for some time, it affects my light bulbs which blink instead of dimming.

  • \$\begingroup\$ How long is your print going to take? Will everything wait until the print is finished? \$\endgroup\$ – Transistor Aug 1 '18 at 20:31
  • \$\begingroup\$ I don't think it should matter because the time is measured before printing \$\endgroup\$ – Mihai Ionuţ-Cosmin Aug 1 '18 at 20:42
  • \$\begingroup\$ But you're calling it every 10 ms if it's in the interrupt. Try taking it out. \$\endgroup\$ – Transistor Aug 1 '18 at 21:30
  • \$\begingroup\$ I think you may also be forgetting to disable the interrupt on entry and re-enable on the way out. Won't that allow it to retrigger if the print routine isn't finished? \$\endgroup\$ – Transistor Aug 1 '18 at 21:38
  • 2
    \$\begingroup\$ Do you have an oscilloscope? Have you used it to look at the zero-cross signal to check for noise? \$\endgroup\$ – brhans Aug 1 '18 at 21:55

Ok, I got this, the mistake was the "up", "high" is what I need.

| improve this answer | |
  • 1
    \$\begingroup\$ Thanks for closing this question. You can accept your own answer to mark it solved. \$\endgroup\$ – Transistor Aug 15 '18 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.