pulse acitvated relay

Hi

I want to activate a relay when a 1hz pulse is present.

Thought this was easy, but as noob I find a bit difficult to decide how to approach this.

I'm thinking that is could be done with a timer that runs out after 2 sec. If no pulse has been detected nothing happens.

But if it detects a pulse every 2 sec the relay will stay ON.

Is the the way to do it?

The relay should be on when there is a 1 Hz frequency detected on an Arduino pin, but off when the frequency is say 20 % higher or say 20% lower ?
You have to work out a way of measuring frequency by counting pulses in a unit time and, on that basis, switch a pin.

6v6gt:
The relay should be on when there is a 1 Hz frequency detected on an Arduino pin, but off when the frequency is say 20 % higher or say 20% lower ?

Yes, please be more specific so we don't waste time.

Have a look at this code snippet

btnVal = digitalRead(btnPin);
if (btnVal == HIGH) {    // assumes btn is LOW when pressed
   lastBtnPressMillis = millis();   // btn not pressed so reset clock
}
if (millis() - lastBtnPressMillis > = interval) {
   // button has been pressed for longer than interval
}

Think of the button as you input pulse. You may need to swap LOW for HIGH.

I think if you set the interval a bit longer than the usual period between pulses (say 1050 for a 1000msec period) the clock will always reset before the interval expires as long as the pulses keep coming.

...R

Michael_N:
I want to activate a relay when a 1hz pulse is present.

Would that be a 1 microsecond pulse every second?

Is the idle state high, low, or indeterminate?

Michael_N:
Is the the way to do it?

You maintain a time stamp variable. Continually check the input. Any time the input is active you set the variable to millis() time. Also continually compare the current millis() time with that variable. If the difference is more than 2000 ms, you switch the relay.

It's really that simple. Two if statements inside loop().

Unless you really do have microsecond long pulses, as mentioned above. Then you have to use interrupts.

I suppose the next question is what level of latency is acceptable ? If it is sufficiently high, then the duty cycle is irrelevant and need not be constant. If it is low, then the signal has to be correspondingly 'well behaved' for an accurate measurement.

Thanks for all the replies!:slight_smile:

The pulse will be present every 0.5 - 1 second. The pulse duration is about 300-500ms

It does not have to be super accurate by the way.

But as I understand I will have to use interrupts due to the "long" pulses.

I will try to works with interrupts to get a better understanding of these:)

Thanks again!

Michael_N:
But as I understand I will have to use interrupts due to the "long" pulses.

No you misunderstood. Exactly the opposite.

OK, so what you are looking at is called an edge re-triggerable monostable.

Can be implemented with about three capacitors, two diodes, two resistors and two gates of a 74HC14.

But you want a microprocessor, eh?

Oh well. So be it.

//  Edge re-triggerable monostable

const int led1Pin =  13;    // LED pin number
const int button1 =  10;     // buttons & switches always connect from input to ground
char led1State = LOW;       // initialise the LED
char bstate1 = 0;
unsigned long bcount1 = 0;  // button timer.

const long int timeout = 1200UL; // Interval after which we drop out

void setup() {
  pinMode(led1Pin, OUTPUT);
  pinMode(button1, INPUT);
  digitalWrite(button1, HIGH);          // internal pullup all versions
  bcount1 = millis();
}

void loop() {
  if (digitalRead(button1) == HIGH) {
    if (bstate1 == 0) {                 // Was low, now high?
      bstate1 = 1;
      bcount1 = millis();               // OK, reset the timer
    }
  } else {
    if (bstate1 == 1) {                 // Was high, now low?
      bstate1 = 0;
    }
  }
  if (millis() - bcount1 >= timeout) { // Timed out?
    if (led1State == HIGH) {
      led1State = LOW;
      digitalWrite(led1Pin, LOW);      // If so, turn off
    }
  } else {
    if (led1State == LOW) {
      led1State = HIGH;
      digitalWrite(led1Pin, HIGH);     // If not, turn on
    }
  }
}

Alternate version triggers on both edges:

//  Edge re-triggerable monostable

const int led1Pin =  13;    // LED pin number
const int button1 =  10;     // buttons & switches always connect from input to ground
char led1State = LOW;       // initialise the LED
char bstate1 = 0;
unsigned long bcount1 = 0;  // button timer.

const long int timeout = 600UL; // Interval after which we drop out

void setup() {
  pinMode(led1Pin, OUTPUT);
  pinMode(button1, INPUT);
  digitalWrite(button1, HIGH);          // internal pullup all versions
  bcount1 = millis();
}

void loop() {
  if (digitalRead(button1) == HIGH) {
    if (bstate1 == 0) {                 // Was low, now high?
      bstate1 = 1;
      bcount1 = millis();               // OK, reset the timer
    }
  } else {
    if (bstate1 == 1) {                 // Was high, now low?
      bstate1 = 0;
      bcount1 = millis();               // OK, reset the timer
    }
  }
  if (millis() - bcount1 >= timeout) { // Timed out?
    if (led1State == HIGH) {
      led1State = LOW;
      digitalWrite(led1Pin, LOW);      // If so, turn off
    }
  } else {
    if (led1State == LOW) {
      led1State = HIGH;
      digitalWrite(led1Pin, HIGH);     // If not, turn on
    }
  }
}

(Proves one thing - must test all code! Testing this requires exactly one pushbutton. :grinning: )

it really depends on what else you are doing in your code.
if you are only scanning for an alarm or some such, and then only trip when it is out of range, your scan times will be orders of magnitude faster than your pulse.

however, if you do a lot of things, there is a chance you can miss the event.
if your scan rate is 10 times faster than the shortest event, you are in great shape
if your scan rate is less than 4 times faster, you are close to missing it, any error and you might.

I think I would look at both a software and hardware solution.
you can charge a cap quickly and have it decay slowly.
watch the voltage with an ADC pin

did we get a clear statement about what should happen if the pulse turns into a constant high signal ?
the absence of a signal would drop out the relay.
the pulse would activate the relay
but what result should happen if there is a continuous high signal on the line ?

and for the purpose of the question, it does not matter, but just what generates the pulse ?

Michael_N:
But as I understand I will have to use interrupts due to the "long" pulses.

Sounds like you did not study Reply #3

...R