RPM proximity sensor value help !

Hi friends,
I am new here ,I need a help
I am using arduino uno with proximity sensor that displays RPM value every thing i work fine and correct ... but it shows the value like
100
110
150
200

and so on ... , i want it to show number inside 110 -120 like 116-117-118 ........
here is my code
thank you.

#include <Arduino.h>
const int sensorPin = 2; 
volatile int count = 0;  
unsigned long previousMillis = 0;  
unsigned long interval = 1000;     
void setup() {
    Serial.begin(9600);
    pinMode(sensorPin, INPUT);
    attachInterrupt(digitalPinToInterrupt(sensorPin), countRevolutions, RISING);
}
void countRevolutions() {
count++; // Increment count on each interrupt
}
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
   
    float rpm = (count / 1.0) * (60000.0 / interval); // Assuming 1 pulses per revolution
    
   
    Serial.print("RPM: ");
    Serial.println(rpm);
    
    // Reset count
    count = 0;
    
    // Update previous time
    previousMillis = currentMillis;
  }
}

Try this change

if (currentMillis - previousMillis >= interval) {
    noInterrupts(); //disable interrupts to avoid a corrupt reading of count variable
    int finalCount = count; //take a copy of count variable
    // Reset count
    count = 0;
    interrupts(); //re-enable interrupts as quickly as possible to avoid missing any
    float rpm = (finalCount / 1.0) * (60000.0 / interval); // Assuming 1 pulses per revolution

    Serial.print("count: ");
    Serial.print(count);
    Serial.print(" RPM: ");
    Serial.println(rpm);

    // Update previous time
    previousMillis += interval;
  }
1 Like

What is "and so on"? Does the value you see increase every time? By a random amount? Why are the numbers you see not multiples of 60?

Why do you want that? Do you know what the correct RPM is in this range? Do you know that the RPM increases by exactly 1 every second?

If your true RPM is less than 120, then you will get only 1 or 2 interrupts over your 1000ms measurement period. Your code should only ever print RPM values of 0, 60 or 120.

If you want to measure the RPM with an accuracy of 1 RPM for 110-120 RPM, your measurement period should be 60000ms.

1 Like

yes exactly like you said it shows multiples of 60 .
i need to display from 0 to 2500 ,but all the numbers like if my bench is working on 778 rpm i need to display 778 EX.

These values are not multiples of 60!?!

2 Likes

if my bench is working on 500 rpm the arduino shows me 500 rpm or 490-500 its normal
but if my bench is working on for example 447 rpm arduino displays like 440 or 460 ..
the last digit is always shows 0

Are those multiples of 60?

I am beginning to lose trust in you.

brother it is not always multiples of 60 ... it's like getting 10 + 10 bigger so if i'm on 220 330 350 700 ....numbers like this work fine but numbers like 234 432 177 343 i'm not getting correct value, i'm getting ~prox value like if it's 177 i am getting 180
I will try your solution and post the result

I have not posted a solution. Only a more correct version of your original code. It won't fix your problem by itself, but the changes should be incorporated in your final code, even though they won't seem to give any different outputs at first.

I think that counting pulses over a long enough time period to give the accuracy you want will result in an update rate that you will consider too slow.

So I suggest you change your method to measure the pulse period (the time between the leading edge of a pulse and the leading edge of the next pulse) using millis() or micros() and calculate RPM from that.

1 Like

Using your code, and simulating the proximity sensor by using a swept pulse generator, I get the following results on the serial monitor:
Tachometer 2024-03-22 083336

Can you see that the results are always a multiple of 60rpm?

Are the figures that you have been giving us from real data, or just a product of your imagination?


With interval set to 60000ms to get the desired resolution, you only get one reading per minute.

The display would be better formatted to remove the 2 decimal places:

   Serial.print("RPM: ");
   Serial.println(rpm,0);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.