Measuring Frequencies?

How can I measure frequencies? Can I use pulseIn function? ( Not preferred to use Freq Counter http://interface.khm.de/index.php/lab/experiments/arduino-frequency-counter-library/ ), it will affect the PWM pin and is only available on pin 5, PWM pin )

How can I measure frequencies?

Yes.

Can I use pulseIn function?

You have our permission.

What range of frequencies do you want to measure? For what purpose? Is the signal well behaved, or does it vary in frequency?

maybe controlling something wirelessly.

it will be 1 Mhz+-

Can I have any examples?

You would need to use the FreqCounter library (and pin 5) to test whether you can get good data at the frequency in question. How will you know?

When you know you can, you can look at alternatives. One of them might be to look at the FreqCounter library, and see why it uses pin 5.

I don't think that you are going to find 1GHz easy to read.

Just about 1 MHz +-

Just about 1 MHz +-

If you quoted the question you are replying to, it would make it easier to understand you.

If this is not in response to a question, and I don't see one for which this is a reasonably coherent answer, then including a noun and a verb in the sentence would make it easier to understand.

What is the signal look like, square wave or a sine, saw tooth ...

Alternative to the feq library might be using SW interrupts, but 1Mhz is quite difficult to read for Arduino in SW.

See - http://www.arduino.cc/en/Reference/AttachInterrupt -

This code is almost the simplest/minimal freq counter in SW I can imagine, give it a try.

volatile uint32_t count = 0;
uint32_t last = 0;

void setup()
{
  Serial.begin(115200);
  attachInterrupt(0, IRQ, RISING);
}

void loop()
{
  if (millis() - last) >= 1000)
  {
    Serial.println(count);
    last = millis();
    count = 0;
  }
}

void IRQ()
{
  count++;
}

I don’t think uou won’t be able to increment a long in 1us!! You might not be able to increment a byte in an ISR and have it return in that time - the only feasible way is to use a counter input pin (The datasheet for 328 says pin 5 is timer1’s external input pin, pin 4 is timer0’s external input pin.

If you set timer0 to be a counter then mills()/micros()/delay()/delayMicroseconds() will stop working…

Alternatively external counter chip might be needed. I’ve used an SN74LV8154N before for making a frequency counter, its two 16bit counters than can be chained together to counter up to 2^32 and clocks upto 40MHz or something like that. Needs a lot of pins to interface to as its has bytewide parallel out - there are probably better chips (not necessarily in DIP format though)

volatile uint32_t count = 0;
uint32_t last = 0;

void setup() {   Serial.begin(115200);   attachInterrupt(0, IRQ, RISING); }

void loop() {   if (millis() - last) >= 1000)   {     Serial.println(count);     last = millis();     count = 0;   } }

void IRQ() {   count++; }

Which pin is the input? Is it digital pin 2 ( on Uno )?

What is an Interrupt ( attach and detach )? Will it affect that pin in any way?

Which pin is the input? Is it digital pin 2 ( on Uno )?

What is an Interrupt ( attach and detach )? Will it affect that pin in any way?

The link I send with the code clarifies all, please read it..... Spending time in the reference and tutorial section is the fastest way to learn a lot about Arduino.

OK that said: YES, digital pin 2 == interrupt 0 on the UNO. => pin3 is interrupt 1.

An interrupt is a small piece of code that will interrupt running code to respond asap on an (external) signal. After the interrupt routine is done the normal program will continue. An interrupt routine must be as small as possible, as it must be ready for the next signal. Arduino interrupt-routines cannot interrupt themselves, this is called reentrancy, one of the well known causes for faulty IRQ-routines. In short: keep IRQ's short. Global variables used in IRQ's must be declared volatile to prevent the compiler from optimizing them. Effectively this makes changes by IRQs directly visible in the main program (see van count in code above)

attachInterrupt is the procedure to connect a software routine (the IRQ routine) to the external signal. detachInterrupt disconnects this.

Makes sense? Rob

Is it necessary to detachInterrupt while you can just upload another sketch?

Will Interrupt affect pin 2 ?

Is it necessary to detachInterrupt while you can just upload another sketch?

No, all code is overruled when uploading new sketches.

Will Interrupt affect pin 2 ?

As said twice(!) before , read - http://www.arduino.cc/en/Reference/AttachInterrupt -

Why didn't you read that page?

volatile uint32_t count = 0;
uint32_t last = 0;

void setup() {   Serial.begin(115200);   attachInterrupt(0, IRQ, RISING); }

void loop() {   if (millis() - last) >= 1000)   {     Serial.println(count);     last = millis();     count = 0;   } }

void IRQ() {   count++; }

How come the code you provide is not working?

It returns weird characters.

Serial.begin(115200);

Is your monitor set to 115200 baud rate?

Mark

Thanks! now it works