Is there a limit to the output frequency of pulses that an arduino can produce?

Hi
I am using Matlab to program arduino mega 2560 to generate pulses of frequency 5000hz and duty cycle 70%. However when I check the output on a DSO, the frequency is only around 1600hz. Is there a limit to the maximum frequency it can produce? If not what can be the possible reason for this frequency dissonance?

Is there a limit to the maximum frequency it can produce?

Yes; as far as I can remember, it's half the processor clock frequency.

AWOL:
Yes; as far as I can remember, it's half the processor clock frequency.

The clock frequency is 16 Mhz. So it should be easily able to produce pulses at 5000hz, but it doesnt. what could be wrong?

You're using it wrong, then. For high speeds, you want to be using one of the hardware timers, on its output compare pins, and you may need to take over the timer.

This is a programming issue, not an installation and troubleshooting question.

Unless you use a timer (hardware) to generate a signal, anything else in your code will impact the final frequency. Are you bit banging, in a loop?

A pin toggle in a loop is at least 4 instructions. If you have a slightly more expensive loop (end condition not just an 8 bit boolean check), say 6 instructions then you are down to 2.~ khz. And an occasional interrupt from something else will take you below the 2khz range.

How do you generate the signal?

Uhm.. You're dropping a factor of 1000 there, Pyro.

DrAzzy:
Uhm.. You're dropping a factor of 1000 there, Pyro.

Ah yes, I was working in Khz. Its 12am here and cognitive thinking stopped a few beers ago.

My bad.