Go Down

Topic: Arduino Frequency Measurement Help (Read 741 times) previous topic - next topic


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.

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

void setup() {

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);
      sum = 0;
      count = 0;

Any help with figuring what's up with this code would be greatly appreciated! Thanks in advance!


Jul 10, 2018, 06:39 am Last Edit: Jul 10, 2018, 06:42 am by yeti_runner
I feel like an idiot now lol. I stopped thinking for a second I guess.. My car is a Miata and it has a wasted spark system where the ignition fires every time the cylinder is at TDC. I'm going to try this. Hope it works out!
Code: [Select]
float frequency = FreqMeasure.countToFrequency((sum / count) / 2);

Or I could just try this and get RPM instead of hz... lol I'm fixing my own problems in some sleep-deprived state..
Code: [Select]
float frequency = FreqMeasure.countToFrequency((sum / count) * 20);


It sounds to me like you get two pulses per rotation, thus doubling the frequency measured.

By the way, how do you go from 12V to 5V using a transistor? It'd be MUCH safer to use an optocoupler, and isolate your Arduino from the car's electrics.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.


Jul 13, 2018, 11:28 pm Last Edit: Jul 13, 2018, 11:29 pm by hammy
There is probably a nice clean rpm signal available from the ECU , either directly or from the OBD2 port

Go Up