I've designed a triac based dimmer circuit. It is working just fine except the bulb is flickering on dimming (working fine with full brightness). Bulb I am using is filament based and dims perfectly from other dimmers. I am attaching schematic of my circuit and code. The strange thing that I am noticing is that the flickering stops as soon as I put my finger on atmega's ground or Vcc pin. On touching these anywhere on the circuit, flickering stops immediately. Circuit contains standalone Atmega 328p au circuit along with dimming circuit. I've placed 0.1uf ceramic caps between both Vcc and ground. Also a 100uf electrolytic cap between Vcc and Ground from incoming supply.

Code that I've used is this, and I am pretty sure that code is okay because it is working fine with the dimmer circuit that I've bought from market.

#include  <TimerOne.h>          
volatile int i=0;               
volatile boolean zero_cross=0;  
int AC_pin = 3;                
int dim = 064; // half brightness                 
int inc=1;                     

int freqStep = 75;   

void setup() {                                     
  pinMode(AC_pin, OUTPUT);                         
  attachInterrupt(0, zero_cross_detect, RISING);   
  Timer1.attachInterrupt(dim_check, freqStep);                                                 

void zero_cross_detect() {    
  zero_cross = true;              
  digitalWrite(AC_pin, LOW);      
void dim_check() {                   
  if(zero_cross == true) {              
    if(i>=dim) {                     
      digitalWrite(AC_pin, HIGH); 
      zero_cross = false; 
    else {

void loop() {                        

This is the schematic:- Schematic

Please let me know the solution for same.

  • \$\begingroup\$ The recommended way to attach an interrupt is attachInterrupt(digitalPinToInterrupt(D2_pin), zero_cross_detect, RISING). See attachInterrupt() for details. \$\endgroup\$
    – tim
    Jul 20, 2020 at 17:13
  • \$\begingroup\$ you code isn't ideal arduino.stackexchange.com/questions/63631/… \$\endgroup\$
    – Juraj
    Jul 22, 2020 at 11:37
  • \$\begingroup\$ Tried attachInterrupt(digitalPinToInterrupt(D2_pin), zero_cross_detect, RISING) and also @Juraj code, but the problem still persists. Also I have a feeling that problem is in circuit and not in code. \$\endgroup\$
    – Rishabh
    Jul 24, 2020 at 9:48

1 Answer 1


Is that a coil, connected to ATmega's D3 pin?! Why is there this 0.1 uF capacitor at MOC3021 input?

This coil and the 0.1 uF capacitor make a LC circuit, that delays the moment when the optocoupler conducts, and so it delays the moment when the TRIAC will light the bulb.

Even if it is just a 470 ohm resistor, this resistor delays the charging of 0.1 capacitor, and that delays the triggering of the TRIAC, too. The value of 470 ohm for this resistor could be reduced, too.

Take this 0.1 uF capacitor out of optocoupler's input, and use a resistor with the valua about 220 ohm or 330 ohm for connecting ATMega's D3 pin to MOC3021 input, and everything will be ok.

  • \$\begingroup\$ Thanks for your interest. I tried removing 0.1uF capacitor and also replaced 470 ohm resistor with 220 ohm, but it didn't helped, still the bulb flickers. Stops as soon as I touch ground or Vcc with my hand. Also it does not flicker when connected to laptop with F232R programmer. \$\endgroup\$
    – Rishabh
    Jul 24, 2020 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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