# How do I time something with an arduino?

I am trying to find RPM of a motor. I was originally going to just count to 60 and check how many times it goes HIGH, but that does not work because it might go high multiple times per rotation by accident or even miss some. So, I am going to basically going to measure how long it takes for the motor to make on rotation and then find out the rpm from there. This is my code:

``````int num = 0;
int val = 0;
int start = 0;
int finish = 0;
int last = LOW;
int time = 0;

void setup() {
pinMode(52, INPUT);
Serial.begin(9600);
}

void loop() {

val = digitalRead(52);
if(val == HIGH && last == LOW){
last = HIGH;
start = millis();
}
if(val == HIGH && last == HIGH){
last = LOW;
finish = millis();
time = (start - finish) * 60000;
}
if(time > 0){
Serial.println(time);
delay(1000);
time = 0;
}
}
``````

It is not working, but I cant figure out why. If someone has another way of accomplishing this, let me know. By the way, I am using an opto interrupter.

``````  unsigned long before = micros () ;
do_something () ;
unsigned long after = micros () ;
Serial.print ("took ") ; Serial.print (after-before)  ; Serial.prinln ("us") ;
``````

I don't quite see what you are saying... could you please put that into the code I posted so I can better understand what you mean? Thanks!

measure how long it takes for the motor to make on rotation and then find out the rpm from there.

How do you know that you will see a HIGH digitalRead of pin 52 once each revolution? What is the rpm of the motor, and the time ratio of HIGH/LOW pin states during a revolution?

What if the opto interrupter is triggered during the delay(1000)?

because it might go high multiple times per rotation by accident or even miss some.

If you do not correct this situation, you are in the same place (and likely a worse one) trying to time one pulse, as you were when trying to count multiple pulses in a given period of time.

Depending on the speed of the motor, and the speed of your loop, this may be a case to use an interrupt rather than polling.

I see what you are saying. How do you suggest I go about doing this then? I was going to tie some string to the motor shaft and let it spin (A high being when the string passes through the opto, based on my wiring).

You need some device which is going to send you some kind of pulse at a rate which corresponds to the speed of the motor. This could be one pulse per revolution, or many. The important thing is that it is a fixed number, which you know.

You then need to count those pulses. Your method of checking the state of the pulse signal, in loop( ), is not a good method. You are probably better using an interrupt which detects the state change of the pulse signal, from high to low, or low to high, or both.

If you want to count the number of revolutions in an actual minute, you would store the time at a particularly starting point using the millis( ) function.

You would then use your interrupt code to count the pulses. You would also keep checking the time. When 60000 milliseconds have elapsed since you started counting, then the total number of pulses would be used to determine the number of revolutions in a minute.

You need to use hardware interrupts.

There is lots of code available to you if you Google "Arduino rpm" or Arduino tachometer". Here's one http://elimelecsarduinoprojects.blogspot.com/2013/06/measure-rpms-arduino.html

Your challenge is to come up with the correct hardware to generate the pulses. Many of these tachometers use Hall sensors, and other use optical interrupters like you mentioned. Usually they are used with a slotted disc on the shaft. Get your sensor working reliably, and determining the rpm will be easy. Depending on the speed of rotation, and the err you can count pulses for a fixed period of time, or determine the time for a fixed number of pulses, or determine the period from pulse to pulse.