Relay only works if coil pin is connected directly to the Arduino [solved]

I have an Arduino Uno R3 and am using a 5V 5-pin relay to make this stuffed bear I have play music.

AFAIK, I think it's probably not safe to connect an Arduino pin and ground to the relay coils. So I googled to check how others were doing it and they were using a transistor controlled by an Arduino pin to control the relay coil activation.

But when I tried using a transistor, the relay didn't work (no current, no 'click'). I verified my circuit by:-

  1. using the relay to control an LED in the same way and it worked;
  2. connecting the bear's pins directly together and it worked;

After experimenting a bit more, I discovered that the relay didn't work even if I didn't use the transistor but placed a diode (correctly oriented) between the pin and the coil. That narrows down the cause of the problem a bit more, I think.

I do not know why this is happening. I'm thinking that maybe the relay generated some back-current or w/e and prevention of that flowing screws with the relay.

I am a n00b when it comes to hardware, so please excuse that. To be honest, this is the first time I'm posting in a forum (if that sounds weird, consider that I'm 13 and I've been spoiled by StackExchange :smiley: ).

So my question are:-

  1. is it safe to connect an Arduino pin and Arduino-supplied ground directly to the relay?
  2. why doesn't the relay work when I use a diode or capacitor?

I hope I've provided enough information. I don't want to appear lazy in asking a question answered by volunteers. If necessary, I'll try to make a circuit representation using Fritzing.

TL;DR: The issue has been successfully resolved 10x to all the great guys who've helped me on this one. Read: Relay only works if coil pin is connected directly to the Arduino [solved] - #51 by system - General Electronics - Arduino Forum

Hi and welcome.
You're at 4 posts now, sure hope you didn't post the same question in other parts of this forum (that's not smart to do because how are you or anyone else going to keep track of all of those, and it's just rude to force your problem all over the forum).

Answers to the quetsions:

1: never do that !
2: We can't tell. Put a clear photo or drawing of the setup and tell us what components exactly you used.

Schematic of how you connected the transistor and what transistor did you use?

YatharthROCK:
To be honest, this is the first time I'm posting in a forum (if that sounds weird, consider that I'm 13 and I've been spoiled by StackExchange :smiley: )representation using Fritzing.

Your age has no relevance to the question.

  1. is it safe to connect an Arduino pin and Arduino-supplied ground directly to the relay?

No. Although your words do not quite make sense.

2) why doesn't the relay work when I use a diode or capacitor?

It is hard to say because your description of what you did is not clear.

If necessary, I'll try to make a circuit representation using Fritzing.

Please please no. Just draw a schematic of what you tried, photograph it and post it. Also post the code, many beginners forget to set the pinMode to be an output.
Please use proper code tags, see the how to use this forum sticky post.

MAS3:
You're at 4 posts now, sure hope you didn't post the same question in other parts of this forum (that's not smart to do because how are you or anyone else going to keep track of all of those, and it's just rude to force your problem all over the forum).

No, I didn't. I am confident that I didn't start more than the 2 topics mentioned here. The other 2 'posts' therefore must be replies to this question.

MAS3:
1: never do that !

I actually kind of was doing it for about 5 minutes now. The "ON" LED was always on (it never turned off to show a short circuit) and the pin is still working. Can anything else go wrong? Should I be worried?

MAS3:
2: We can't tell. Put a clear photo or drawing of the setup and tell us what components exactly you used.

OK. I shall either take a photo or post a Fritzing-generated circuit schematic or breadboard picture.

I meant that this being my first post in any forum online was less weird as it sounded as I use StackExchange a lot. Age is no barrier when you have the internet :wink:

Grumpy_Mike:

YatharthROCK:

  1. is it safe to connect an Arduino pin and Arduino-supplied ground directly to the relay?

No. Although your words do not quite make sense.

YatharthROCK:
2) why doesn't the relay work when I use a diode or capacitor?

It is hard to say because your description of what you did is not clear.

I think everything should be clear once I actually represent my circuit.

Grumpy_Mike:

If necessary, I'll try to make a circuit representation using Fritzing.

Please please no. Just draw a schematic of what you tried, photograph it and post it. Also post the code, many beginners forget to set the pinMode to be an output.
Please use proper code tags, see the how to use this forum sticky post.

OK, you in all likelihood probably know more than me about this stuff.

OK, you in all likelihood probably know more than me about this stuff.

Given that there is a 48 year age gap then you are probably right.

Grumpy_Mike:

OK, you in all likelihood probably know more than me about this stuff.

Given that there is a 48 year age gap then you are probably right.

Wow, I sound so arrogant online. Sorry for that and my poor formatting. I'll use the 'preview' feature from now on.

Wow, I sound so arrogant online.

No problems :slight_smile:

Seriously though you are doing something wrong and we can help you find out what it is. But as all we have to go on so far is words it is not enough. You will be able to turn on a relay through a transistor if you do it right. What we have to find out is what you are doing. Fault finding is a skill, some people never manage to crack it, but if you can it lets you go much further. However, fault finding your own mistakes can be hard because what ever brain fart you made tends to be repeated. I have found that most of the time the bit that turns out to be wrong is the bit that was so obviously right it wasn't worth checking.
Good luck and looking forward to helping you.

2 pics of my crude circuit (with all the extraneous components removed) are attached.

Here's my code:

// set pins
const int BEAR = 2;
const int delay_ = 2000;

// init vars
char char_;
String command = "";

// init program
void setup() {
  pinMode(BEAR, OUTPUT);  // init pin as `OUTPUT`
  Serial.begin(9600);     // being data communication with the computer at 9600b/s
}

// convenience function to 'momentarily' close the relay
void play() {
  digitalWrite(BEAR, HIGH);
  delay(delay_);
  digitalWrite(BEAR, LOW);
  delay(delay_);
}

// called when some Serial data arrives
void serialEvent() {
  // while we still have data to read
  while( Serial.available() ) {
    char_ = Serial.read();   // read char from buffer
    command += char_;        // and add it to `command`
    
    // if we've reached the end of the message, `play()`!
    if (char_ == '\n') {
      play();
    }
  }
}

// event loop
void loop() { }

Ok you have no resistor in the base of the transistor. You don't say what the transistor is so there is no way of knowing if you have it the right way round.

Test your code by seeing if it lights up the on board LED on pin 11.

Grumpy_Mike:
Ok you have no resistor in the base of the transistor.

Should I have one? I can understand when you're making a full circuit with no components that you need to prevent excess drawing of current, but when connected to a transistor?

Grumpy_Mike:
You don't say what the transistor is so there is no way of knowing if you have it the right way round.

It is a BC547 NPN transistor with the pin configuration (as far as I can tell) Collector, Base, Emitter.

Grumpy_Mike:
Test your code by seeing if it lights up the on board LED on pin 11.

It works as expected to (and it's pin 13 on my board, maybe b'coz I have a newer one).

What do you have the other side of the relay coil connected to? You have used nicely color-code wires, and it looks black, which you're using for ground. For the NPN transistor relay circuit, the other side of the coil must be connected to +V...

westfw:
What do you have the other side of the relay coil connected to?

I'm not really sure what exactly you're asking. The coil is connected as mentioned below in my answer. The COM and NO pins of the relay are connected to the bear's circuitry (green and blue wires). The NC relay pin isn't connected to anything. On close of the circuit, the music starts playing. I am not sure which of the bear's wires is positive and negative.

westfw:
You have used nicely color-code wires, and it looks black, which you're using for ground.

Yes, black for ground, red for live (+5V), yellow for wires carrying the Arduino pin's voltage and green and blue for the bear.

westfw:
For the NPN transistor relay circuit, the other side of the coil must be connected to +V...

Umm... I didn't quite get you. The 'other side of the coil' as in? One pin of the relay coil is connected to the +5V transistor setup controlled by the Arduino and the other pin of the coil is connected to the Arduino's ground.

You can't report your own posts (I suppose that makes sense) but if you report someone's reply the moderators will work it all out. :slight_smile:

Your other thread deleted, as requested.

const int BEAR = 2;

In your photo the yellow wire looks like it is in pin 12, not pin 2.

From the photo I can see that you have the transistor connected in the emitter-follower configuration. This will give you at most about 4V across the relay. Try connecting it like this Electronics | DS instead, however I would reduce the base resistor to 1K.

PS - although standard relays need too much current to be driven direct from an Arduino pin, you can get reed relays that only need 10mA @ 5V, and can therefore be driven directly. Reed relays can't switch high voltages or currents, but are ideal for switching low voltage, low current signals. A reed relay might well be suitable for your application.

While the usual NPN transistor circuit for driving a relay is turned ON with a HIGH (5V) signal from Arduino, it switches the transistor to GND, so the other side of your load (the relay coil) needs to go to +5V, as shown here:

I'd add a diode to that circuit (place it backwards in parallel to the relay) to protect the transistor.