arduino rpm sensor

Solutions on MaxInterview for arduino rpm sensor by the best coders in the world

showing results for - "arduino rpm sensor"
Emiliano
12 Nov 2016
1//From arduino forums, but with slight changes
2//To use with a infrared sensor (KY-032)
3//and (optional) a liquid crystal I2C Display (4 rows and 16 columns)
4#include <LiquidCrystal_I2C.h>
5
6LiquidCrystal_I2C lcd(0x27, 20, 4);     //declare the lcd display
7
8const int pinIRSensor = 2;	//that's the pin for the IR-Sensor
9
10float rpm = 0;
11float rev = 0;
12
13unsigned long oldtime = 0;
14unsigned long time;
15
16void isr() {
17  rev++;
18}
19
20void setup() {
21  lcd.begin();
22  lcd.backlight();
23  lcd.setCursor(0, 0);
24  lcd.print(" RPM-IR-Sensor: ");
25  attachInterrupt(digitalPinToInterrupt (pinIRSensor), isr, RISING);
26}
27
28void loop() {
29  delay(1000);
30  detachInterrupt(digitalPinToInterrupt(pinRPM));
31  time = millis() - oldtime;
32  rpm = ((float) rev / (float) time) * 60000.f;
33  oldtime = millis();
34  rev = 0;
35  lcd.setCursor(0, 1);
36  lcd.print(rpm);
37  lcd.print(" r/min");
38  attachInterrupt(digitalPinToInterrupt(pinRPM), isr, RISING);
39}