arduino hall sensor rpm code example

Example: arduino rpm sensor

//From arduino forums, but with slight changes
//To use with a infrared sensor (KY-032)
//and (optional) a liquid crystal I2C Display (4 rows and 16 columns)
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);     //declare the lcd display

const int pinIRSensor = 2;	//that's the pin for the IR-Sensor

float rpm = 0;
float rev = 0;

unsigned long oldtime = 0;
unsigned long time;

void isr() {
  rev++;
}

void setup() {
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print(" RPM-IR-Sensor: ");
  attachInterrupt(digitalPinToInterrupt (pinIRSensor), isr, RISING);
}

void loop() {
  delay(1000);
  detachInterrupt(digitalPinToInterrupt(pinRPM));
  time = millis() - oldtime;
  rpm = ((float) rev / (float) time) * 60000.f;
  oldtime = millis();
  rev = 0;
  lcd.setCursor(0, 1);
  lcd.print(rpm);
  lcd.print(" r/min");
  attachInterrupt(digitalPinToInterrupt(pinRPM), isr, RISING);
}

Tags:

Cpp Example