Go Down

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

#### Michael_Schmidt

##### Aug 29, 2013, 10:42 am
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

#### dc42

#1
##### Aug 29, 2013, 11:01 am
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.

#### Michael_Schmidt

#2
##### Aug 29, 2013, 12:11 pm

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

#### nickgammon

#3
##### Aug 29, 2013, 01:35 pm
http://www.gammon.com.au/forum/?id=11504
Please post technical questions on the forum, not by personal message. Thanks!

#### dc42

#4
##### Aug 29, 2013, 02:20 pm

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