#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ENCODER_A 32 // CLK pin
#define ENCODER_B 33 // DT pin
#define PULSES_PER_REV 20 // Adjust based on your encoder specs
volatile int pulse_count = 0;
unsigned long last_time = 0;
float rpm = 0;
// LCD initialization (address 0x27, 16 columns, 2 rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);
void IRAM_ATTR countPulses() {
pulse_count++;
}
void setup() {
Serial.begin(115200);
pinMode(ENCODER_A, INPUT_PULLUP);
pinMode(ENCODER_B, INPUT_PULLUP);
// Attach interrupt to count pulses on rising edge
attachInterrupt(digitalPinToInterrupt(ENCODER_A), countPulses, RISING);
// Initialize the LCD
lcd.begin(16, 2);
lcd.setBacklight(1); // Turn on the backlight (if available)
lcd.print("RPM Monitor");
delay(2000); // Display "RPM Monitor" for 2 seconds
lcd.clear();
}
void loop() {
unsigned long current_time = millis();
if (current_time - last_time >= 1000) { // Update RPM every 1 second
rpm = (pulse_count / (float)PULSES_PER_REV) * 60.0; // Convert to RPM
pulse_count = 0; // Reset pulse count
last_time = current_time;
// Display RPM on Serial Monitor
Serial.print("RPM: ");
Serial.println(rpm);
// Display RPM on LCD
lcd.clear(); // Clear the display
lcd.setCursor(0, 0); // Set cursor to the first row
lcd.print("RPM: ");
lcd.print(rpm); // Display the RPM value
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4