Hello, I have been working on making a Non-contact tachometer that uses an Arduino Uno R3 ATmega328P, IR proximity sensor, and an LCD to display the RPM of a motor. I don't know why but the Arduino displays the RPM as 4316 even in an idle state. Can someone please help me in debugging the code?
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7); //defining lcd pins
const int dataIN = 2; //IR sensor INPUT
unsigned long prevmillis; // To store time
unsigned long duration; // To store time difference
unsigned long lcdrefresh; // To store time for lcd to refresh
int rpm; // RPM value
boolean currentstate; // Current state of IR input scan
boolean prevstate; // State of IR sensor in previous scan
void setup()
{
pinMode(dataIN,INPUT);
lcd.begin(16,2);
prevmillis = 0;
prevstate = LOW;
}
void loop()
{
// RPM Measurement
currentstate = digitalRead(dataIN); // Read IR sensor state
if( prevstate != currentstate) // If there is change in input
{
if( currentstate == HIGH ) // If input only changes from LOW to HIGH
{
duration = ( micros() - prevmillis ); // Time difference between revolution in microsecond
rpm = (60000000/duration); // rpm = (1/ time millis)*1000*1000*60;
prevmillis = micros(); // store time for nect revolution calculation
}
}
prevstate = currentstate; // store this scan (prev scan) data for next scan
// LCD Display
if( ( millis()-lcdrefresh ) >= 100 )
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Speed of Motor");
lcd.setCursor(0,1);
lcd.print("RPM = ");
lcd.print(rpm);
lcdrefresh = millis();
}
}
Hey there, I am using an Infrared proximity sensor for my project.

Hi,
What is the source of your IR energy?
If you disconnect the sensor and gnd the Pin 2, what does your display show?
Do you have a link to data/spec of the IR sensor?
Tom....

Hey, the IR sensor doesn't work when I ground the pin 2, the display freezes when I do the same.
Kindly scroll down to the description section on this website to check the specifications of the IR module.https://www.roboelements.com/product/ir-proximity-infrared-sensor-obstacle-detector/
Thanks
Hi,
I asked;
If you disconnect the sensor and gnd the Pin 2, what does your display show?
Disconnect the IR sensor first.
What is the target that you get the IR sensor to look at?
Is the target hotter than it surroundings.
Tom..

Hi,
Unless you can get the response of the IR sensor, you will not know if it is capable of any decent frequency response.
Is the LED on the sensor flashing?
I think you will find it is not designed for your use.
Obstacle detection does not need a high response IR detector.
Tom..

Hey, let me just give you a brief about my project. I am using an Arduino Uno board, an Infrared sensor and a 16x2 LCD display. The main aim of my project is to measure the speed of a motor(low power). The use of Infrared proximity sensor in my project is to sense the presence of an object in vicinity and count the revolutions per minute of a spinning motor
Hi,
So do you see the LED on the sensor board, flash when you move and object closer than the set distance.
What on the mower are you sensing?
Tom...

Yes, I do. When the object is in the specified range of it's working, I see the LED on the Sensor board turn on and when I move away from it's detection range it turns off.
Hi,
So if you have it not looking at anything, untriggered what does your display show?
When you get it to look at the mower and flash, what is displayed?
What object on the mower are you detecting?
Tom....

Well when it is idle the LCD display shows '4316', and when I bring a spinning motor to measure it's RPM it gives me a measure of it's RPM which is fine.
When I do this, the LCD display goes off, it doesn't show a thing.
Hi,
Can I suggest you google.
arduino tachometer
You need to use a different display proceedure.
Tom...

Hey, when I am not detecting the sensor doesn't flash and I believe I have not coded for slow RPM speed as you said.
Can you kindly check my code?
Hi,
I have, but not with the LCD, I use Serial.prints to output rpm to the IDE monitor.
It doesn't freeze for me.
You need to get rid of the lcd.clear(). It will make your display blink.
You only need to update the RPM.
Try this edit to your display part of your code.
Before the RPM is printed, a block of spaces is printed to erase the old RPM value.
if( ( millis()-lcdrefresh ) >= 100 )
{
lcd.setCursor(0,0);
lcd.print("Speed of Motor");
lcd.setCursor(0,1);
lcd.print("RPM = ");
lcd.setCursor(6,1);
lcd.print(" ");
lcd.setCursor(6,1);
lcd.print(rpm);
lcdrefresh = millis();
}
Another improvement would be to only update when the RPM value changes.
Tom..

Hey Tom, it doesn't work. I believe I need to input some code that will measure very low rpm as you said and I need to set the RPM to zero when the sensor is not measuring anything, can you help me with that?