Control Relay

Hello everyone!

I'm trying to control a Relay over the protocol Z-Wave. So far, I manage to control the embed LED on the Z-Uno board.

So I edited my code in order to control my relay (SRD-05VDC):

// pin number, where relay is connected
#define RELAY_PIN 14
#define LED_PIN 13

// variable to store current relay state
byte lastSetValue;
byte currenLEDValue;

ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_BINARY(getter, setter));

void setup() {
  pinMode(RELAY_PIN, OUTPUT); // set up relay pin as output
  pinMode(LED_PIN, OUTPUT); // set up led pin as output
}

void loop() {
  // loop is empty, because all the control comes over the Z-Wave
}

byte getter() {
  return lastSetValue;
}

void setter(byte newValue) {
  // newValue is a variable, holding a "value"
  // which came from the controller or other Z-Wave device
  if (newValue > 0) { // if greater then zero
    digitalWrite(RELAY_PIN, HIGH); //turn relay on
    digitalWrite(LED_PIN, HIGH); // turn LED on
  } else {            // if equals zero
    digitalWrite(RELAY_PIN, LOW);  //turn relay off
    digitalWrite(LED_PIN, LOW); // trun LED off
  }

  // save the new value in a variable
  lastSetValue = newValue;
  currenLEDValue = newValue;
}

Currently, my led is turning on/off when i'm asking it to do so. However, the pin connected to the relay (14), which is supposed to behave the same way, is not working. It seems to staying at 5v.

I tried to control my relay with the ground/5v power pin of my Z-Uno and it worked perfectly. I also tried to connect my relay directly on the LED pin (13), nothing worked.

I'm really lost, I don't know what to do. I can't figure out why the pins 13 and 14 are behaving differently as the LED it is supposed to be connected to...

Please HELP ME...

  1. What kind of relay is that? Datasheet or link would be helpful.
  2. Could you post a schematic of your wiring the relay and the Arduino

-> There are 5V relay modules especially designed for use with Arduinos or any other MCU which is working with +5V I/O. These come with a transistor (and a protection diode parallel to the relay) and draw the current from the +5V power pin, but being controlled via an Arduino I/O pin.

I suspect, that your relay is not of that kind and draws too much current (40mA is the absolute maximum of an Arduino I/O pin! better: stay around 20mA not to kill your Arduino).

And: you will need a fly back diode antiparallel to the relay if you don't want to kill your Arduino by back EMF.

So, if you don't want to wait for that relay module, you will need at least: - a transistor (which is capable to deliver the required relay current) - a base resistor between Arduino pin and transistor - a fly back diode to protect the circuit