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);
}
3 Answers 3
- 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 !! - 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.
-
\$\begingroup\$ 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 \$\endgroup\$sk95– sk952019年10月24日 21:14:59 +00:00Commented Oct 24, 2019 at 21:14
-
\$\begingroup\$ 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. \$\endgroup\$Peter Bennett– Peter Bennett2019年10月24日 22:46:31 +00:00Commented Oct 24, 2019 at 22:46
You are resetting IRQcount each time thru the loop.
-
\$\begingroup\$ 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. \$\endgroup\$Transistor– Transistor2019年10月24日 17:29:38 +00:00Commented Oct 24, 2019 at 17:29
-
\$\begingroup\$ That's not a problem. \$\endgroup\$hobbs– hobbs2019年10月24日 17:29:53 +00:00Commented Oct 24, 2019 at 17:29
-
\$\begingroup\$ What are the units on delay()? I don't program Audrino, but is that ticks or ns? \$\endgroup\$mjh2007– mjh20072019年10月24日 17:31:15 +00:00Commented Oct 24, 2019 at 17:31
-
\$\begingroup\$ @mjh2007 milliseconds, so it's definitely long enough. \$\endgroup\$hobbs– hobbs2019年10月24日 17:33:38 +00:00Commented Oct 24, 2019 at 17:33
-
\$\begingroup\$ Arduino
delay()
is in ms. \$\endgroup\$Transistor– Transistor2019年10月24日 17:33:52 +00:00Commented Oct 24, 2019 at 17:33
Explore related questions
See similar questions with these tags.
pinMode(pin, mode)
? \$\endgroup\$