Hey y'all, I have been trying to mess with this code for a while now. What I am trying to do is measure hertz from a 5v square wave source. The source for this is the tachometer signal on my car. I have stepped it down from 12v to 5v using a 2N4401 NPN transistor. Everything online is telling me this code should accurately measure hertz, but what I am getting on my serial monitor is way off. At around 1000 rpm, I am getting between 34 and 36 hertz which the rpm would actually be between 2000-2200 at that frequency.
#include <FreqMeasure.h>
void setup() {
Serial.begin(57600);
FreqMeasure.begin();
}
double sum=0;
int count=0;
void loop() {
if (FreqMeasure.available()) {
// average several reading together
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
float frequency = FreqMeasure.countToFrequency(sum / count);
Serial.println(frequency);
sum = 0;
count = 0;
}
}
}
Any help with figuring what's up with this code would be greatly appreciated! Thanks in advance!