I have been developing a product that uses an atmega8a to turn on/off 5vdc relays. The layout is simple: 5v directly to relay coil, coil to npn 2n2222a transistor, transistor to ground, transistor base triggered by arduino pin. A reverse diode is connected on coil to protect from spikes. The atmega8a is working on its internal 8MHz oscillator.
The problem is when i connect a load to the relay's contacts, like a 220v fan or light, and turn on/off the relays, the atmega resets immediately which also obviously turns off the relay. This behavior is seen almost never when no load is connected to the relay. I also have an esp8266 in the same circuit and communicating with atmega through i2c and a third wire for slave (atmega) to start communication with esp. ESP receives it as interrupt and I can see that, only sometimes though, there are too many interrupts that even makes the ESP reset.
I have tried:
- operating the atmega at 3.3v and 5v, both
- i have tried many relays, different pieces and different models, sugar cube and g5nb mini relays
- i have tried using an inductor in line with the load in hope to eliminate some spikes
- i thought maybe the current requirements weren't being met. so instead of a 5v 1a smps, i used a 5v 2a smps
- used capacitor and snubber across the relay contacts to limit spikes
- used an external power adaptor instead of on board smps circuit. This adaptor had more filtering
- tried using a resistor and even a diode between arduino pin and transistor base
- switched transistor to a BS170 mosfet
- thought the track carrying AC was maybe too close to atmega for comfort so i cut the track and used a long enough wire for testing just to make sure this wasn't it
- added a 1000uF cap on power source
Then I ran out of ideas and came to this forum. I hope someone knows what to do and why my atmega just wont simply turn on relays with loads on it.