Measuring HZ

Dear all,

Hello, I have few tasks coming up... and I have no experience

with arduino. So this is the only place I could ask.

Here is the thing.

I am trying to make simple frequency detecting system with arduino UNO.

I've done coding & wiring for 'Sound detecting sensor' and it worked.
(When I make any noise, sensor detects it and LED turns on, also used serial.print for monitoring)

What I am trying now is to detect 'frequency' on certain Hz, have LEDs turn on.

For example, I need LED to turn on at 1hz~1500hz and 2500hz~5000hz.

So at 1501hz to 2499hz LED needs to be turned off.

Here is the question.

  1. Here is my coding for above

int sds = A0; // sds as in sound detecting sensor
int led = 6;
int sensor_value;

void setup() {
Serial.begin(9600);
pinMode(sds,INPUT);
pinMode(led, OUTPUT);
}
void loop(){
sensor_value = analogRead(A0);
Serial.println(sensor_value);
delay(10);

if(sensor_value < 1501){
digitalWrite(led, HIGH);
}

if(sensor_value > 2500){
digitalWrite(led, HIGH);
}
}

I know I have a lot to go.. and if anyone could tell me

what 's wrong with the coding, please by all mean..

I need your guidance.

  1. Now I am using Arduino UNO and sound sensor that only detects decibel.

I need to buy small frequency sensor for Arduino UNO that has range from 0 to 5000hz.

Over 5000hz is fine, but not below. Can anyone recommend me one?

I know my English isn't that great, but please understand that I am from Korea

and I don't speak that well. It'd be great if you could help me out.

Thank you so much in advance.

 sensor_value = analogRead(A0);
  Serial.println(sensor_value);
  delay(10);

  if(sensor_value < 1501){

Your code suggests you have an ADC with at least 12 bit resolution.

What is connected to pin A0?

Not easy... You will need two [u]bandpass filters[/u].

Notes:
Filters are not "perfect". Some frequencies near the cutoff frequencies will "leak through" so it depends on how much frequency-accuracy you need. It takes more than one filter stage (multiple op-amps) if you want a sharper cutoff.

To get the best performance from op-amps (in most applications) you need positive & negative power supplies (and for the 5V Arduino the op-amp power supplies should be higher than 5V). Then you may need a "protection circuit" in case the signal to the Arduino exceeds 5V. That's just a resistor and one or two diodes.

Digital filters are not perfect either, but a more-perfect, more-complex filter is just more code. I'm pretty sure the Arduino isn't fast-enough for real-time continuous-filtering, especially at 5kHz.

If you do use digital filtering (even if you choose a different/faster processor) you'll need a low-pass analog filter before digitizing to prevent [u]aliasing[/u].

The Arduino can't read negative voltages and audio is AC (swinging positive and negative). It can actually be damaged by negative voltages.

There are two ways to deal with the AC signal. The most common solution is to bias the filter-outputs (two resistors and a capacitor), then you can subtract-out the DC bias in software. Most "sound sensors" have biased outputs but the DC bias is zero-Hz so your bandpass filters will remove the bias and you'll have to add it back in.

Or you can use a peak detector (AKA envelope follower). That requires an op-amp, a diode, and a couple of other components. A basic peak detector "throws away" the negative half of the signal (which is almost identical to the positive half anyway) and it puts-out a varying DC voltage that's easier to read than an AC signal because you can read it more slowly (maybe 10 times per second or slower depending on your needs) whereas the AC waveform has to be sampled (read) thousands of times per second and at least 2 times per cycle (10,000 times per second for 5kHz audio to prevent aliasing).

You can make a passive peak detector without an op-amp but the signal has to be strong enough to overcome the diode voltage drop. Since real world sound waves vary a lot (from quiet to loud) that's probably not going to work for you.

...I'll leave it to you to research the bias (aka "DC offset") and peak detector circuits.

I know my English isn't that great, but please understand that I am from Korea

and I don't speak that well. It'd be great if you could help me out.

You are doing fine with your writing.

Dual supplies are completely unnecessary for opamps and do not increase performance, just make circuits a little simpler, that's all.

serenade46:
Dear all,

Hello, I have few tasks coming up... and I have no experience

with arduino. So this is the only place I could ask.

Here is the thing.

I am trying to make simple frequency detecting system with arduino UNO.

I've done coding & wiring for 'Sound detecting sensor' and it worked.
(When I make any noise, sensor detects it and LED turns on, also used serial.print for monitoring)

What I am trying now is to detect 'frequency' on certain Hz, have LEDs turn on.

For example, I need LED to turn on at 1hz~1500hz and 2500hz~5000hz.

So at 1501hz to 2499hz LED needs to be turned off.

Then you need some simply DSP to estimate the frequency. A fairly small FFT buffer should be able to discriminate like this.

Sampling at 10kHz or so (may be limited by the hardware here), into a short buffer and then running an FFT on it will derive the spectrum, go and search for the highest peak. 128 samples would give a frequency resolution of 80 Hz or so with 10k samples/second, upto 5kHz maximum.

TheMemberFormerlyKnownAsAWOL:

 sensor_value = analogRead(A0);

Serial.println(sensor_value);
  delay(10);

if(sensor_value < 1501){


Your code suggests you have an ADC with at least 12 bit resolution.

What is connected to pin A0?

Connected to analog sound sensor, which is connected to this product (Sound Sensor LM386 Arduino Compatible Philippines | Circuitrocks – circuitrocks).

This was done to just test out my coding.

DVDdoug:
Not easy... You will need two [u]bandpass filters[/u].

Notes:
Filters are not "perfect". Some frequencies near the cutoff frequencies will "leak through" so it depends on how much frequency-accuracy you need. It takes more than one filter stage (multiple op-amps) if you want a sharper cutoff.

To get the best performance from op-amps (in most applications) you need positive & negative power supplies (and for the 5V Arduino the op-amp power supplies should be higher than 5V). Then you may need a "protection circuit" in case the signal to the Arduino exceeds 5V. That's just a resistor and one or two diodes.

Digital filters are not perfect either, but a more-perfect, more-complex filter is just more code. I'm pretty sure the Arduino isn't fast-enough for real-time continuous-filtering, especially at 5kHz.

If you do use digital filtering (even if you choose a different/faster processor) you'll need a low-pass analog filter before digitizing to prevent [u]aliasing[/u].

The Arduino can't read negative voltages and audio is AC (swinging positive and negative). It can actually be damaged by negative voltages.

There are two ways to deal with the AC signal. The most common solution is to bias the filter-outputs (two resistors and a capacitor), then you can subtract-out the DC bias in software. Most "sound sensors" have biased outputs but the DC bias is zero-Hz so your bandpass filters will remove the bias and you'll have to add it back in.

Or you can use a peak detector (AKA envelope follower). That requires an op-amp, a diode, and a couple of other components. A basic peak detector "throws away" the negative half of the signal (which is almost identical to the positive half anyway) and it puts-out a varying DC voltage that's easier to read than an AC signal because you can read it more slowly (maybe 10 times per second or slower depending on your needs) whereas the AC waveform has to be sampled (read) thousands of times per second and at least 2 times per cycle (10,000 times per second for 5kHz audio to prevent aliasing).

You can make a passive peak detector without an op-amp but the signal has to be strong enough to overcome the diode voltage drop. Since real world sound waves vary a lot (from quiet to loud) that's probably not going to work for you.

...I'll leave it to you to research the bias (aka "DC offset") and peak detector circuits.
You are doing fine with your writing.

Thank you so much for your explanation.

I didn't even have any idea what band pass filter is, since I am completely new in this field.

I did little research based on your reply and somehow I think I will be able to complete my tasks.

once again, thank you :slight_smile: