Arduino based triac dimmer bulb flicker fix on touching arduino ground

Hi all,
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() {                        
  
}

Please let me know the solution for same.

Is the 0V of '328 connected to electrical safety earth? It should be.

When you touch the circuit you are giving it an earth, although not a very good one, that might account for stopping the flicker.

Thanks for your interest PerryBebbington. No, I hadn't connected 328 with earth and reason for that is because my house doesn't have proper earthing. I am using a 5v power supply with just two wires i.e. Live and Neutral. I know that circuit is supposed to be earthed, but I have a robodyn triac based dimmer and it is working fine, no flickering, and I am driving it with same power supply.

Bulb stops flickering as soon as I touch Vcc or ground or connect it with my laptop with F232R programmer.

No, I hadn't connected 328 with earth and reason for that is because my house doesn't have proper earthing.

That's scary! If the house does not have proper earthing then probably there are other serious faults with the electrical installation. I urge you to get it sorted before the house burns down or you get electrocuted. Yes, I am being very serious.

I'm afraid I don't have any further useful advice on your problem, sorry.

Try to cover your circuit with shrink tube. it might having noise. also do not use your laptop power supply to power up the circuit. use a 5V charger or supply.

PerryBebbington:
That's scary! If the house does not have proper earthing then probably there are other serious faults with the electrical installation. I urge you to get it sorted before the house burns down or you get electrocuted. Yes, I am being very serious.

I'm afraid I don't have any further useful advice on your problem, sorry.

I tried in my brother's house which has proper earthing and there also bulb is flickering. I strongly think that the problem is somewhere else because as I said, robodyn dinner works perfectly with arduino uno.

Your circuit diagram shows D2, your code has int AC_pin = 3;
There's no triac snubber.