How to connect a relay module to an arduino

I have the relay directly connected to my board and it seems that it can't handle it when there electricity going through the relay module, it just freezes the program running it so I'm going to connect to my bread board but I'm wondering how that's done. Thanks in advanced.

whitewolf578: I have the relay directly connected to my board and it seems that it can't handle it when there electricity going through the relay module, it just freezes the program running it so I'm going to connect to my bread board but I'm wondering how that's done. Thanks in advanced.

Relay or relay module? I think a link to what you actually have and a picture of how you are wiring it up is needed before anyone can really answer your question.

DON’T DO THAT!!! You can burn the output pin.
You must use a transistor, a resistor and a diode (the last is very important). See this picture.

luisilva:
DON’T DO THAT!!! You can burn the output pin.
You must use a transistor, a resistor and a diode (the last is very important). See this picture.

Could be but the OP states RELAY MODULE and most such “modules” I’ve seen have on board transistor to drive the relay coil. So we shouldn’t speculate until the OP provides a link to what he actually has and how he tried to wire it up.

Its a relay module, here it is http://www.ebay.com/itm/131143451377?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649 there’s an attachment with a picture of my setup.

Yes, is a module, and I thought that was a relay "alone", sorry about that comment.

I don't like this:

Relay Control Current: 20-30 mA

But after I saw the picture, I say that the wire white must be connected to "GND" and not to "AREF". forget.

And if you connect the yellow wire to 5V too it work?

yes the white wire is connected to GND. so why do you think its freezing on me?

See my last answer, I edit it. Can you please post your code too? (use the code tags in your toolbar with the icon ' # ')

I have the relay connected to 5v pin digital 6 pin and the digital GND. Here’s my code its really long cause I’m also controlling two LEDs and it’s being controlled with bitvoicer, it should near the top of the code named RELAY1:

//Includes the BitVoicer library to the sketch
#include <BitVoicer11.h>

#define RELAY1  6
//Sets up the pins and default variables
int pinR = 3;
int pinG = 5;
int blinkDelay = 250;
int sequenceDir = 0;
int lightLevel = 0;

//Creates a new instance of the BitVoicerSerial class
//Sets up serial port to 0
BitVoicerSerial bvSerial = BitVoicerSerial();

void setup()
{
  //Starts serial communication and sets up the pinModes
  pinMode(RELAY1, OUTPUT);
  Serial.begin(9600);
  pinMode(pinR, OUTPUT);
  pinMode(pinG, OUTPUT);
}

void loop()
{
  //Retrieves data from serial buffer 
  bvSerial.getData();
 
  //Quits the loop if no string data was returned from getData
  if (bvSerial.strData == "")
  {
    return;
  }
 
  //Each of the next 'if' statements performs a different
  //task based on the data received from BitVoicer
  if (bvSerial.strData == "wake")
  {
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    digitalWrite(pinR, HIGH);
    digitalWrite(pinG, HIGH);
    delay(200);
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    delay(200);
    digitalWrite(pinR, HIGH);
    digitalWrite(pinG, HIGH);
    delay(200);
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    delay(200);
    digitalWrite(pinR, HIGH);
    digitalWrite(pinG, HIGH);
    delay(200);
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    bvSerial.strData = "";
    lightLevel = 0;
  }
  else if (bvSerial.strData == "sleep")
  {
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    digitalWrite(pinR, HIGH);
    digitalWrite(pinG, HIGH);
    delay(200);
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    delay(200);
    digitalWrite(pinR, HIGH);
    digitalWrite(pinG, HIGH);
    delay(200);
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    bvSerial.strData = "";
    lightLevel = 0;
  }
  else if (bvSerial.strData == "cdon")
  {
       digitalWrite(RELAY1,LOW);  
       bvSerial.strData = "";
  }
    else if (bvSerial.strData == "cdoff")
  {
       digitalWrite(RELAY1,HIGH);  
       bvSerial.strData = "";
  }
  else if (bvSerial.strData == "RH")
  {
    digitalWrite(pinR, HIGH);
    bvSerial.strData = "";
    lightLevel = 255;
  }
  else if (bvSerial.strData == "RL")
  {
    digitalWrite(pinR, LOW);
    bvSerial.strData = "";
    lightLevel = 0;
  }
  else if (bvSerial.strData == "GH")
  {
    digitalWrite(pinG, HIGH);
    bvSerial.strData = "";
    lightLevel = 255;
  }
  else if (bvSerial.strData == "GL")
  {
    digitalWrite(pinG, LOW);
    bvSerial.strData = "";
    lightLevel = 0;
  }
  else if (bvSerial.strData == "blink")
  {
    digitalWrite(pinR, HIGH);
    digitalWrite(pinG, HIGH);
    delay(blinkDelay);
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    delay(blinkDelay);
    lightLevel = 255;
  }
  else if (bvSerial.strData == "BF")
  {
    blinkDelay = 100;
    bvSerial.strData = "blink";
    lightLevel = 255;
  }
  else if (bvSerial.strData == "BFF")
  {
    switch (blinkDelay)
    {
      case 500:
        blinkDelay = 250;
        break;
      case 250:
        blinkDelay = 100;
        break;
      default:
        break;
    }
    bvSerial.strData = "blink";
    lightLevel = 255;
  }
  else if (bvSerial.strData == "BS")
  {
    blinkDelay = 500;
    bvSerial.strData = "blink";
    lightLevel = 255;
  }
  else if (bvSerial.strData == "BSS")
  {
    switch (blinkDelay)
    {
      case 100:
        blinkDelay = 250;
        break;
      case 250:
        blinkDelay = 500;
        break;
      default:
        break;
    }
    bvSerial.strData = "blink";
    lightLevel = 255;
  }
  else if (bvSerial.strData == "sequence")
  {
    if (sequenceDir == 0)
    {
      digitalWrite(pinR, HIGH);
      delay(250);
      digitalWrite(pinR, LOW);
      delay(250);
      digitalWrite(pinG, HIGH);
      delay(250);
      digitalWrite(pinG, LOW);
    }
    else
    {
      digitalWrite(pinG, HIGH);
      delay(250);
      digitalWrite(pinG, LOW);
      delay(250);
      digitalWrite(pinR, HIGH);
      delay(250);
      digitalWrite(pinR, LOW);
    }
    lightLevel = 255;
  }
  else if (bvSerial.strData == "revert")
  {
    if (sequenceDir == 0)
    {
      sequenceDir = 1;
    }
    else
    {
      sequenceDir = 0;
    }
    bvSerial.strData = "sequence";
    lightLevel = 255;
  }
  else if (bvSerial.strData == "ALLON")
  {
    digitalWrite(pinR, HIGH);
    digitalWrite(pinG, HIGH);
    bvSerial.strData = "";
    lightLevel = 255;
  }
  else if (bvSerial.strData == "ALLOFF")
  {
    digitalWrite(pinR, LOW);
    digitalWrite(pinG, LOW);
    bvSerial.strData = "";
    lightLevel = 0;
  }
  else if (bvSerial.strData == "brighter")
  {
    if (lightLevel < 255)
    {
      lightLevel += 85;
      analogWrite(pinR, lightLevel);
      analogWrite(pinG, lightLevel);
    }
    bvSerial.strData = "";
  }
  else if (bvSerial.strData == "darker")
  {
    if (lightLevel > 0)
    {
      lightLevel -= 85;
      analogWrite(pinR, lightLevel);
      analogWrite(pinG, lightLevel);
    }
    bvSerial.strData = "";
  }
  else
  {
    Serial.println("ERROR:" + bvSerial.strData);
    bvSerial.strData = "";
  }
}

You seem to be powering the relay module from the Arduino's 5V line. Do you know how much current the relay module takes to operate? How is the Arduino itself powered?

The Relay takes 3.3 - 5.0V to operate I'm powering my arduino through usb.

A simple suggestion to just test the relay module. Move the output pin wire to pin 13 and load the example blink sketch. Relay should click on and off at the same rate as the pin 13 led. If that works then you know you have a sketch problem, if not you know you have a wiring problem. Let us know.

whitewolf578: I have the relay directly connected to my board and it seems that it can't handle it when there electricity going through the relay module, it just freezes the program running it so I'm going to connect to my bread board but I'm wondering how that's done. Thanks in advanced.

And you have this BIG program?! I think it can be anything! Try to do a smaller program to only turn on and off the relay and then, I think, that we may get some conclusions.

i have already ran smaller programs where the relay blinks on and off that works but i tried one where it sends a serial message and that froze 5-10 seconds after i plugged the power in to the outlet. this ones the same thing.

whitewolf578: i have already ran smaller programs where the relay blinks on and off that works but i tried one where it sends a serial message and that froze 5-10 seconds after i plugged the power in to the outlet. this ones the same thing.

If one sketch can control relay OK and others cannot then one would suspect problems with the other sketches, no?

whitewolf578: i have already ran smaller programs where the relay blinks on and off that works but i tried one where it sends a serial message and that froze 5-10 seconds after i plugged the power in to the outlet. this ones the same thing.

And you was waiting for what to say that? Then the problem is not the connection to the module. I think that can be noise in the serial line or something like that. You have something connected to the relays?

OK I have a relay module that is connected to my computer and it works fine with the program i have until I plug in the thing thats being controlled by the relay module.

until I plug in the thing thats being controlled by the relay module

What is that thing?

or more importantly, How is that thing wired into the relay?

the optocoupler should isolate your arduino's output pin from the relay. So having the relay make/break a live circuit should not affect your arduino... unless you have improperly wired something (e.g. powering that 'thing' with the arduino)

The thing is CD player(I didn't have any old lamps laying around) I have the wire of the CD player connected to the normally Open connection and the other part of the wire, the part that goes directly to the output goes to the common connection on the relay module.