# Arduino based triac dimmer bulb flicker fix on touching Arduino ground

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.initialize(freqStep);
Timer1.attachInterrupt(dim_check, freqStep);
}

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

void loop() {

}


This is the schematic:-

Please let me know the solution for same.

• The recommended way to attach an interrupt is attachInterrupt(digitalPinToInterrupt(D2_pin), zero_cross_detect, RISING). See attachInterrupt() for details. – tim Jul 20 '20 at 17:13
• you code isn't ideal arduino.stackexchange.com/questions/63631/… – Juraj Jul 22 '20 at 11:37
• 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. – Rishabh Jul 24 '20 at 9:48