Go Down

Topic: Reading frequency (Read 1 time) previous topic - next topic

plank

Hi,
This is my first arduino project so apologies if i'm missing something basic.
The project is going to read in two frequencies, then perform a calculation on the two frequencies to get another number.
I've found the freqmeasure library (http://www.pjrc.com/teensy/td_libs_FreqMeasure.html)  perfect for my needs, but the input can only be mapped to pin 8, so it only allows one input.I need two inputs for my project.  Is there someway of getting  the same input without pin 8 or is there a component i can buy to replicate pin 8.

Thanks

plank

Thanks for the reply.
I'll fool around with pulseIn tomorrow and see if i can get some results.

nitrolx

I have found this library; http://www.avdweb.nl/arduino/hardware-interfacing/frequency-period-counter.html
quite helpful for projects like this. You can use 2 inputs (using the 2 interrupt pins) with the code implemented either using the interrupts or polling the counter in the loop.

Works well, you can have it measure Hz directly from the library (in whole numbers) if you're measuring a relatively high frequency, or using a floating point variable and calculate the frequency from the period of pulses if you need a fractional Hz result (if you need to detect small changes in low frequencies).

My project measures the RPM of 2 different shafts, which are relatively low, so I use the fractional option.
Uses a bit more memory that way due to the floating point calculation so if you're reading higher frequencies, the built in counter.hertz() function is better.

Cheers,
Ryan.

nitrolx

Here is some example code using the above library to calculate 2 different frequencies (and convert to RPM). Using floating point and a fractional frequency.
This is just the frequency parts pulled out of a bigger sketch so this is untested stand alone.

Code: [Select]
#include <FreqPeriodCounter.h>

// PIN ASSIGNMENTS
const byte counter1Pin = 2;             
const byte counter1Interrupt = 0;
const byte counter2Pin = 3;
const byte counter2Interrupt = 1;

// RPM CALCULATIONS
int rpm1;
float hz1;
int rpm2;
float hz2;

FreqPeriodCounter counter1(counter1Pin, micros, 0);
FreqPeriodCounter counter2(counter2Pin, micros, 0);

void setup() {
  attachInterrupt(counter1Interrupt, counter1ISR, RISING);
  attachInterrupt(counter2Interrupt, counter2ISR, RISING);
}

void loop() {
  if(counter1.ready()) {
    hz1 = (1000000.0/counter1.period);   // period in ms
    rpm1 = (hz1 * 60);
  }
  if(counter2.ready()) {
    hz2 = (1000000.0/counter2.period);
    rpm2 = (hz2 * 60);
  }
}


void counter1ISR()
{
  counter1.poll();
}

void counter2ISR()
{
  counter2.poll();
}

AndyCC

#4
Jul 31, 2012, 08:42 pm Last Edit: Jul 31, 2012, 08:56 pm by AndyCC Reason: 1
Alternately, you might use a relay (SPDT or DPDT) to switch the line that's connected to pin 8. You'll need a transistor to drive it:

http://www.arduino.cc/playground/uploads/Main/relays.pdf

This assumes you can read one frequency and then read the other sometime after that - i.e., you don't need to read both at exactly the same time. And that you can wait for the relay to settle (a few hundred ms) before you read.

JavaMan

Quote
I'll fool around with pulseIn tomorrow and see if i can get some results.


Hi plank,
Here's an easy way to play around with pulseIn().  The sketch measures the Arduino PWM frequency and the length of the HIGH and LOW pulses for each PWM value.  Try it out - you only need one wire!
Tom

Code: [Select]
// PWM analysis, Tom Fangrow, July 8, 2012
// connect a wire from digital pin 2 to digital pin 3

long hi, lo, x;
 
void setup() {
  pinMode(3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  for(x=1; x<255; x++) {              // for all useful values
    analogWrite(3, x);                // output PWM on pin 3
    hi = pulseIn(2, HIGH);            // measure HIGH pulse
    lo = pulseIn(2, LOW);             // measure LOW pulse
    Serial.print("PWM: ");            // PWM value (1-254)
    Serial.print(x);                 
    Serial.print(", HI: ");           // signal at 5 volts
    Serial.print(hi);                 // for 'hi' microseconds
    Serial.print(", LO: ");           // signal at 0 volts
    Serial.print(lo);                 // for 'lo' microseconds
    Serial.print(", Freq: ");         // PWM frequency
    Serial.print(1000000/(hi + lo));
    Serial.println(" Hz");            // in pulses per second
  }
}

NOOB4101

Ok please forgive me, i am very noobish ... i actually need to ask a question but not sure how to start it as a topic like he did up there..


i need a simple program where i can read a frequency, no need to display it.
ill read it if its above 500hz an led light up,
and if its below 500hz another led from another pin light up.. of course i am not going to ask u guys to do this for me but i will try my self in the cod below... please forgive my obvious syntax error.
pseudo code-
#include <FreqMeasure.h>
int led1=13;
int led2=9;
void setup() {
FreqMeasure.begin();
PinMode(led1,OUTPUT);
PinMode(led2,OUTPUT);
}

if (FreqMeasure.available()< 500)
  DigitalWrite(led1,HIGH);
else
DigitalWrite(led2,HIGH);
end


will this work ?


elac

Syntax errors can be forgiven, not properly posting code, now that's a different story.  ;)
It's all about the skills

Paul__B


Syntax errors can be forgiven, not properly posting code, now that's a different story.  ;)


It's only a really little piece of code ...

XD

NOOB4101

hahaha i like how you guys didnt answer my question :( ,, so how can i fix that code.. its not finding
the frequency libraries or folder.

elac

Did you download and properly install the library?
It's all about the skills

PeterH


hahaha i like how you guys didnt answer my question :( ,, so how can i fix that code.


If you want help fixing a problem with your code you need to post your actual code - in its entirety - that demonstrates the problem. If the problem is a compilation error then you need to also post the actual output from the compiler.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up