I am trying to make an RPM measurement by using an IR sensor with an Arduino MEGA. My rotated object is a white plastic disk with 36 holes (in ellipse shape to ensure that the light passing though). When I have completed the installation I run the following code to measure the pulses and verify that the sensor works fine and indeed the pulses on the serial plotter looks fine.
int sensor = 21;
int wave;
void setup() {
Serial.begin(9600);
pinMode(sensor, INPUT);
}
void loop() {
wave=digitalRead(sensor);
Serial.println(wave);
delay(1);
}
But when I run another code to measure the rpm i get strange values (the "rpm" variable is not used in this version of code). In order to track the problem I run the following code and measure just the "rev" variable to see if it counts correctly the holes, but even when i just rotate it one hole step, I get a value of 6 or 9. When I give it a half turn i get 70 or a similar value.
What am I missing?
int rev =0;
int rpm;
int previous=0;
int mytime;
void rotation()
{
rev=rev+1;
}
void setup() {
Serial.begin(9600);
attachInterrupt (digitalPinToInterrupt(21), rotation, RISING);
}
void loop() {
delay(1000);
mytime=millis()-previous;
rpm=(rev/mytime)*60000/36;
previous=millis();
Serial.println(rev);
rev=0;
}
how do you insure you do not do any rotations during the delay()? Mixing millis() and delay() is not a good idea. Why do you need to wait one second between readings when what is being read responds faster than a second?
Describe the purpose of the project and perhaps a different or more tried and true way to do the thing can be realized.
Project description (brief):
I want to measure the speed of a wheel that its speed is controlled by an electric throttle. Then I will pass the value in a VR project to simulate the speed of the vehicle. I have attached the aforementioned disk with holes so as to rated with the wheel in order to measure the speed.
how do you insure you do not do any rotations during the delay()? Mixing millis() and delay() is not a good idea. Why do you need to wait one second between readings when what is being read responds faster than a second?
I just rotate the disk with my hand to watch the reliability. But ok I can by delete the millis and just keep the delay, otherwise how can i measure the speed and start the next measurement circle?
I re construct the loop part, do you agree? But still have the same issue
As you can see, initially I do not want to count the rpm but the variable "rev" in order to identify the reliability of the installation and code.
So, i expect that when i rotate the disk lets say 3 holes, to get rev value equal to 3. But, by using the above code i get 34 or 25, is not stable.
I also change the code to
unsigned long OneKDelay = 6000;
to give me enough to time to make a complete disk rotation by hand and expect to get rev=36 (reminder: the disk has 36 holes) but get around 170
Hi,
what kind of motor are you using?
Post a schematic and photo of your project, as it seems that there are electrical noises (perhaps from the motor), causing undue interrupts.
currently i am not rote it with a motor but by hand in order to validate it. I have attach the disk with its axis to hole and rotate it by hand slowly to get the values.
But as i said in my initial post (post#1) the pulses feedback is OK.
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.
Hi,
From the graph you posted, there may be an undefined zone between 0V and 5V, due to the "slow" rise.
I suggest using a Schmitt trigger between your IR sensor and the UNO input, such as the CD40106.
I am not sure that I am following you. You mean that the pulses on the graphs should be more "vertical"? Is this a problem with my sensor? All the examples or tutorials that I have watched do that simple coding to measure speed (no one counts just the rev but they ensure that its working).
To avoid using Schmitt trigger is it possible to count the times that the digital inputs goes from 0 to 1 without using attach interrupt?
The output from serial monitor for pulses observation is as in the following picture. The holes are not drilled accurate so i believe that this is the reason that there are a few double 0 or double 1.
That apparent slow rise time is not 'real'. It is an artifact of the low sampling rate used.
There is a delay of 1ms between taking and plotting digital readings, and the serial baud rate is relatively low. This causes the rise and fall times to look as though they are at least 1ms, although in reality they may be much faster.