Trouble with IR Sensor to Detect DC Motor Speed

Hello Everyone,

I am needing some assistance with a project that I am working on for school. For one aspect of it I am trying to make an automated process for recording a DC motor’s speed. There are quite a few projects that I have found online similar to this and they all seem to work for the individuals well. However, I seem to be getting some pretty weird recordings. When data is being displayed on the 16x2 LCD it has a very large fluctuation. For instance, with 3.3v supplied to the small dc motor (with attached 1 blade prop) it will produce values close to 4000. But, it will soon begin to show data ranging in 4000 ± 500 RPM. Can anyone help me by giving some recommendations / guidance for possible features to add to my circuit or code that will alleviate this issue that I am encountering? I am not sure if these errors are being produced due to noise in the system or if I am missing something that should be implemented. Below, I have attached an image of my current circuit as well as the code in my Arduino Uno R3. Also, it is important to note the the IR sensor in use is a " IR Infrared Obstacle Avoidance Sensor Module 3-Wire Reflective Photoelectric Sensor Module".


p.s. I am very new to this if that wasn’t obvious already.

#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()
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)10001000*60;
prevmillis = micros(); // store time for next revolution calculation
prevstate = currentstate; // store this scan (prev scan) data for next scan

// LCD Display
if( ( millis()-lcdrefresh ) >= 3000 )
lcd.print(“Speed of Motor”);
lcd.print("RPM = ");
lcdrefresh = millis();


Please post code, not text. Read the first topic telling about it.

What are you sensing? Is the sensor shielded from room lights ( LED or fluorescent lights that pulsate at 2 times mains frequency)?

An optical sensor also requires a light source (LED…) and one or more markers on the axle. How did you do that?

In this case I’m using the IR receiver pair as a blockage sensor detecting when the prop attached to my DC motor (which is covered in reflective material) passes in front of the emitter receiver pair. Which reflects the light back.
And I did not have it shielded from ambient lighting. I just created a small cowling to cover the top of the sensor and that has improved my recordings drastically. Thank you!

Know that reflexion of IR sometimes is not at all as it is for conventional light.

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