How to use a Speed Sensor with Arduino

//How to use a Speed Sensor with Arduino

How to connect and use a Speed Sensor with an Arduino


Whether you’re building a small robot or using any kind of motors in your projects, it might be useful to know the speed at which your are driving them.

In this tutorial we will see how to connect and use an infrared speed sensor based on the LM393 chip.

The speed sensor uses a disc with holes (encoder disc) to block the infrared beam, thus by counting the number of times the sensors goes from Low to High we can calculate the number of revolution for a given time period.

For our tutorial we will count the number of time the speed sensor goes from Low to High in a second and then divide that number by 20 (number of holes in the encoder disc) to get the number of revolution per second.


The speed sensor uses only 1 pin that goes from Low to High to detect holes in the encoder disc.

Since we will be using Interrupt zero to read the speed sensor, we need to connect it to Pin 2 (interrupt 0 pin) on the UNO.

Were are also using the L9110 motor driver to control the speed and direction of the geared DC motor.

Pin 2 is connected to the trigger pin of the speed sensor.

5V and GND is connected to the UNO

We connect the potentiometer to 3.3V and GND and use the Analog Pin 1 (A1) to read the value.

Pin 6 and  Pin 9 are connected to B-1A and B-1B of the L9110

Power and GND of the L9110 are connected to an external 5V 2A power supply since DC motors can require more current than the UNO can provide.


We will use interrupts and the TimerOne library in our code.

The TimerOne will be used to wait 1 second before displaying the speed of our motor in the serial monitor window.

Interrupts are used so that we can control the speed and direction of our motor at the same time that we read the trigger pin of the speed sensor and to display information in the serial monitor.

So every time the speed sensor pin goes High, interrupt zero will be called since it’s connected to Pin 2 of the UNO (which is the interrupt 0 pin), thus increasing the counter variable by 1.

Then when Timer1 reaches 1 second it will call another interrupt to display the results, and reset everything back to zero to start over again.

As you can see in the code, we are using the counter value after 1 second elapsed and dividing it by 20 since our encoder disc has 20 holes. Thus giving us the number of revolution per second.

As always, please check out the tutorial video to have more information.

#include "TimerOne.h"
unsigned int counter=0;

int b1a = 6;  // L9110 B-1A 
int b1b = 9;  // L9110 B-1B

void docount()  // counts from the speed sensor
  counter++;  // increase +1 the counter value

void timerIsr()
  Timer1.detachInterrupt();  //stop the timer
  Serial.print("Motor Speed: "); 
  int rotation = (counter / 20);  // divide by number of holes in Disc
  Serial.println(" Rotation per seconds"); 
  counter=0;  //  reset counter to zero
  Timer1.attachInterrupt( timerIsr );  //enable the timer

void setup() 
 pinMode(b1a, OUTPUT); 
 pinMode(b1b, OUTPUT); 
  Timer1.initialize(1000000); // set timer for 1sec
  attachInterrupt(0, docount, RISING);  // increase counter when speed sensor pin goes High
  Timer1.attachInterrupt( timerIsr ); // enable the timer

void loop()
  int potvalue = analogRead(1);  // Potentiometer connected to Pin A1
  int motorspeed = map(potvalue, 0, 680, 255, 0);
  analogWrite(b1a, motorspeed);  // set speed of motor (0-255)
  digitalWrite(b1b, 1);  // set rotation of motor to Clockwise

Just copy the above Sketch code you want to use above in your Arduino IDE software to program your Arduino.

You can download the latest version of the TimerOne library here : TimerOne Library download

Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder.

By | 2018-02-23T17:15:37-05:00 May 31st, 2015|Tutorials|


  1. Magarajeswari Ramasamy November 13, 2019 at 4:49 am - Reply

    Hi, Thanks for sharing the code. I have used this code and I am getting the rpm of the motor in range of 700. But in the motor specification the speed of the motor is given as 240 (with tire). Please explain what is the difference between both the rpm.

  2. Magarajeswari Ramasamy November 8, 2019 at 1:32 am - Reply

    We have purchased a dc motor with a speed of 240 rpm . The link for the motor is attached for your refernce:

    I have watched your video and you are using the same motor. I am getting an output in the range of 600-900 rpm. Please clarify whether both rpm must match or different?. If they are different which is the actual rpm of the motor.

  3. Gora March 7, 2019 at 8:29 pm - Reply

    how can i make the reading faster? i tried to reduce the timer but it messed up

  4. Gora March 7, 2019 at 8:10 pm - Reply

    by looking at the schematic, you are using D2 as input pin for the sensor, but i dont see u are using d2 pin in your code, can you explain?

    • brainy-bits March 7, 2019 at 10:46 pm - Reply

      The sensor pin is connected to Pin 2 of the UNO, and in the code I’m using an Interrupt to detect the change.
      In this case I’m using interrupt 0 (zero) which is Always connected to pin 2 on the UNO. You can get more information about this here:

      • Gora March 20, 2019 at 7:00 am - Reply

        well thanks, i understand now, but i still cant make the reading faster, in your code i tried, the rpm is updated every second, i want to make it faster by reducing Timer1.initialize(1000000);
        the result is different if say i use 1000 or 10000, i want to make it faster so i can implement PID

  5. Ahsan February 11, 2019 at 11:45 am - Reply

    how can i measure the speed of four motors using this code by using only one controller and four sensors!.
    then the timer library may effect?

  6. Aiman February 1, 2019 at 12:19 am - Reply

    Hello there ,

    can i know the motor speed for the code, how to convert to normal speed which is (speed:distance/time) .

    Thanks ,

    Mohama Aiman

  7. NEKTARIOS September 20, 2018 at 5:22 am - Reply

    Hi , i admire your work and love to learn…My name is Nektarios Kourakis Physics teacher FROM GREECE and i built a obstacle avoid car – like – .BUT i see your work about speed sensor and i have the idea to the previous buillt to submit your sketch to the sketch of obstacle avoid car, and TRANSMIT LIVE DATA TO MY PC , SPEED, ACCELERATION …BUT HOW ? WITH WHAT ADDITIONAL COMPOUND? THE IDEAL IS TO HAVE SPEED-TIME GRAPH OUTPUT TO MY PC.
    I am rookie to arduino world and i will appreciate to give me direction.

  8. Mahmoud June 13, 2018 at 8:53 am - Reply

    how can I only read the speed of the motor, without setting a potentiometer..

Leave A Comment