0

I have a DC motor which is rated at 11 200 RPM.

I would like to count the actual RPM (it seems to be maybe 1500 RPM from what I can tell manually.)

I bought a Photo Interrupter, and have connected it to pin A0. When the beam of the Photo Interrupter is broken, the reading drops to zero.

All seems to work well when using a delay of 100, but as soon as I drop the delay to less than about 50, I get multiple reads.

This is my code:

int must_print = 1;
int sensorValue = 0;

//the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);

  if (sensorValue > 0) 
    {
      if (must_print == 1)
      {
        must_print = 2;
        Serial.println(sensorValue);
      }
    }

  if (sensorValue == 0) {
    must_print = 1;
  }

  delay(100);        // delay in between reads for stability
}

At delay(100) the Serial Monitor looks like this for a single interrupt of 10 seconds:

354

At delay(50) the Serial Monitor looks like this for a single interrupt of 10 seconds:

298
312
328
336
349
360
366
371

Thanks!

3

You shouldn't use an analog pin with a photointerruptor. The photointerruptor is a digital device. Connect it to, preferably, an interrupt pin and use an interrupt to count your revolutions at high speed.

|improve this answer|||||
  • Thanks for the advice. I just read up on the interrupt pins and I see that it's definitely the way to go. I just don't understand how you would write to the Serial Monitor when using an interrupt. – Daniel Gee Jul 6 '15 at 11:12
  • 1
    You wouldn't. You just count in the interrupt - everything else is done in loop(). – Majenko Jul 6 '15 at 12:06
  • I'm using the following code, but I don't see anything in the Serial Monitor. : int pin = 13; int tcnt = 0; volatile int state = LOW; void setup() { pinMode(pin, OUTPUT); attachInterrupt(0, blink, CHANGE); } void loop() { Serial.println(tcnt); digitalWrite(pin, state); } void blink() { tcnt = tcnt + 1; state = !state; } – Daniel Gee Jul 6 '15 at 13:03
  • @DanielGee - You also need to make tcnt volatile and initialize Serial with Serial.begin(9600); – RSM Jul 6 '15 at 13:09
  • 1
    You must set the input pin to be INPUT_PULLUP – Majenko Jul 6 '15 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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