# Counting pulses without an interrupt

**URL:** <https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347>\
**Category:** Programming\
**Created:** [August 27, 2014, 7:39pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347 "2014-08-27T19:39:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 27, 2014, 7:39pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/1 "2014-08-27T19:39:30Z")

</div>

Hello,

I have an RPM sensor that outputs a digital pulse for every rotation.  
To calculate the RPMs, I must count the pulses it outputs. However, I cannot implement an interrupt routine (ISR).

How can I count pulses without an interrupt routine?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [August 27, 2014, 8:03pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/2 "2014-08-27T20:03:05Z")

</div>

count state changes like this.

untested

```cpp
int sensorPin = 3;
unsigned long pulseTime; 
unsigned long lastPulseTime;
int lastSensorState = 0; 
int pulseCount;

void setup() 
{
  pinMode(sensorPin, INPUT);  
}

void loop()
{ 
  int sensorState = digitalRead(sensorPin);
  if (sensorState == HIGH) 
  {
    if (lastSensorState == LOW)
    {
      lastPulseTime = pulseTime;
      pulseCount++;
    }
  }
  lastSensorState = sensorState;
  // 
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 27, 2014, 8:05pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/3 "2014-08-27T20:05:19Z")

</div>

> I cannot implement an interrupt routine (ISR).

Is this homework?  
Cannot implement an ISR or cannot use an external interrupt?

If you are using a 328 or 1284 see Nicks stuff, can easily modify to your needs. Does use "internal" ISR though:

> **[Gammon Forum : Electronics : Microprocessors : Timers and counters](http://www.gammon.com.au/forum/?id=11504)**
>
> Gammon Forum : Electronics : Microprocessors : Timers and counters

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 29, 2014, 3:29pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/4 "2014-08-29T15:29:15Z")

</div>

Thank you all for the help.  
I cannot implement an ISR because the microprocessor is way too overcharged, which gives me noisy and not so accurate results. So I need some other solution.

The best alternative I've found so far was to count it by hardware (with the counters set to external interrupts)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 29, 2014, 3:54pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/5 "2014-08-29T15:54:38Z")

</div>

> I cannot implement an ISR because the microprocessor is way too overcharged

What?

> which gives me noisy and not so accurate results.

How are results noisy?

> The best alternative I've found so far was to count it by hardware (with the counters set to external interrupts)

But you think you can deal with those interrupts? I'm missing something.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 1, 2014, 4:57pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/6 "2014-09-01T16:57:28Z")

</div>

The Arduino already has a demanding main loop, I am using all serial ports, I2C, etc. I am using almost all resources already. For this reason, a solution based on ISR does not give satisfatory results, as the Arduino may fail to count some interrupts.  
If i use the timer overflow interrupt, OR SOMETHING SIMILAR (and this is my question), I don't need to call an ISR for every pulse and I might get better results.

---

<div class="post-metadata">

**Author:** ![holmes4](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/holmes4/32/215302_2.png) [@holmes4](https://forum.arduino.cc/u/holmes4)\
**Post date:** [September 1, 2014, 5:44pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/7 "2014-09-01T17:44:06Z")

</div>

> For this reason, a solution based on ISR does not give satisfatory(sic) results,

Rubbish!

> as the Arduino may fail to count some interrupts.

Double rubbish with bells on

Post your code!

Mark

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [September 1, 2014, 5:58pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/8 "2014-09-01T17:58:25Z")

</div>

> manattta:  
> The Arduino already has a demanding main loop, I am using all serial ports, I2C, etc. I am using almost all resources already. For this reason, a solution based on ISR does not give satisfatory results, as the Arduino may fail to count some interrupts.  
> If i use the timer overflow interrupt, OR SOMETHING SIMILAR (and this is my question), I don't need to call an ISR for every pulse and I might get better results.

If (which I very much doubt) an [external ?] ISR might be unsatisfactory why do you think an ISR triggered by a timer would be any better?

The reason for my doubt (in common with @holmes4 and@PaulS) is that the whole point about interrupts is that they get priority.

...R

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:50pm UTC](https://forum.arduino.cc/t/counting-pulses-without-an-interrupt/254347/9 "2021-05-05T20:50:47Z")

</div>

Closed on May 5, 2021, 8:50pm UTC


