Go Down

Topic: Problem Programming pulse counter (Read 458 times) previous topic - next topic

Hello

I need to make a pulse counter that gives me the time between two pulses vor rpm calculating.
i will count pulses from a rpm sensor with 6000-20000prm
Arduino Uno pins usage
-pulsein on PD6(AIN1)
-20k resistor on PC1(ADC1) to GND?? <-- is this resistor really needed?

I found the Pulsein function:
Quote
pulseIn()
Description

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within a specified time out.

The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length.


the problem is that i will have up to 340 pulses per second that will be around 3ms between the pulses!
will it work with pulse in or do you have a better solution?

best regards
Michael

http://www.rcmk-tuning.de

dc42

A better solution is to feed the signal to one of the external interrupt pins (digital pins 2 and 3 on a Uno). In the interrupt service routine, read the time using micros(), subtract the time of the last interrupt to get the interval, and store the current time for use at the next interrupt.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

thanks for your fast reply

So interrupt pins should be Digial pin 2 and 3 on uno correct!?

This is my first arduiono project, do you have some hints?
or maybe an complete example code to play with?

Michael


http://www.rcmk-tuning.de

Nick Gammon

http://www.gammon.com.au/forum/?id=11504
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dc42


thanks for your fast reply

So interrupt pins should be Digial pin 2 and 3 on uno correct!?

This is my first arduiono project, do you have some hints?
or maybe an complete example code to play with?

Michael


Code: [Select]

volatile unsigned long interval;
unsigned long lastTime=0;
volatile bool hadInterrupt = false;

void setup()
{
   attachInterrupt(0, my_isr, RISING);
}

void my_isr()
{
   unsigned long now = micros();
   interval = now - lastTime;
   lastTime = now;
   hadInterrupt = true;
}

void loop()
{
  if (hadInterrupt)
  {
    Serial.println(interval);
    hadInterrupt = false;
  }
}
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Go Up