UNO: Frequency Counter "Jumpy"

Hardware: Arduino UNO R3

Issue: Serial Monitor output shows unstable counts/ random spikes.

Basic setup: UNO > Input pin 5, Analog ground. 5 volt square wave input (clean edge).
Also tested with transistor circuit (squares up wave).

Serial Monitor Output (example):
338
337
338
338
1211
338
337
679

Troubleshooting:
Tried another UNO R3. Checked signal with o-scope. Swapped jumper leads.
Tried different code.

Setup works with MEGA 2560, perfectly.

CODE SAMPE:

#include <FreqCount.h>

unsigned long count;

void setup(void){
 Serial.begin(500000);
 FreqCount.begin(1000); 
}
 
// main loop
void loop(){
  
  if(FreqCount.available()){
    count = FreqCount.read();
  }
  Serial.println(count);  
}

Secondary Code:

#include <FreqCounter.h>
#include <stdlib.h>

#define countdelay 1 // measurement delay
#define calibration 0 // adjusts frequency for variation in Arduino
#define gatetime 100 // gate time in milliseconds
#define onems 1000 // alias for 1000 milliseconds


void setup(){
  Serial.begin(500000);
}

void loop(){
  char Freq[16];  //to store frequency
  unsigned long Fcalc=0;
  float Fval;
  FreqCounter::f_comp=calibration; // calibrate with known source
  FreqCounter::start(gatetime); // count pulses for specified gating period
  while (FreqCounter::f_ready == 0)
    Fcalc = FreqCounter::f_freq;
    delay(countdelay);
    Fval = (float) Fcalc * (onems / gatetime); // scale the input
    
    //check and display in frequency correct units
    if(0 <= Fval && Fval < 1000){
      dtostrf(Fval, 3, 2, Freq);
      Serial.println(Fval);
    
    }
                else if(1000 <= Fval && Fval < 1000000){
      Fval = Fval/1000;
                        dtostrf(Fval, 3, 2, Freq);
                        Serial.println(Freq);
    }
    else if(1000000 <= Fval){
      Fval = Fval/1000000;
                        dtostrf(Fval, 3, 2, Freq);
                        Serial.println(Freq); 
    }
    else{
      // do nothing
    }
}

Any ideas why this output is jumping? I've tried low frequencies and higher ranges. No luck. The scope shows a steady, clean signal.

Thank you in advance.

FreqCount does not work well below 1kHz.
Try using the freqMeasure library
Also, use a slower serial baud like 115200.
I don't see a ground connection between the little transistor board and the UNO

1 Like

Interesting, thank you.

I'll give it a shot!

(p.s. the transistor circuit is not hooked up in that image.)

When asking for help, please always include all the relevant, correct, and complete information. Otherwise it could either make us giving you wrong advices, or fail to solve your issue, or even make us waste our time with dozens of posts.
Thank you.

1 Like

Basic setup: UNO > Input pin 5, Analog ground. 5 volt square wave input (clean edge).
Also tested with transistor circuit (squares up wave).

:sunglasses:

I downloaded the latest files from GitHub:

Using the UNO shows a blank Serial Monitor.

If I sweep the frequency input, the Serial Monitor will sometimes read a few lines of values,
then stop/freeze.

My first question is: do i have the correct library? This tends to mention only Teensy products.

Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.