Go Down

Topic: Relay not activating every time (Read 1 time) previous topic - next topic


I might be missing a resistor or something, but with the following code and prototype layout, is there something I'm missing? The logic seems to work out ok in the code, the relay 'led' lights, but the relay doesn't actuate every time the PIR goes HIGH. Sometimes it does, sometimes it doesn't. I don't get it. :(

Code: [Select]
int PIR = 2;                   // arduino board pin for PIR sensor
int RELAY = 7;                   // arduino board pin for RELAY
int LED = 13;                    // arduino board pin for the LED light
int val = LOW;                      // variable for reading the pin status
int toggle = 1;                     // variable to track PIR activation

void setup() {
  pinMode(PIR, INPUT);         // declare sensor as input
  pinMode(RELAY, OUTPUT);        // declare relay as output
  pinMode(LED, OUTPUT);          // declare LED as output
  digitalWrite(RELAY, HIGH);

void loop(){
  val = digitalRead(PIR);                 // read input value
  if ((val == HIGH) && (toggle == 1)) {        // check PIR input is HIGH
    digitalWrite(LED, HIGH);                // turn LED ON
    digitalWrite(RELAY, LOW);            //activate relay
    digitalWrite(LED, LOW);                 // turn LED OFF
    toggle = 0;
  } else if ((val == HIGH) && (toggle == 0)) {
    digitalWrite(LED, HIGH);                // turn LED ON
    digitalWrite(RELAY, HIGH);           //activate relay
    digitalWrite(LED, LOW);                 // turn LED OFF
    toggle = 1;

Runaway Pancake

What is the coil resistance of that relay?
If that's less than 150? then you don't want to run it directly from an Arduino output, it needs the help of a transistor.
Does the fritzing diagram indicate a "flyback diode" across the coil, too?
(There are solid examples for using relays and transistors at the Arduino Playground.)
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"


Not sure of the specifics, but this is what I'm using.


The specs say it uses 15-20mA driver current. I believe the Arduino outputs that much. But the way I have it wired, I think the voltage is split in parallel and getting reduced??


I don't see a resistor on the led either, that could be drawing the power supply down enough to make the relay unreliable. You  are probably frying your Arduino outputs. Might slow things down too, I know I can't see a led change state in a few microseconds.
Einstein once said you don't really understand anything until you can explain it to your Grandmother


Veeeeery confusing colour scheme on the wiring there.... would be simpler if you stuck with the red black yellow idea from the PIR  8)

Here's a good tutorial on driving a relay. And seeing as you have the LED on pin 13, why not lose it anyway since it will be using the built in one.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Go Up