| ||
Shopping Cart
![]() |
Home | Products | Teensy | Blog | Forum |
You are here:
Teensy
![]() ![]() ![]() |
|
FreqCount LibraryFreqCount measures the frequency of a signal by counting the number of pulses during a fixed time. See FreqCount vs FreqMeasure below to choose the best library.
Update: FreqCountMany can measure up to 10 frequencies simultaneously on Teensy 4.0, 4.1. See this forum conversation for details.
Hardware RequirementsFreqCount requires the input frequency as a digital level signal on a specific pin.
The Teensy and Teensy++ boards have a LED connected to the input pin. If the input signal is unable to drive the LED, a buffer must be used. The 74HC14 is a good choice.
An amplifier may be needed if the input signal is a sine wave or small AC signal which can not directly drive a TTL logic level input. Basic UsageFreqCount.begin(GateInterval);Begin frequency counting. GateInterval is the time in milliseconds for each measurement. Using 1000 provides direct frequency output without mulitplying or dividing by a scale factor. FreqCount.available();Returns true when a new measurement is available. Only a single measurement is buffered, so it must be read before the next gating interval. FreqCount.read();Returns the most recent measurement, an unsigned long containing the number of rising edges seen within the gating interval. There is no delay between gating intervals, so for example a 1000.5 Hz input (and perfectly accurate crystal osciallator) will alternate between 1000 and 1001 when used with a 1 second gate interval. FreqCount.end();Stop frequency counting. PWM (analogWrite) functionality may be used again. Example ProgramOpen from the menu: File > Examples > FreqCount > Serial_Output#include <FreqCount.h> void setup() { Serial.begin(57600); FreqCount.begin(1000); } void loop() { if (FreqCount.available()) { unsigned long count = FreqCount.read(); Serial.println(count); } }
Interrupt Latency RequirementsFreqCount uses a timer interrupt for the gate interval. If another interrupt is running, or interrupts are disabled by the main program, response to the timer could be delayed. That will lengthen the gate interval, perhaps leading to counting more cycles. The next gate interval will be shorter (if normal interrupt response occurs), so a corresponding decrease will occur in the next measurement.During USB startup on Teensy, activity from the PC can cause interrupts which interfere with FreqCount's gate interval. Other libraries which disable interrupts for long times, such as NewSoftSerial, can cause trouble. FreqCount vs FreqMeasureFreqCount: best for 1 kHz to 8 MHz (up to 65 MHz with Teensy 3.0 - 3.6)FreqMeasure: best for 0.1 Hz to 1 kHz
![]() FreqCount measures the number of cycles that occur during a fixed "gate interval" time. This works well for relatively high frequencies, because many cycles are likely to be counted during gate interval. At lower frequencies, very few cycles are counted, giving limited resolution. FreqMeasure measures the elapsed time during a single cycle. This works well for relatively low frequencies, because a substantial time elapses. At higher frequencies, the short time can only be measured at the processor's clock speed, which results in limited resolution. Other Frequency MeasurementFreqCount development has moved to this GitHub repository:https://github.com/PaulStoffregen/FreqCount Martin Nawrath's FreqCounter and FreqPeriod are similar to FreqCount and FreqMeasure. I originally ported FreqCounter to Teensy, but could not get it to work reliably. It did work, but had trouble at certain frequencies, and requires a "compensation" factor for accurate results. Ultimately I wrote a FreqCount from scratch, using a thin hardware abstraction layer for easy porting to different boards, and accurate results without a compensation factor. I also used a more Arduino-like API (begin, available, read) and designed for continuously repeating measurements. I did not try to use FreqPeriod. |