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.