A very efficent zero crossing dimmer (direct pin control from timer)

Hi, my goal is to use a Mega 2560 to dim 2 group of led lights, using leading edge way.
There are different approaches to realize that, but looking around I can't find efficent examples, libraries managing timers are probably a bit slow and most of them are designed for millisecons purposes while I need microseconds to get accurate low dim outputs, driving pins with digital.write on an interrupt routine takes cycles too.. not really efficent.

I've read that timers can drive hardware particular pins, this is for PWM purposes, and I would use the same concept, but simply I don't want that the timer reloads after reaching zero, I want to start the counter next time only when the zero is crossing again.
In this case I can use only an interrupt to get the zero crossing, the timer will deactivate the hardware pin automatically, without need to use interrupt from timer.

Shortly, I think I can load a value in the timer, using it as a counter instead, and have a reflection on the corrispondent hardware output, is that possible? One timer for one dimmer.

Beeing more concrete describing the idea, theorically, thinking that the corrispondent hardware connected pin is low while the counter is 0, and high with any value <>0 is loaded on the counter, I trigger an interrupt from another pin when the zero is crossed, and in the interrupt routine I'll load the desidered dim value on the counter, reflecting an immediate HIGH on hardware pin output, quitting back to loop to do something else, then the counter beging to decrease by the clock to zero, reflecting then a LOW on hardware pin outputm the next zero will repeat the process. Exteamely clean and efficent, the CPU can do anything else without worries, no glitches.
LOW or HIGH output is not important, I can manage the hardware according to this.

To reach the result I have to set timer register values manually, as I read I have timer 3, 4 and 5 free for this purpose, can please somebody help me with thise values? Thank you a lot!

It appears that you have focused on some sort of PWM solution using hardware timers for dimming these two groups of leds.
Can you describe what waveform you expect to see on the led pins with frequency and duty cycle and how these should change during the execution of your code.
Provide also a diagram of how the leds are to be wired.
Zero crossing detection, which you have mentioned in your title, is appropriate for control of AC circuits only.

1 Like

@nickgammon wrote a nice tutorial on setting the timers:

How fast do you need to the the switching? 60Hz has a 16667us period, and plain old micros() with 4us resolution could slice that to better than 12bit resolution. Do you actually need the interrupts and timers?

You might experiment with micros() timers rather than millis(), saving the zero-crossing micros() and doing some

...
if(led1Armed & (now - lastZCmicros > led1Interval)){
   digitalWrite(led1Pin,HIGH);
   led1Armed = false;
}

or

Thank you mates for your replies!! The last post of Juraj seams to be exactly what I'm looking for! Thanks!!
Seams projected fro triac which is a leading edge HW, I?m focusing instead for LED driver 220V lamps, which supports only trailing edge. I'm getting ask to make it compatible

To clear more my purpose, I'm not focusing to any PWM driver/DC controller, I'm focusing on AC dimmer with hardware control output

I am having a problem following what you want.

You mentioned two groups of LED lights—are they household lamps or individual LEDs? You also mentioned triacs, which suggests you're working with an AC power source, such as mains power, typically operating at 50 or 60 Hz. While 400 Hz systems exist, they’re not used in home applications.

Keep in mind that a triac can be gated in one of four quadrants. Which quadrants are you working with? Also, note that once a triac is gated, it won’t turn off until the current falls below the minimum holding current, usually at the zero-crossing point.

As I understand leading edge is:

In the context of LED lighting, the term "leading edge" typically refers to leading-edge dimming or phase-cut dimming. This is a method used to control the brightness of lights, including LEDs, by modifying the power delivered to the light source.

Here’s a brief explanation:

  1. Leading-Edge Dimming:
  • In leading-edge dimming, the dimmer cuts off the beginning (or "leading edge") of each AC power cycle. This reduces the amount of power that reaches the light, dimming it in the process.
  • It is commonly used with traditional incandescent bulbs and some halogen lights, but can cause issues with LEDs unless they are compatible with this dimming method.
  • This method can sometimes result in flickering or uneven dimming when used with LED lights not specifically designed for leading-edge dimmers.
  1. How it Works:
  • The dimmer quickly turns on and off the power during the first part of each AC cycle. By cutting off part of the waveform, it reduces the energy delivered to the light, lowering brightness.
  1. Compatibility with LEDs:
  • Some LED lights are designed to work with leading-edge dimmers, but many are more compatible with trailing-edge dimmers, which cut the waveform at the end of the AC cycle. Trailing-edge dimming is generally smoother for LEDs and causes less flicker or noise.

In summary, leading-edge dimming is a method of reducing power to control the brightness of lights. It was primarily used for incandescent lighting but is less ideal for modern LED lights unless they are specifically designed for it.

Hi mate, thanks for explaining, I'm really sorry to have mistaken trailing with leading.

I need trailing, which is the only fully compatible way with LED bulbs, 220V, dimmable.
This circuit doesn't use triacs, it uses 2 mosfets, to manage positive and negative half waves, and it needs an enable signal for all the necessary period, not only a signle pulse that works with triacs.

The library Juraj posted seems ideal, it gets the interrupt of zero crossing which load up the timer as counter, which is connected to the hardware pin, so the CPU is free to do what it wants, and the process is mostly hardware managed. Yes I need 2 dimmers, it needs 2 timers of Mega 2560, they are available so all is ok.
I have to look inside the library, cause it's designed for triac, it works opposite than I need and I need to avoid only pulses. I hope the author is interested to make it compatible with trailing edge, soon I'll post the scheme

This is the circuit I've designed for 220V (for 110 you need to recalculate), it's efficent, works with low temperatures and consuming 0,147W on st-by, mosfets have only 0,57ohm resistance and support up to 10A (but I recoment use till 3A, in this case you can use a small heat dissipator and you can put the 2 mosfets on the same radiator, containing spaces), am extra diode is used to reduce the forward voltage of the internal mosfet's diode, lowering mosfet's dissipation and working temperature, they can be eventually avoided.
The circuit is driven by 2 optocouplers, one is used to activate mosfets, the second to discharge the gates quickly, leaving mosfets operate on saturation only.
The microcontroller used should be loaded enough to assure optocouples create the most available saturation of transistors, to fast charge/discharge gates. For 5V microcontroller a resistor of 390 ohm assures about 13mA for light the opto's leds, on low and high; for 3,3v controllers use 220/270 ohm. The circuits activates the load with LOW logic and turn mosfets off with HIGH level, this is preferable according the fact microcontrollers boot usually with pins on input mode and high pullup internal resistor. Only a forced LOW level can turn on mosfets.
Snubber is optional, by the way it's better to use anycase to preserve life.
I've added the zero crossing detector, I have to work on it more, probably I'll insert a capacitor to avoid noise, maybe it's not necessary an EMI filter. I've to focus to optimal values for resistors to reduce dissipation without enlarging too much the zero crossing LOW time detect.
The zero crossing takes about 0,5W dissipation, I guess it's possible to combine it with the power needed to turn mosfets but the result won't change too much

Hello, I am very interested in the 220v zero-crossing detection circuit diagram you shared. But I can't see your picture very clearly. I hope to get a clearer picture. Because I am also designing this zero-crossing detection circuit, I sincerely hope you can help me. Also, I want to know what is the maximum temperature of the resistor on this circuit, this is very important to me.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.