# Counting pulses using Arduino Uno

I am trying to count pulses from a waveform generator using an Arduino Uno. The waveform generator is set at the following settings:

• Waveform Type: Pulse
• Freq: 20 Hz (50ms)
• Amplitude: 3.0 Vpp
• Offset: 0V
• Width = 5ms

So far I get only 0s displayed on my serial monitor.

Here is what I have tried so far:

1) Using pulseIn() and pulseInLong() --> no success

2) Check wiring connections --> Used multimeter to check and they are properly connected.

3) Hookup an oscilloscope to see if the waveform is being generated --> The waveform is being generated as shown in pictures below.

Note: The waveform generator is part of the oscilloscope:

My code is shown below:

volatile int IRQcount;
int pin = 2;
int pin_irq = 0; //IRQ that matches to pin 2
int result = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin (115200);
pinMode(2, INPUT);
attachInterrupt(pin_irq, IRQcounter, FALLING);
}

void IRQcounter() {
IRQcount++;
}

void loop() {
// put your main code here, to run repeatedly:

cli();//disable interrupts
IRQcount = 0;
sei();//enable interrupts

delay(500);

cli();//disable interrupts
result = IRQcount;
sei();//enable interrupts

Serial.print(F("Counted = "));
Serial.println(result);
}

• I don't know if it will default to input but do you need to use pinMode(pin, mode)? – Transistor Oct 24 '19 at 17:15
• Is the "low" part of your waveform at 0V, or at -1.5V? – hobbs Oct 24 '19 at 17:41
• From the 'scope photo, the low level of the signal is -1.5 volts, and the high level is 1.5 volts. 1.5 volts is probably too low a voltage to be recognized as a logic High by the Arduino. – Peter Bennett Oct 24 '19 at 17:52
• To be kind to the Arduino inputs, you should set the signal generator for 5 Vpp, and change the offset to +2.5 volts, so the signal switches between 0 and +5V (assuming the Arduino Vcc is 5 volts.) The digital inputs on the microcontroller should not be driven negative. – Peter Bennett Oct 24 '19 at 18:23
• Oh i see, i changed the delay to 250 ms that is why i was seeing result = 5 but now i changed it back to 500 ms and now i see result = 10, can you explain how to check if the pulse count is correct ? – sk95 Oct 24 '19 at 19:02

1. As Peter Bennett and other pointed out, initially you had set 3Vpp, or a pulse alternating +1.5V to -1.5v. As per the Electrical characteristics of Arduino's Controller, 1.5V is not guaranteed to be read as '1'. It should be atleast 0.6 of Vcc. Hence your irq pin cannot recognise edge transitions, instead it reads a '0' all time. And, you are not supposed to drive -ve voltage to the I/O pins as well to avoid damage !!
2. The pulse frequency is 20 Hz or 20 pulses per second. With 500ms delay, you are counting how many pulses occur per half a second, so result should be of value 10. That's what you are getting displayed on serial monitor.

From the 'scope photo, the low level of the signal is -1.5 volts, and the high level is 1.5 volts. 1.5 volts is probably too low a voltage to be recognized as a logic High by the Arduino.

Also, the Arduino digital inputs may be damaged by the negative voltage. The inputs on most digital ICs are designed to accept inputs between Ground and the positive supply voltage.

If the Arduino is operated from +5 Volts, you should set the signal generator for 5 Vpp, with a +2.5 volt offset to keep the signal within the Arduino's input voltage limits.

To be recognized as a logic High, the input voltage must be above 0.7 Vcc, or 3.5 volts - lower voltages may not be recognized as a High, so will not trigger an interrupt.

• I have set it to 5Vpp, however the +2.5V offset causes fluctuations in the number of counts and without the offset i have a consistent pulse count reading, is that expected ? i would think so – sk95 Oct 24 '19 at 21:14
• Check the input levels at the Arduino. The Low level should be at (or very close to) Ground, while the High level should be close to the Arduino's positive supply, which i assume is 5 volts. – Peter Bennett Oct 24 '19 at 22:46

You are resetting IRQcount each time thru the loop.

• I think that's OK. Zero the counter, enable the interrupts, delay and then print the number of interrupts at the end of the delay. – Transistor Oct 24 '19 at 17:29
• That's not a problem. – hobbs Oct 24 '19 at 17:29
• What are the units on delay()? I don't program Audrino, but is that ticks or ns? – mjh2007 Oct 24 '19 at 17:31
• @mjh2007 milliseconds, so it's definitely long enough. – hobbs Oct 24 '19 at 17:33
• Arduino delay() is in ms. – Transistor Oct 24 '19 at 17:33