#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SENSOR_PIN 2 // Interrupt pin
#define SLOTS_PER_REV 24 // Number of slots (or interruptions) per revolution
volatile unsigned long pulseCount = 0;
unsigned long lastMillis = 0;
float rpm = 0;
// Set the LCD address to 0x27 or 0x3F depending on your module
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(SENSOR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING);
lcd.begin();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Measuring RPM...");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - lastMillis >= 1000) { // Update every 1 second
noInterrupts(); // Temporarily disable interrupts while reading pulseCount
unsigned long count = pulseCount;
pulseCount = 0;
interrupts(); // Re-enable interrupts
// Calculate RPM: (counts / slots) * (60 seconds / 1 minute)
rpm = ((float)count / SLOTS_PER_REV) * 60.0;
//display rpm on LCD screen
lcd.setCursor(0, 1);
lcd.print("Speed: ");
lcd.print(rpm);
lcd.print(" RPM "); // Extra spaces to overwrite old chars
lastMillis = currentMillis;
}
}
void countPulse() {
pulseCount++;
}
/*Notes
SLOTS_PER_REV: Set this based on how many interruptions happen per full rotation of your disk.
I2C address (0x27) may vary. If nothing appears on your screen, try 0x3F or scan with an I2C scanner script.
*/