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).
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 ?
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?
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?
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.