0

I'm trying to find The RPM of a fan, so I removed one blade from the fan and put an IR sensor in front of it, so when the fan complete a spin the sensor will return a pulse, Here is the code I'm using:

#define PWM 9
#define DINS 2
volatile int counter = 0;
int delaycounter = 0;
int lastcount = 0;
int newcount = 0;
int countCounter = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(PWM, OUTPUT);
  pinMode(DINS, INPUT);
  attachInterrupt(digitalPinToInterrupt(DINS), readEncoder, RISING);
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(PWM,50);
  int value = digitalRead(DINS);
  
  if (delaycounter == 1000){
    lastcount = counter;
    newcount = (newcount + counter) / 2;
    //Serial.print(counter);
    counter = 0;
    delaycounter = 0;
    countCounter++;
  }else{
    lastcount = 0;
  }
  delaycounter = delaycounter + 2 ;

  //Serial.print(delaycounter);
  //Serial.print(" ");
  Serial.print(value);
  Serial.print(" ");
  Serial.print(counter/2);
  Serial.print(" ");
  //Serial.print(lastcount);
  //Serial.print(" ");
  //Serial.print(newcount);
  Serial.println();
  delay(2);
}

void readEncoder(){
  counter++;
}

The problem is the counter in the ISR in counting more than the number of RISING edges,I just want the counter value to increase by one, but instead it increase multiple times even at the FALLING edge of the pulse, like in this Serial plotter: enter image description here

I tried other solutions like using millis() and a last_time variable to track the time to reset the counter but sill the results were the same.

Edit1: I have opened Serial Monitor and here are the vales it reads, it's adding by 1 or by 2 randomly on the FALLING edge while it should only count ones and in the RISING edge: enter image description here

15
  • 1
    Removing a blade will tend to make a fan unbalanced. Check the output of your sensor with a meter. What does the voltage do as you slowly turn the fan?
    – Abel
    May 27 at 12:58
  • I will check it now, however the reading of the sensor are in the bottom of the plot but they are tiny because of scaling and they are showing that the fan is stable, also I have made a wooden frame for the fan to minimize shaking even at high speeds. @Abel May 27 at 13:05
  • So you have a fan that has no gaps between any of the blades? So how does it work then? Maybe you should only be counting the wide gap...?
    – Majenko
    May 27 at 15:43
  • well The fans blades are angles in a way that no matter how the sensor is positioned, It will read 0 witch mean there is an object in it's way, dew to it nature of detecting stuff that reflect off IR beam, so It could detect reflection from the side of the bald, also the fans spins fast enough for the sensor to not pick it up. @Majenko May 27 at 17:07
  • 1
    Could you check the DINS signal on a scope? May 27 at 19:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.