Arduino Tachometer

Program Download:

Arduino Sketch
lcd_tachometer.ino

The Software
           There are two main portions of code that you'll see explained and in detail below:
    -Main Loop LCD Update
    -Interrupt Time Update



           The main loop seen below is where the RPM is calcuated and the LCD is updated. Since the main loop is gigantic while(1) loop, which means it runs over and over forever, the RPM is being calcualted and LCD updated many times a second. The interrupt function is counting the time between interrupts so that in the main loop the RPM calculation can be done.

16x2 Static LCD Text

------------« Begin Code »------------
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 5, 9, 10, 11, 12);

volatile float time = 0;
volatile float time_last = 0;
volatile int rpm_array[5] = {0,0,0,0,0};

void setup()
{
  //Digital Pin 2 Set As An Interrupt
 attachInterrupt(0, fan_interrupt, FALLING);

  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Current RPM:");
}

//Main Loop To Calculate RPM and Update LCD Display
void loop()
{
  int rpm = 0;
  
  while(1){    

     //Slow Down The LCD Display Updates
  delay(400);
  
  //Clear The Bottom Row
  lcd.setCursor(0, 1);
  lcd.print("                ");   
  
  //Update The Rpm Count
  lcd.setCursor(0, 1);
  lcd.print(rpm);   

  ////lcd.setCursor(4, 1);
  ////lcd.print(time);   

  //Update The RPM
  if(time > 0)
  {
    //5 Sample Moving Average To Smooth Out The Data
      rpm_array[0] = rpm_array[1];
      rpm_array[1] = rpm_array[2];
      rpm_array[2] = rpm_array[3];
      rpm_array[3] = rpm_array[4];
      rpm_array[4] = 60*(1000000/(time*7));    
    //Last 5 Average RPM Counts Eqauls....
      rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
  }
 
 }
}

//Capture The IR Break-Beam Interrupt
void fan_interrupt()
{
   time = (micros() - time_last); 
   time_last = micros();
}
------------« End Code »------------

           Remember that the CPU fan has 7 blades, so this tachometer is only meant to work with fans like that. If your fan or encoder only sends 4 pulses per rotation, you'll want to change that in the code so that "(time*4)".