Motor Speed Tester Using Arduino & IR Sensor




Introduction: Motor Speed Tester Using Arduino & IR Sensor

About: Hi, I am Mechanical Engineering student. But loves electronic mini projects. Also like to do creative things. In free time, I will be making something or will be searching for a new idea.

Hey !!!

Is it difficult to measure rpm of motor ???

I don't think so. Here is one simple solution.

Only one IR sensor and Arduino in your kit can do so.

It is very useful for DIY creators & robot makers and is very easy to make. It requires only 5 minutes for set up.

This assembly works well and can measures rpm much accurately upto 5%. I had tested it till 10000rpm.

And Yes, this can be used for any type of motor Big or Small (industrial too)

What you can do with this?

  • Measure rpm / check rpm of motor
  • measure speed of robot m/s (step 4)
  • Maintain constant speed(rpm) of DC motor (step 5)

How to use

  1. Stick aluminium foil piece as reference point anywhere on shaft or on its attachment
  2. Start the motor
  3. Place IR sensor infront of the reference point.

That's it.

Step 1: We Require

Basic Requirement

  1. Arduino
  2. IR sensor
  3. Aluminium foil

LCD if you have

  1. LCD Display
  2. 10K potentiometer or I2C module(will reduce pins)

For controlling speed

  1. Motor Driver
  2. 10K potentiometer

A rigid stand if you have/can make (recommended)

Step 2: How It WORKS ??

So what is the logic behind this ??

It works on basic principle of an encoder and it works much similar to encoder

When a shaft/body rotates, same point on it repetatively comes to original position after one revolution.

But for that it requires some time.

By measuring time (in second) between these two positions ie. completeing one revolution, we get time (second) required to complete one revolution.

Simply taking reciprocal of this value we get rps (revolution per second) of motor.

Multiplying it by 60 will give rpm

Simple haa ! ! !

RPM does not depend on Distance between centre of shaft and reference point.

Now to measure the time difference IR sensor is used. Aluminium foil is used set a reference point. Also it reflects light thus enhances sensitivity of IR. sensor gives HIGH output, when aluminium foil comes infront of the sensor. micros() command is used to measure time which is 10^-6 of second which can measure time lapse at high speed accurately.

Step 3: Preparation & Code

Basic min. requirement

If you want to measure rpm but don't have display.

No problem. You can measure on Arduino's Serial Monitor on PC.

Simply connect data pin of IR sensor to Arduino's pin 2 and connect +5 & ground pin accordingly.

Upload code and test on Serial Monitor

LCD Display

The assembly looks like mess. But it's really simple. Connecting LCD display requires too much wires and connections.

So if you have I2C connector for LCD then I highly recommend to use it. It requires only 2 Arduino pins.


The code uses unsigned long to store time. The code is written so that only when input from IR sensor changes from LOW to HIGH, it will proceed ie. calculate time difference. So it will be triggered only when leading edge of aluminium foil comes in front of the sensor. So there are less chances of false triggering. Each time one revolution is completed, arduino measures time required and it's corresponding rpm which is then displayed on LCD screen. LCD will refresh its value every 0.1 second.


  1. VSS = Ground
  2. VCC = +5V
  3. VEE = Centre pin of Potentiometer
  4. RS = Pin 8
  5. RW = Ground
  6. E = Pin 9
  7. DB0 = N/C
  8. DB1 = N/C
  9. DB2 = N/C
  10. DB3 = N/C
  11. DB4 = Pin 4
  12. DB5 = Pin 5
  13. DB6 = Pin 6
  14. DB7 = Pin 7
  15. LED+ = +5V
  16. LED- = Ground

Step 4: Update 1 : How to Measure Speed (m/s or Km/hr) of Robot

If you are mounting this on a robot and want to know speed of your robot then follow this -

A very basic principle.

Speed = Perimeter of wheel * speed of wheel

But perimeter is Pi times the Diameter of wheel

So Speed = PI*D * speed of wheel

Speed(m/s) = Pi * D(in meter) * rps

Speed(km/hr) = Pi*D(in kilo meter) * rpm * 60

Add this in code

const int D = _____ ; // Diameter in cm (include at the top)

speed = 3.14159*(D/100)*(rpm/60) // speed in m/s

speed = 3.15159*(D/100000)*(rpm*60) // speed in km/hr

Step 5: Update 2 : Controlling Rpm of DC Motor

Now we know the rpm of the motor.

So it's not difficult to manage speed of the motor.

For this purpose we require a motor driver. EN (enable) pin on driver is used to vary speed of motor using PWM (pulse width management). So simply connect pwm pin (pin 11 used in code) to the EN pin on driver. Also set the pin on driver to HIGH which drives the motor.

Same thing can be done by using fast acting NPN transisor. Use resitors to limit current.

Potentiometer knob is used to set the rpm required.


I tested this code and assembly. Every thing is working well.

Code needed to be slightly fixed because rpm of motor fluctuates wrt set point (speed is not smooth).

But you can try it, it works well. If you have any solution then it will be great.

Advantage of using IR sensor for maintaining conatsnt speed of DC motor

Suppose somehow load is increased on the motor. Which will reduce the RPM of motor. By checking real time rpm, arduino will sense that rpm has been reduced. Accordingly arduino will send signal to increase power supplied to the motor in order to compensate the reduced RPM. Similar thing will happen if load is reduced. Thus a constant RPM will be managed in any condition.

Step 6: Tips and Troubleshooting

1) rpm does not remains stable / not accurate

Change sensitivity of IR sensor by turning knob. Use stand

2) Shows wrong RPM

Reset arduino because code uses micros() which overflows fast. if this not works change sensitivity

I will recommend you to use stand to hold IR sensor

Try this simple and useful RPM tester which is easy to make.

Give your feedback and suggestions.

Thanks :)



    • Fix It! Contest

      Fix It! Contest
    • Metalworking Contest

      Metalworking Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    20 Discussions

    Hi! I have to do something similar for a school project, but instead of using batteries to power the motor I have to power it from a 7-25V power source and maintain a constant speed when it reaches a certain voltage. Do you think your project idea will work on my project? If yes, what changes should I do in the circuit? Thank you!


    5 months ago

    Just uploaded this to my board to verify it. I haven't tried using it with an IR sensor yet, but without plugging anything in I'm reading 3600 RPM +/- 100 on the serial monitor. Any explanation for this? I am rather new to coding so I'm just hoping I could plug and play. The code won't be used for an LCD, rather "rpm" will be used to control something else.

    if( ( millis()-refresh ) >= 100 )

    i haven't found this refresh variable anywhere(except the above mentioned code and initialization) can you explain the purpose of this code ad the initial value that will be present in this code

    3 replies

    Hi MithnKumar, It's very simple.

    millis() is current live time in milli seconds. So the whole program in that loop is executed only if current time 100miliseconds greater than refresh. new refresh value is set for every execution of loop.

    Hey, I just glanced at all programs. I think in 2nd program ie. program without lcd there should be one more statement in the last loop. After Serial.println(rpm); command there should be refresh = millis(); so it will set that time as new refresh value.

    So after every 0.1s there will be serial output. By the way thanks for the comment :)

    Update: VSS must be connected to Arduino GND, otherwise you will see garbled information as you power up the LCD screen.I did the project and works great, toast for you


    1 year ago

    hey Rahul. What type the IR sensor that you are using? And can i request the circuit drawing of this project? Thanks :)

    hey rahul .can i get the project report of this?

    Hi Rahul...!!! I am getting error.....I am using Flying Fish MH Sensor Series. It has two lights one for power on and other for output indicator....I have copied your code and burn it into my Arduino...I am getting error that the indicator light on sensor is turned on..and LCD is showing RPM=0...
    Then I have changed the LCD pin arrangements as LiquidCrystal lcd(12, 11, 5, 4, 3, 2); and dataIn = 7; NOw the sensor indicating light turns off it on when i put my hand in front of the sensor and turns off by removing hand. Now error is that LCD is showing starting RPM =4765 and when i placed rotating motor infront of sensor it is giving random values in thousands. MY LCD no is 1602A

    kindly help me out

    2 replies

    Hi Ateebh. I think your setup is working well. Regular small DC motors generally have rpm in range of thousands without load on it. You can relate your test with the video I posted. You can try different motor (of known rpm probably upto 1000). The main problem of non zero initial value is the issue that could not be solved for the current program and still finding solution for it. And another thing is that as rpm increases, fluctuation in value also increases. This setup is just a simple basic version. Hope this clear your doubt...

    Some advice - 1) Try varying sensitivity of sensor by varying trim pot on the sensor. 2) Use a sturdy stand for the sensor.

    Thank you so much Rahul....One more thing I want to ask you is...I want to control the speed of Hard drive motor with potentiometer by using Arduino.....if you have any code, schematic relating this...kindly help me in this....i'll be thankful to you.

    Can you please explain me the code(i.e tachometer_withoutLCD.txt) of rpm measurement without LCD. Actually, I have copied your code & using it for measuring RPM of my motor But I have encountered following problems:

    1)After removing sensor I am still getting a non-zero rpm value on Serial monitor.

    2)Initially, RPM value is not correct & after some time I am getting correct rpm value.

    Thanks in Advance.

    2 replies

    Sorry for late reply.

    I made only a basic version, so that I can check rpm of my motors. Program is written such that it will measure time difference between two successive trigger points. So it gives correct values only when motor is rotating. Else as you mention it will give non zero value. Thanks :)

    First of all thanks for your reply.

    I am using your RPM code in my Project, It would be really helpful if you will tell me that how I can resolve the problem no. 1 which I had mentioned in my earlier Comment.

    Please Reply AS SOON AS POSSIBLE.

    I had used your (tachometer_withLCD.ino)code for ir sensor.But nothing appeared(values)in display. It looked blank. What i have to do?????.iam just trying your project.kindly reply me quickly .......

    1 reply

    I think there might be a wrong connection to the display. Check pin layout of your LCD display or try a different one, it may be faulty. sorry for late reply...