Issues in interfacing the Relay with Arduino

I want to control AC bulb with Arduino, 5v-relay. My connections are

My code is

#define RELAY1  7

void setup() 
{

pinMode(RELAY1, OUTPUT);
Serial.begin(9600);

}

void loop() 
{


 digitalWrite(RELAY1,LOW); // Turns ON Relays 1

Serial.println("Light ON");

delay(5000); // Wait 5 seconds

digitalWrite(RELAY1,HIGH); // Turns Relay Off

Serial.println("Light OFF");

delay(2000);

}

Even though I have given AC power supply, bulb is not lighting up. But one small LED (Red coloured one) on relay is blinking according to timings in the code.

*Power LED (Green colored one) is also on as I have given 5v supply from Arduino.

Is it possible the bulb is blown? Check that you're getting power on the bulb base connector terminals using a multimeter. Also check the relays voltage ratings.

Where did you get that AC battery?

I did not use the AC battery. I have given supply from power port (plug) in my home.

I did not use the AC battery, I have given the AC power supply from the power port (plug) in my home!

Some part of the problem was solved. It was problem with my AC power supply port. I have changed the port. Now bulb is glowing!

Now the remaining issue is

Bulb is not following the delay given in the code. For example: If we give 5 sec ON and 5 sec OFF, out put is 5 sec ON - 5 sec 0FF - 2 sec ON - 2sec OFF -2 sec ON - 2sec OFF - 5sec ON - 5sec OFF like that.

On monitor also, only first 3 actions are print after that no printing is observed.

Do you have a link to the relay you are using? I suspect that it draws more current than the Arduino can provide.

I don't have a link to the relay I am using. But I will upload the Image of it.
part no. on the transistor is: "CTBC 5478 JS"

Your code works fine. I would expect that the power to your arduino is not enough. Low voltage would cause unpredictable errors. One of the first signs is the serial port drops off and at the same time maybe enough power to run the relay.

Your code does 5 on and 2 off. Are you running the arduino from a battery? Maybe the batteries are low.

Expect ~75mA for tose 5volt relays, and some for the LEDs, and the Arduino.
You could be looking at ~200mA when both relays are on.
That could be too much for a 9volt block battery, even when fresh.
Leo..

I am not using any batteries to power the Arduino. I am connecting Arduino to my Laptop.

I am not using both the relays . I am using only one relay out of two relays.

My PC with a 1500 watt power supply will not give me enough power through the USB port to power my relays and my display properly. I use a 10 volt 1 amp wallwart. My USB ports won't keep up with charging the older tablets.

USB should be able to power one or even two relays.

A power spike could reset the micro.

Try changing the setup to this:

void setup()
{
pinMode(RELAY1, OUTPUT);
Serial.begin(9600);
Serial.println("Restarting");
}

Then you will know if a spike has caused the micro to reset.
Leo..

not sure of this :
delay(5000); // Wait 2 seconds

USB spec is for 500mA, but it seems that no all laptops can deliver that. if the laptop power is on battery, then you are using a battery.
it would be best to power the relay from an external power supply and have a common ground with your Arduino.

shivaaprs:
I did not use the AC battery. I have given supply from power port (plug) in my home.

He's referring to the (+) and (-) symbols that you drew on the AC supply.

@Wawa, I have included Serial.println("Restarting") as you said.

Now my code is

#define RELAY1  7

void setup() 
{
  
pinMode(RELAY1, OUTPUT);
Serial.begin(9600);
Serial.println("Restarting");

}

void loop() 
{
  

  digitalWrite(RELAY1,LOW); // Turns ON Relays 1

  Serial.println("Light OFF");

  delay(15000); // Wait 15 seconds

  digitalWrite(RELAY1,HIGH); // Turns Relay Off

  Serial.println("Light ON");

  delay(20000); //Wait for 20 seconds

}

I have 15sec, 20sec durations only in my code, but in between bulb is glowing for 1sec (such a small duration).

And the output on the serial monitor is:

Restarting
Light OFF
Light ON
LigèRestarting
Light OFF
Light ON
LigèRestarting
Light OFF
Light ON

==> We can conclude that Arduino is restarting in between the process. What might be the problem. How to resolve this issue?

Thanks in advance.

dave-in-nj:
not sure of this :
delay(5000); // Wait 2 seconds

I think that this "error" is just due to shivaaprs editing his code, and not updating the comment to reflect the current edit.

Now that you have used Wawa/Leo's technique, and found out that the Arduino is being reset, you can do further tests to help find the cause.

Try running your sketch (with Wawa's addition), but don't have the lamp plugged into the wall socket.

The results of this test will tell you whether it is the Lamp's current increase and or wiring that is causing the problem, or whether it is due to the relay coil being energised/de-energised.

OK. Drawing doesn't jive with photo.

Photo shows Gnd, P1, P2. I assume that P1 controls relay 1, P2 controls relay 2.

PCB connector doesn't make sense. There is a power LED (green) but I don't see any 'Power" connection. I would assume that P1 and P2 would control the gates of the transistors. Am I missing something? Really need the spec sheet of the pcb to know what is going on here. My guess is the the Arduino power is sagging, forcing a power on reset.

@ shivaaprs
Try with the lightbulb removed.
If it still reboots, it must be the supply.
How do you supply your Arduino.
If you're sure the supply is ok, try a capacitor across the supply of the relay board.
Close to the relay board. Try 100-1000uF.
That will bridge supply dips and absorb relay kickback.
Leo..