Arduino tachometer not working

Hi!

I am trying to make a tachhometer with an NPN NO inductive sensor and send the data through Serial. Unfortunately, the most I could get with my code are about 6000-7000 RPM. After that the arduino just sends random numbers.
I am using a 19V power supply for the sensor and I power the arduino nano from my computer. Since the module output is also 19V, I use a voltage divider on 20k/68k resistors and connect the output to pin 2 (interrupt 0).

What could I do to get more RPM? Here is my code:

#include <Arduino.h>

unsigned long rpmtime;
float rpmfloat;
int rpm;
bool tooslow = 1;

void setup() {
 
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1B |= (1 << CS12); //Prescaler 256
  TIMSK1 |= (1 << TOIE1); //enable timer overflow
  pinMode(2, INPUT);
  attachInterrupt(0, RPM, FALLING);
  Serial.begin(250000);
}

ISR(TIMER1_OVF_vect) {
  tooslow = 1;
}

void loop() {
  delay(1000);

    rpmfloat = 120 / (rpmtime/ 31250.00);
    rpm = round(rpmfloat);

    Serial.println(rpm);

}

void RPM () {
  rpmtime = TCNT1;
  TCNT1 = 0;
  tooslow = 0;
}

How do you know the sensor works above 6000-7000RPM?

The datasheet says its frequecy is 500 Hz and 7000rpm is just 116,6 RPS, so it should work

Have you looked at the output signal on a scope?

whoops, looks like the signal on the scope is just like a line with occasional 0.5us drops. I will try to inclease the length of the metal that makes it detectable. If that doesnt work, what sensor should I use for a tachometer that has to count 20000+RPM ?

They work best with iron or steel and the closer the better.
I don't of any that goes to 20000+

maybe something optical or hall will go better?

rpmtime, and possibly tooslow, should be declared 'volatile'.

You should temporarily disable interrupts while accessing rpmtime outside of the ISR, otherwise an interrupt may occur between individual bytes. Since you are doing math with floats, disable interrupts, copy rpmtime to another variable, re-enable interrupts, then do the calculation using the copy of rpmtime. Float arithmetic takes a long time on an 8-bit processor.

Do you need an unsigned long to store the value from a 16-bit counter?

Optical would be good if it's in a clean enviroment. The halls maybe faster

1 Like

ok. that worked for me, the only issue I get now is that after 9000RPM the counter just freezes, it doesn't send any more data to Serial and I must reconnect it to the pc fot it to start working again. I don't think it's a power issue since the motor power is independent from the arduino power. Could it be something in code?

#include <Arduino.h>

volatile int rpmtime;
float rpmfloat;
int rpm;
bool tooslow = 1;

void setup() {
 
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1B |= (1 << CS12); //Prescaler 256
  TIMSK1 |= (1 << TOIE1); //enable timer overflow
  pinMode(2, INPUT);
  attachInterrupt(0, RPM, FALLING);
  Serial.begin(250000);
}

ISR(TIMER1_OVF_vect) {
  tooslow = 1;
}

void loop() {
  delay(1000);
noInterrupts();
    rpmfloat = 120 / (rpmtime/ 31250.00);
    rpm = round(rpmfloat);

    
interrupts();
Serial.println(rpm);
}

void RPM () {
  rpmtime = TCNT1;
  TCNT1 = 0;
  tooslow = 0;
}

This will work
0165-HF-BULLETIN.pdf (65.0 KB)

If you are uncertain about your code, then use a library that is known to work: https://www.pjrc.com/teensy/td_libs_FreqMeasure.html

Hi,
What is the target that you are using to activate the sensor?

How wide is it?
What is the diameter of the shaft it is on/

What I am trying to get at is the pulse width at 9000RPM in ms.

You may need a wider target to give a longer pulse.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

This one certainly doesn't work, the best I could measure is 20Hz

Hi! The shaft is 20mm in diameter and to detect it the target is an aluminum square with 5mm side.

Hi,
Thanks for the information.

Circumference = Pi x d = Pi x 20 = 62.83 mm

9000rpm = 9000 / 60 = 150 rps

One revolution = 1 /150 = 0.0066s

Pulse length = ( 5 / 62.83 ) x 0.0066 = 0.525ms.

So although the pulse is every 6.6ms the actual pulse is 0.535ms long.

Is that long enough to be detected?

Tom... :smiley: :+1: :coffee: :australia:

The FreqMeasure is very good and reliable for many years and does work for everyone else. The problem can not be the FreqMeasure library.
Something else must be going on.

Probably loosing the signal because of narrow pulse width and using a very weak voltage divider. Since the NPN outout of the sensor can handle 100mA, I would suggest using 6.8K/2.4K divider which gives 4.96V out when the source is 19V. This should allow for a much higher pulse frequency (RPM) from the sensor.

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