I'm trying to calculate the 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.
Below 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()
{
encoderCounter++;
}
double calculateMotorSpeed(int interval)
{
Serial.print("enc ");
Serial.println(encoderCounter);
double rpm = (encoderCounter*1.0/encoderDiscSlots)*60000/interval;
encoderCounter = 0;
return rpm;
}
void setup() {
Serial.begin(9600);
pinMode(encoderPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderPin), encoderIncrementISR, RISING);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
double rpm = calculateMotorSpeed(1000);
Serial.print("rpm ");
Serial.println(rpm);
}
The sensor uses the LM393 IC from TI, a spec sheet can be found here
-
2the 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 abovejsotola– jsotola2019年05月16日 01:12:09 +00:00Commented May 16, 2019 at 1:12
-
@jsotola added aditional sensor information to the question.Gnomo– Gnomo2019年05月16日 22:03:05 +00:00Commented 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.