Measuring Hz with Uno and activating relay/LED

New to Arduino, not so much electronics, but the C++ programming is a major barrier for me. I'm more experienced with web language, but catch on pretty quickly to new things :wink:

Looking for some help/guidance for a project I'd like to tackle with my new Uno (besides changing the duration of the Blink program, haha!)

I'm wanting to sample frequency from an AC source (Through a resistor, cap and optoisolator such as the 4N25) and then monitor that reading internally to the Uno. When the reading drops below a certain threshold, I'd like to trigger a relay to open for a duration of time and possibly incorporate some LED diagnostics along with it to ascertain what the program is currently doing in its cycle... I realize that this is not complete noob territory, but wanted to start out with a challenge and learn along the way!

Hopefully someone can point me in the right direction or offer a starting place for me. This thing is just blinking at me and begging to be turned into something... :o

Thanks to anyone willing to get involved!

Is your threshold amplitude (fairly easy) or are you actually thinking of measuring frequency and talking about a frequency threshold e.g. less than 100Hz (more difficult)?

Is your “AC source” sine wave, pulses or complex wave (like audio signals)?

Steve

slipstick:
Is your threshold amplitude (fairly easy) or are you actually thinking of measuring frequency and talking about a frequency threshold e.g. less than 100Hz (more difficult)?

Is your "AC source" sine wave, pulses or complex wave (like audio signals)?

Steve

Thanks Steve!

Sorry, I should have been more specific...

This will be a sine wave based on utility power so should be a fairly nice looking wave (mains AC) stepped down via a control transformer to a much lower voltage, most likely 24VAC. So yes, it will be a lower value, 60Hz and down to about 50Hz worst case scenario. I would like the samples to be quick enough to be able to trigger the relay within about 500ms of a threshold being reached.

I hope my explanation makes some better sense.

With an analog sine wave, you can simply find the time between successive positive-going (or negative going) zero-crossings. But, since the Arduino can be damaged by negative voltages you have to deal with that.

An opto-isolator will also “have trouble” passing a sine wave because they can’t go negative and are non-linear. But, an opto-isolator certainly can be used (along with the Arduino) to measure the frequency of the power line, or something like that.

Interrupts can be useful if you’re ready to deal with that…

In most countries the power-line frequency is as-good as the Arduino’s oscillator (much better over the long-term average) so the measurement won’t be of much real-world use. :wink:

DVDdoug:
With an analog sine wave, you can simply find the time between successive positive-going (or negative going) zero-crossings. But, since the Arduino can be damaged by negative voltages you have to deal with that.

An opto-isolator will also "have trouble" passing a sine wave because they can't go negative and are non-linear. But, an opto-isolator certainly can be used (along with the Arduino) to measure the frequency of the power line, or something like that.

Interrupts can be useful if you're ready to deal with that...

In most countries the power-line frequency is as-good as the Arduino's oscillator (much better over the long-term average) so the measurement won't be of much real-world use. :wink:

I'm not sure what I'm 'ready to deal with' haha!

To be honest, I know so very little about programming, I was hoping someone could point me in the direction of an example that I could play around with and see what my outcomes are. I really only need it to be accurate withing about .5Hz or so, nothing into the serious realm of accuracy. Basically if Hz falls to 'X' amount, trigger the relay output.

I really only need to measure the duration from peak to peak or zero crossing I would imagine? Basically if X number of samples on a pin happens withing X amount of time it would = Hz if I'm understanding correctly...

Basically if X number of samples on a pin happens withing X amount of time it would = Hz if I'm understanding correctly.

No. It is more like
Basically if X number of samples on a pin happens withing One second it would = X Hz

I'm not sure what I'm 'ready to deal with'

To deal with not killing yourselve or others or causing a fire or explosion. Playing with mains power with so little knowledge is not a good combination.

If you want a starting place google “power minitering Arduino” you should get some sample circuits.

Grumpy_Mike:
No. It is more like
Basically if X number of samples on a pin happens withing One second it would = X Hz
To deal with not killing yourselve or others or causing a fire or explosion. Playing with mains power with so little knowledge is not a good combination.

If you want a starting place google “power minitering Arduino” you should get some sample circuits.

Maybe I should add that I’m an electrician (14 years) by trade… My ‘little knowledge’ is of the Uno and it’s programming language more than the hardware itself. Mains power is the least of my worries with this project :wink: But I appreciate your honesty and concern very much! All testing will be done over a bed of very fine sawdust and paper shreddings for effect in the video, haha!

I’ve found this project on another site and it looks promising for what I am trying to do… I’m sure there’s a way to call to it’s result (the program’s) and make the outputs on the Uno do what I want. I will probably mess around with this a bit. Hz Project

Code appears simple enough (to my mind). This is made to print the results on screen or an LCD, but I’m sure it can be worked with

#include <FreqMeasure.h>

void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
}

double sum=0;
int count=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      float frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
}

Measuring mains frequency has been done many times before on this site.
Google something like "measure mains frequency site:arduino.cc".
Or use the search field on top of this page.
Leo..