Counting pulses with Arduino Nano

Hello guys,
I have a 40KHz square-wave generator, and i want to count the pulses using arduino. I need to print on serial at each 40 pulses and then, reset the counting. I tried do it using Arduino but only worked fine until 5KHz, more than it is too faster to it identify. I want to know if you guys have some idea that i cam implement to make it possible.
I am just using a digital port as input, if signal is high, it add more one to a counter, if its low, do nothing.

I want to do it without use external devices, but if its not possible... You can say your ideas about which device to use.

Thank you!

Counting pulses with a frequency of 40kHz should not be a problem.
Printing it every 40 pulses (every 1ms) to the serial monitor at a low baud rate could be.
It surely would be ~250 times too fast for human brains to follow.
Not a good coder here, but I think you should use interrupts for counting pulses.
Are these short pulse trains, or is this a constant frequency.
Maybe you should Google something like "Arduino frequency counter".
Leo..

Hi!
Haha, i am not planing count it by brain, it is just a part of a larger circuit. I am newbie with Arduino, so it turns the things difficult sometimes. I searched for it on Google but nothing i could find really helped. The code i used is here below. Very simple... But i think i am not in the correct way.

int input = 9; //Square Wave In
int ant = 0; 
int cont = 0; // Counter
void setup() {
pinMode(input, INPUT_PULLUP); //It will count the zeros
Serial.begin(57600);
}

void loop() {
int reading = digitalRead(input); 
if(reading != ant){ 
  if(reading == LOW){
    cont++;
    Serial.print(cont);
  }
}
ant = reading;
if(cont>=40){
cont=0;
delay(1000); // Stop to count for 1s before start it again.
}
}

It works but not with 40KHz, only until 5KHz, any idea?
The frequency is constant.

You're trying to print every single pulse, not every 40.
Each print statement prints 1-2 characters, which at 57600 bps takes 0.14 ms per character, or 0.28 ms for two characters, so your counting will work up to 3,500 Hz.
Double your port speed and you double the speed you can count at.
Remove that Serial.print(cont) statement and you can count up to about 500 kHz.

Thank you!
I am trying to count every one until 40. When it reach the number 40 it has a 1 second delay and the count return to 0 and then start again.
If i remove the Serial.print(cont) How will i see the cont value? How will i know when it reach 40?

Printing something every pulse (40000 times per second) can't be seen by humans.
Printing every time when 40 is reached (1000 times a second) also can't be seen by humans.

What are you trying to do.
If the frequency is relatively constant, it makes more sense to count pulses for e.g. one second.
And then display the number of pulses in the serial monitor.
Leo..

I have a function generator. I want to read it, and after every 40 pulses i want to stop it for one second. Just it. But it must be precisely 40 pulses.

GiovannyS10:
I have a function generator. I want to read it, and after every 40 pulses i want to stop it for one second. Just it. But it must be precisely 40 pulses.

Wouldn't it be much simpler to generate the 40 pulses with your Arduino?

...R

GiovannyS10:
If i remove the Serial.print(cont) How will i see the cont value? How will i know when it reach 40?

Your code already checks for this reaching 40. There is no need or any practical use to try and print it out that fast - you can't read that fast, you can't react that fast to stop it, whatever. Just don't, it doesn't make sense.
When it reaches 40 you get into that if block, that's where you may print out the value of cont (if you don't believe it to be 40), and then wait a second before you start counting again.

Really guys, the Arduino was reading it correctly, but had not time enough to print it. Thank you for help. Now i just print a number when it reach 40. I was just trying to see if it identify all the 40 pulses correctly.