Electronic – arduino – How to switch a 220V AC electric bulb using a relay through Arduino

arduinorelay

My aim is to switch an electric bulb operating at 220v AC. For this purpose I use a 12V DC relay that can operate for 220V appliance according to its data sheet.
My relay circuit is as under:

enter image description here
Using a regulated power supply (a desktop PC's SMPS unit with 12v, 5v, and 3.3v outputs), when 5V is applied to the pin of microcontroller, I get a click sound from the relay and the bulb is switched ON and when the pin is kept open, the bulb is OFF.

But when using an Arduino Uno board for the same purpose (I have 12V, 5V, 3.3V and GND pins on the board so I give all of the supply from the board itself now not from the separate supply), when I connect 12V GND and 5V at the pin that goes to MCU pin the relay is making noise. I am getting a continuous clicking sound from the relay. If I connect the bulb to it, it may damage my bulb (I'm assuming this since the relay seems to be switching fast and continuously)

Why am I getting the noise of clicking continuously when I use Arduino instead of the power supply?

How could I solve this problem?

Best Answer

One issue is that you do not have a fixed grounding resistor on the transistor. This may not always be needed. However the Uno ports are not programmed at reset or when there is no program running. In those cases the port may be set to a high impedance input with or with out a light pull up resistance. In this case you might be unknowingly turning the transistor on intermittently with noise or pull up leakage current.

To insure stable operation be sure to set the controlling port pin quickly to a digital output and set the initial logic value. Adding a resistor to ground, (before or after the existing 10k) will help prevent an open port or a light pull up from activating the transistor. With a 5v Uno a matching 10k should be OK. Lower resistor values reduce the chance of unknowingly activating the transistor, but at some point the high signal will not be able to turn it on.

If immediate switching time is not critical adding a small cap on the transistor base to ground can prevent fast multiple ON-OFF cycles that could reduce the life of the bulb. (Maybe 1uf).