Tachometer class and some test code

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Tachometer class and some test code

Post by dhylands » Tue Apr 28, 2015 7:32 am

I implemented a Tachometer class which uses Input Capture.

It requires one of the 32-bit timers.

The test function assumes that X1 is jumpered to X2. THe very first reported RPM (60 RPM) is often incorrect due to not enough samples being collected. I'm planning on connecting an optical sensor or hall effect sensor up and an LCD and using this to measure the RPM of the spindle on my OX CNC Router (you can read more about my OX here: blog.davehylands.com/search/label/OX)

The tachometer code that I've coded so far can be found here:
https://github.com/dhylands/upy-example ... er/Tach.py

With a 16-bit counter, it would wrap every 64 msec, so the slowest pulse count you could detect would be 15 Hz. At 2 pulses/revolution, this corresponds to 7.5 revs/sec or 450 RPM. For higher RPMs you can get by with a 16-bit counter.

The basic algorithm keeps a number of samples and basically counts how long it took for the last NUM_SAMPLES to arrive. From this is can do a direct calculation of RPM. There are a few edge cases (where you query the RPM, but there are fewer than NUM_SAMPLES samples collected.

The code uses a circular buffer to record the last NUM_SAMPLES captures. The capture is basically a microsecond counter, and by collecting multiple samples, its essentially performing a low pass filter over the data.

Post Reply