Pages: [1]   Go Down
Author Topic: Problem Programming pulse counter  (Read 433 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged


United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/forum/?id=11504
Logged


United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
  }
}
Logged

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.

Pages: [1]   Go Up
Jump to: