I'm trying to calculate de rotation speed of a dc motor.

I'm using the sensor shown in the picture and a slotted disk attached to the motor. The results I was getting made no sense and after several tests I realised the following:

Most of the times I manually blocked or unblocked the sensor, two pulses were counted. That would mean that for each tooth of the slotted disk, up to four pulses were counted instead of one.

Bellow is a test code. I count pulses on the interrupt handler and reset the counter when calculating the motor speed.

Am I doing something wrong or is the sensor suffering from bouncing signals?

int encoderCounter = 0;
int encoderDiscSlots = 21;

int encoderPin = 19;

void encoderIncrementISR()

double calculateMotorSpeed(int interval)
  Serial.print("enc ");
  double rpm = (encoderCounter*1.0/encoderDiscSlots)*60000/interval;

  encoderCounter = 0;
  return rpm;

void setup() {

  pinMode(encoderPin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(encoderPin), encoderIncrementISR, RISING);

void loop() {
  // put your main code here, to run repeatedly:

  double rpm = calculateMotorSpeed(1000);
  Serial.print("rpm ");

enter image description here

The sensor uses the LM393 IC from TI, a spec sheet can be found here

  • 2
    the picture does not tell us anything about the sensor, other than validating the type of the sensor .... please add a link to the datasheet in you question above
    – jsotola
    Commented May 16, 2019 at 1:12
  • @jsotola added aditional sensor information to the question.
    – Gnomo
    Commented May 16, 2019 at 22:03

1 Answer 1


I've done some more search on the interwebs and found someone (in Spanish) describing the same problem. That person owns an oscilloscope and could confirm that there is some bouncing on the data signal both going up and down.

The hardware solution presented is to place a 100 nF capacitor between the signal pin and ground.

I've tried it and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.