Measuring RPM using hall effect sensor

Not sure if this will help but this is what I used in a recent project (Adding a digital RPM readout to a drill press), my hall effect sensor just outputs a constant signal, not variable so it may not work for you but maybe some of it will come in handy. for some reason it was half the actual speed though, so I just doubled it and made it a second rpm value.

#include <LiquidCrystal.h>

volatile byte rpmcount;

unsigned int rpm;
unsigned int rpm2;

unsigned long timeold;

LiquidCrystal lcd(12, 11, 6, 5, 4, 3);

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, rpm_fun, RISING);
  lcd.begin(8, 2);
  
  rpmcount = 0;
  rpm = 0;
  timeold = 0;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  RPM:  ");
}

void loop()
{
  if (rpmcount >= 10) { 
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update
    rpm = 30*1000/(millis() - timeold)*rpmcount;
    timeold = millis();
    rpmcount = 0;
    rpm2 = rpm * 2;
    Serial.println(rpm2,DEC);
    
    lcd.setCursor(0, 0);
    lcd.print("  RPM:  ");
    lcd.setCursor(0, 1);
    lcd.print("  ");
    lcd.print(rpm2);
  }
}

void rpm_fun()
{
  rpmcount++;
  //Each rotation, this interrupt function is run twice
}
1 Like