Not getting any rpm reading

Hello everyone, I am in a big problem and freaking out because my project deadline is due next week.
I want to get rpm reading from a motor using an IR sensor and ESp32. I have uploaded the code below. But everything is fine, the only problem is that I am getting 0 value. Pls help me.
Thanks

//DIY Tachometer to Measure Accurate RPM using ESP32
//This code is published by https://www.circuitschools.com
//Attribution required to republish.
#include <LiquidCrystal_I2C.h>

// Create the lcd object address 0x3F and 16 columns x 2 rows
LiquidCrystal_I2C lcd (0x3F, 16, 2); //
unsigned long lastflash;
int RPM;
void ICACHE_RAM_ATTR sens() {
  RPM++;
}
void setup() {
  Serial.begin(9600);
 //lcd. init ();

  // Turn on the backlight on LCD.
  lcd. backlight ();

  // IR Infrared sensor
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), sens, RISING);
  //SENSOR: GPIO0 (NodeMCU - D3)
}

void loop() {
  noInterrupts();
  int wings = 1; // no of wings of rotating object, for disc object use 1 with white tape on one side
  int RPMnew = RPM / wings; //here we used fan which has 3 wings
  Serial.println(" Rev/sec :");  //Revolutions per second
  Serial.println(RPMnew);
  lcd.clear();
  lcd.print(RPMnew);
  lcd.print("Rot/sec");
  Serial.println("Rev/min: ");   //Revolutions per minute
  Serial.println((RPMnew * 60));
  lcd. setCursor (0, 1);
  lcd.print(RPMnew * 60);
  lcd.print("Rot/min");
  RPM = 0;
  interrupts();
  delay(1000);  //1 second.
}

Please read and follow the forum guide in the sticky post at the top of most forum sections. Your post is breaking forum rules. Please edit it to fix that.

Updated it.

Variables that are updated in interrupt routines and read by the rest of the code should be declared volatile. See if that helps.

Otherwise please post your schematic, because the pin numbers shown on the image you posted don't appear to match the pin numbers in your code.

Tried using both approaches which u mentioned, but still not getting any rpm value.

try 57600? Your serial prints etc are taking too long.

on the display or the serial monitor?

What are you seeing on the display?

I only suggested one approach. I also suggested you post your schematic.

Have you tested the IR sensor? Connect an led + series resistor to its output, provide power and test placing objects in front of the sensor to see if the led lights and goes out when the object is removed.

Your interrupts are turned off for way too long.
Should turn them off, then update a few variables, then immediately turn them back on.
Some reading material.

First I want to see it on serial monitor. Once it is shown there I will try on display.

Is the LCD not connected? That might cause the code to freeze.

lcd.begin() is missing from setup()

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.