# Zero-crossing signal inconsistent timing

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);
end

t = tmr.now();

end

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.

• How long is your print going to take? Will everything wait until the print is finished? – Transistor Aug 1 '18 at 20:31
• I don't think it should matter because the time is measured before printing – Mihai Ionuţ-Cosmin Aug 1 '18 at 20:42
• But you're calling it every 10 ms if it's in the interrupt. Try taking it out. – Transistor Aug 1 '18 at 21:30
• 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? – Transistor Aug 1 '18 at 21:38
• Do you have an oscilloscope? Have you used it to look at the zero-cross signal to check for noise? – brhans Aug 1 '18 at 21:55