# Tachometer calculation

Discussion in 'Embedded Systems and Microcontrollers' started by KansaiRobot, Sep 2, 2015.

1. ### KansaiRobot Thread Starter Active Member

Jan 15, 2010
318
5
Hello and thanks always

I am building a tachometer based on a series of pulses using a PIC18F2550
I am thinking how to calculate this and so far I have this reasoning.

If the period of a pulse is period and a revolution is completed in say 6 pulses, the period of a revolution is period*6

This period in minutes would be (period*6)/60

Now one the RPM would be RPM=1/((period*6)/60) or 60/(period*6)

Is my calculation correct???

However I found this tutorial
http://www.pyroelectro.com/tutorials/digital_tachometer_rpm/theory.html

and here they calculate the RPM as

RPM=60*(1/Fosc*4)/(Period*6)

(I replaced the 7 for 6 since that is physical)

Now, why would they do this? Does it have to do with the "Period" not being a period in seconds but a value of a Timer register??
Is their reasoning correct? (I suspect their Fosc*4 must be Fosc/4 but I am not sure)

Any help greatly appreciated

2. ### Dodgydave AAC Fanatic!

Jun 22, 2012
5,143
768
If its for a car, i would count the number of pulses in a second using the tmr1 input to clk, and then multiply by 30 for a four cylinder car , or 20 for a six cylinder car, 15 for an eight cylinder car.

Jul 18, 2013
10,833
2,502
Here is an example math of a tach using a 32Khz Xtal for TMR1, the CCP capture could also be used.
Max.

• ###### 8513_tach.pdf
File size:
1 MB
Views:
7
KansaiRobot likes this.
4. ### KansaiRobot Thread Starter Active Member

Jan 15, 2010
318
5
I am sorry, I read the article and understood nil

Ok, i get that RPM= 60/P.. (which is basically what I wrote above). I also understand that since timer1 will give a binary number, this has to be converted into actual seconds (or fractions of that).

however the article goes into "Because the resolution is 0.00025s , we have to make three right shifts to get 0.25 seconds (why 0.25 seconds?) so our new equation is 60000/T"...
what???

-----------------------------
I definitely don't understand tha article explanation.

I made my own calculations and arrive that

RPM= (60*(Fosc/4))/Counter

which yes gives 60000/Counter but for completely different reasons to what is given in the article...

Last edited: Sep 7, 2015

Jul 18, 2013
10,833
2,502
The one shown uses a 32Khz clock crystal for Timr1.
Max.

6. ### jayanthd Member

Jul 4, 2015
275
29
Hi KansaiRobot

Which pin are you using to count the pulses ? INT0 or T0CKI /or T1CKI ? If your frequency is low then it is better to use RB0/INT0 external interrupt to detect the RPM.

If T1CKI pin is used then all you have to do is increment a variable like overflow once a 16 bit/Timer/counter interrupt occurs.

Your system gives 6 pulses per revolution - Data given by you.

If your counter variable holds 2 then it means 2 Timer overflows has occured in 1 sec.

So, you have to do the maths like this.

frequency = (counter * 65536) + (TMRiH << 8) + TMR1L (assuming 1:1 prescalar is used for the counter timer).

then you have to divide this by 6

and then you have to multiply this by 60 to get the RPM.

If you want I can write a code for you using INT0 / T1CKI pin using mikroC PRO PIC Compiler.

7. ### KansaiRobot Thread Starter Active Member

Jan 15, 2010
318
5

I am using the capture module of CCP1 for this.
Any code you could show me is of course very welcomed

8. ### KansaiRobot Thread Starter Active Member

Jan 15, 2010
318
5
I made my calculations, put it in code and run it. The results seemed awfully high!

Then I re-read the datasheet of the motor and it seems they are as specified, which seemed weird since the motor seemed to revolve much slower

then I found out this is a "geared" motor, ergo there are gears that make the external appearance of the motor to be much slower than the actual revolutions..

Well, I am going to tweek a little more with the code, to correct some things.. (like for example, in the extreme case it is not moving, the current program keeps adding values to the timer counter (since I use timer overflow for that) which yes makes it RPM very low (it must be 0!) but I dont think this is the best approach.

Later I will try to put all of this on a board... which is so difficult for me.. so far for reasons unknown the board I built doesnt move the motor. wish me luck

9. ### jayanthd Member

Jul 4, 2015
275
29
Have you seen the RPM counter example C Code in the book "Embedded C and the Microchip PIC". It is a CCS C Code but I can translate it to your Compiler. You have to mention which PIC and Compiler and what frequency crystal you are using.

10. ### KansaiRobot Thread Starter Active Member

Jan 15, 2010
318
5
I am using a PIC18F2550 with the XC8 compiler. I am not using a crystal but the internal oscillator. I have also set the OSCCON to 0x62 (no special reason) to get a 4MHz signal.

Thank you for the reference of the book too

11. ### jayanthd Member

Jul 4, 2015
275
29
If you can get your hands on that book then your problem is solved. I have that book at home and if you want me to post the CCS C Code for RPM measurement using CCP1 pin which is in that book then I will do that when I get back home from Office.

KansaiRobot likes this.

Jan 15, 2010
318
5

13. ### KansaiRobot Thread Starter Active Member

Jan 15, 2010
318
5
Well, I implemented the code using the capture mode CCP1 and interruptions.
It turns out well. I realize that I don't need to account for overflows since that would only occur if the motor is really slow.

And I built the board and it is running nicely.

Then I found you can totally do the same by using the Timer0 as a counter, and count the ticks in one second and multiplying it by 60!
So simple!!! no interruptions needed either...
sigh.. I hope I can get some time to program this before I move a new project I think is coming

Thank you guys for your help always! I learn a lot

EDIT: One more annotation.
I build the board to support both programs. Of course I dont use all the connected pins each time. But I learned that if there is a pin that I dont use, and is conected to a input, I have to set it as input (TRIS=1) if I dont want it to interfere with the input signal going to another pin and messing the whole program. Once I put TRIS=1, it doesnt interfere with the signal.

Last edited: Sep 10, 2015