2 transistors driving 2 high amperage relays

Hello,

I am trying to control 2, 30A SONGLE relays (SLA-05VDC-SL-C) using 2 transistors (2N2222). I have some code posted below along with my schematic of my current setup. I have 2 timers which I want to control each relay independently so that 1 relay switched state every second and the other every 5 seconds. However it is as if the signals are getting crossed somehow and I noticed that they will both switch state every second and also every 5 seconds. So it starts by switching 4 times every second, then on the fifth second it stays on and the lights on the relay dim slightly and then brighten every second (for the 1 second timer) then this sequence repeats. I have tried putting diodes to block back EMF, tried switching up my orientation but nothing works. I am not entirely sure what is happening. Any help would be greatly appreciated! I put a voltmeter across the relay coil terminals and notice the voltage is around 4.8 volts then when the 1 second timer comes on the voltage drops to about 3.6V and so on. The reason I must control the relays with transistors is that I had to use a 5V power supply (the battery in the schematic) in order to get adequate current to flip the state of the relays as the 20mA from the Arduino just wasn't skookum enough to drive the coil. Again thanks for any help in advance!

const int Agitate1 = 5; //Set pin 5 to Agitator 1 trip (drives base signal for control-power circuit).
const int Agitate2 = 10; //Set pin 6 to Agitator 2 trip (drives base signal for control-power circuit).

unsigned long previousMillis1 = 0; //Will store the last time the outputs were updated.
unsigned long previousMillis2 = 0; //Will store the last time the outputs were updated.
const long interval1 = 5000; //Interval at which to blink.
const long interval2 = 1000; //Interval at which to blink.

int AgitateState1 = LOW;
int AgitateState2 = LOW;

void setup() {
pinMode(Agitate1, OUTPUT); //Set Agitator trip to output mode.
pinMode(Agitate2, OUTPUT); //Set Agitator 2 trip to output mode.
digitalWrite(Agitate1,LOW);
digitalWrite(Agitate2,LOW);
}

void loop() {

unsigned long currentMillis = millis();

if (currentMillis - previousMillis1 >= interval1){ //Nested if loop that checks to see if the interval time has elapsed yet.  If so, it flips the state of the relay for agitator 1.
  previousMillis1 = currentMillis;

  if (AgitateState1 == LOW) {
    AgitateState1 = HIGH;
  }
    else {
      AgitateState1 = LOW;
    }
  }

if (currentMillis - previousMillis2 >= interval2){ //Nested if loop that checks to see if the interval time has elapsed yet.  If so, it flips the state of the relay for agitator 2.
  previousMillis2 = currentMillis;

  if (AgitateState2 == LOW) {
    AgitateState2 = HIGH;
  }
    else {
      AgitateState2 = LOW;
  }
}
    digitalWrite(Agitate1, AgitateState1);
    digitalWrite(Agitate2, AgitateState2);
}

Code as image, really!? Have a look at How to use the forum please.

That is no schematic. Here is a schematic of the connection for a relay driven by an 2N2222.

septillion:
Code as image, really!? Have a look at How to use the forum please.

Sorry, didn't see the option. Edited it to include code tags.

groundFungus:
That is no schematic. Here is a schematic of the connection for a relay driven by an 2N2222.

Schematic attached above, I tried your schematic with the diode and it didn't help any with my problem. I can drive one no problem but my issue is when I am trying to drive two transistors independently.

I tried your code with 2 LEDs in place of the relay drivers and the LED attached to pin 5 blinks 5 seconds on and 5 seconds off, the LED attached to pin 10 blinks on for 1 second and 1 second off. If your relays are not working like that, there is probably a wiring or power problem.

groundFungus:
I tried your code with 2 LEDs in place of the relay drivers and the LED attached to pin 5 blinks 5 seconds on and 5 seconds off, the LED attached to pin 10 blinks on for 1 second and 1 second off. If your relays are not working like that, there is probably a wiring or power problem.

Thank you for verifying the code! Any idea based on my schematic where the wiring problem or power problem may be? I am thinking it is likely something to do with the two power sources being mixed up or something. I have the 5V low current rail and then the 5V high current rail which actually drives the relays.

If you have it wired like in the fritzing in the original post, I am surprised that it works at all.

The 2 relays should be wired identically.

Red wire from battery to one side of relay coil.
Other side of relay coil to collector of the transistor.
Emitter of the transistor to breadboard ground.
Diode across the relay coil, banded end of diode to red wire of battery.
Black wire of battery to breadboard ground.
Digital output pin to one side of resistor.
Other side of resistor to transistor base.
Arduino ground, battery ground and transistor emitter all connect to breadboard ground.
Arduino 5V NOT connected to the breadboard.

The flyback diode is important. If you have been operating the relays without one, the transistors could be compromised (ruined).

groundFungus:
If you have it wired like in the fritzing in the original post, I am surprised that it works at all.

The 2 relays should be wired identically.

Red wire from battery to one side of relay coil.
Other side of relay coil to collector of the transistor.
Emitter of the transistor to breadboard ground.
Diode across the relay coil, banded end of diode to red wire of battery.
Black wire of battery to breadboard ground.
Digital output pin to one side of resistor.
Other side of resistor to transistor base.
Arduino ground, battery ground and transistor emitter all connect to breadboard ground.
Arduino 5V NOT connected to the breadboard.

The flyback diode is important. If you have been operating the relays without one, the transistors could be compromised (ruined).

Both transistors are wire identically, looking at the fritzling diagram it shows them wired the same, no? I removed the +5V from the breadboard but nothing was connected to it anyways, just leftover from when I had another sensor on the board and forgot to remove it. I tried the flyback diode (which i assume is just a normal diode?) but it did not fix the problem. I assume the flyback is simply to protect against back EMF when the solenoid electric field collapses from what I have read. I checked both transistors and they are both still fine. I am at a loss, I cannot understand how the voltages are mixing with one another. I even tried putting diodes between both the positive and negative wires to the power supply but it had no effect.

Fritzing diagrams are worse than useless.

You probably wired the transistors backwards, but you would never know from that ridiculous diagram.

jremington:
Fritzing diagrams are worse than useless.

You probably wired the transistors backwards, but you would never know from that ridiculous diagram.

How so? It's a pretty simple circuit and I think they're a great visual as you can see exactly what my setup looks like. I didn't wire the transistor backwards or the circuit would be always on as the relay would be seeing a constant 5V. Also the fritzling shows the direction the transistor is facing (the flat on the housing) which is easily referenced to the data sheet...

Your diagram is wrong. The transistor is wired on the high side. That doesn't work well with NPN transistors.

as you can see exactly what my setup looks like

No, you can't, because the base, emitter and the collector of the transistors are not identified on that idiotic diagram. Not even the PART is identified, out of somewhere between 10,000 and 40,000 things that come in a TO-92 package.

I agree with groundFungus: if you have the EBC arrangement for this particular version of the 2N2222 transistor (one of several possible), it is wired incorrectly.

M4g1c45:
How so? It's a pretty simple circuit and I think they're a great visual as you can see exactly what my setup looks like. I didn't wire the transistor backwards or the circuit would be always on as the relay would be seeing a constant 5V. Also the fritzling shows the direction the transistor is facing (the flat on the housing) which is easily referenced to the data sheet...

You are confusing a wiring diagram with a circuit diagram/schematic.

A wiring diagram is solely for construction of the circuit by rote.

A circuit diagram is the language of electronics - its designed to show the circuit's essential structure
as clearly as possible in the abstract, irrespective of construction details. There are conventions used in
circuit schematics that are important to allow such clarity, supply rail at the top, signal flow left to right,
symmetry of symmetric configurations reflected (no pun intended) in the diagram. Standard symbols for
types of components are used.

Once you are fluent in such diagrams you can take them in literally in a glance. A wiring diagram is
spaghetti by comparison.

jremington:
No, you can't, because the base, emitter and the collector of the transistors are not identified on that idiotic diagram. Not even the PART is identified, out of somewhere between 10,000 and 40,000 things that come in a TO-92 package.

I agree with groundFungus: if you have the EBC arrangement for this particular version of the 2N2222 transistor (one of several possible), it is wired incorrectly.

I have tried to reverse the wiring (i.e. switch the emitter and collector sides) the relays stay on. If they were wired backwards I shouldn't see the Arduino able to switch them on and off at all, right? I'll admit I got a large bulk pack of assorted transistors on Amazon and all they give me is the "2N2222" number, the transistor itself reads "2N2222A H 331". The box has no manufacturer. I figured a wiring diagram would be easier as I am not too comfortable with schematics (i have done a few in school but my background is largely mechanical). Figured that it would be easier to see if I was doing something incorrectly if you could see my physical wiring because if I made the schematic I would obviously draw the schematic to be correct to the transistor's base emitter and collector but seeing as I am not 100% on the wiring itself I figured it would be more visual. Let me know on my theory of it always being on if wired backwards though because the relay would see the +5V on the emitter but the switch would not actuate as the 5V signal to the Arduino would not be able to reach ground, correct?

The transistor is not wired backwards, it is in the wrong place.

When a switch switches power it is called a high side switch and sources current to the load. If the switch switches ground it is called a low side switch and is said to sink current. A NPN transistor needs to be on the low side for it to be biased to work correctly.

The transistor is not wired backwards

It is indeed wired backwards, if the OP has a transistor with pinout like this one from ON Semiconductor.

2n2222.png

2n2222.png

the transistor itself reads "2N2222A H 331".

I can only go by what the OP posts. The part number for the CBE pinout is P2N2222.

And the transistor still needs to be on the low side.

I can only go by what the OP posts.

I think it is a good idea to consider all the possibilities, and to illustrate the failures that lurk in making unwarranted assumptions. I certainly don't know the pinout of the OP's transistor.

groundFungus:
The transistor is not wired backwards, it is in the wrong place.

When a switch switches power it is called a high side switch and sources current to the load. If the switch switches ground it is called a low side switch and is said to sink current. A NPN transistor needs to be on the low side for it to be biased to work correctly.

Ok, so I switched around the transistor as jremington posted and i wired up the circuit diagram you sent me. I took a photo and labelled the connections I made, it makes no sense to me and didn't work so I am assuming I did something wrong per your circuit diagram (again I am not very good at circuit diagrams so I apologize). Literally nothing happens in this configuration.

tried to embed image and it won't show up...tried several hosting sites.