# Maximum frequency of digital signal in Arduino Uno?

As Arduino Uno has a 16 MHz oscillator but while running program it has less frequency because some of the processing power is used for running the program. I have used the `delay(1)` but it is giving me around 500 Hz.

My questions:

1. Is there any way to achieve more than 500 Hz?
2. What would be the maximum frequency of digital signal for below program?
``````void setup() {
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
}
``````
• note that digitalWrite is relatively slow. Also, wrapping everything inside a `while(true){...}` will make it a bit faster, instead of letting the loop function exit. – Gerben Jan 26 '16 at 13:57

1. Yes, use the hardware timers.

2. You can achieve 8 MHz.

Example sketch which outputs 8 MHz on pin 9 on a Uno:

``````#ifdef __AVR_ATmega2560__
const byte CLOCKOUT = 11;  // Mega 2560
#else
const byte CLOCKOUT = 9;   // Uno, Duemilanove, etc.
#endif

void setup ()
{
// set up 8 MHz timer on CLOCKOUT (OC1A)
pinMode (CLOCKOUT, OUTPUT);
// set up Timer 1
TCCR1A = bit (COM1A0);  // toggle OC1A on Compare Match
TCCR1B = bit (WGM12) | bit (CS10);   // CTC, no prescaling
OCR1A =  0;       // output every cycle
}  // end of setup

void loop ()
{
// whatever
}  // end of loop
``````

If you change OCR1A you can get lower frequencies. If you change the prescaler you can get lower frequencies again.

See my page about timers for more details.

• Below I have put a oscilloscope waveform from your code. Thanks! – Lukas Kock Aug 13 '19 at 20:05

Also, don't forget that there are alternative methods of toggling outputs.

You can use PORTS to do the job.

Here's a good example. And another here.

And this code is the idea behind it:

``````void setup()
{
DDRD = B11111111; // set PORTD (digital 7~0) to outputs
}

void loop()
{
PORTD = B11111111; // set PORTD pins (digital 7~0) high
PORTD = B00000000; // set PORTD pins (digital 7~0) low
}
``````

P.S. Don't forget that the port numbering is in reference to the chip pins and not the Arduino pins/legs.

Nick Gammon's code worked fine for me. Here is an oscilloscope picture of the waveform I've got from his code:

His code above (from Nick Gammon, on Jan 25 '16 at 20:33) worked excellent for me. I used his code on Arduino UNO and got about 7.9 MHz of output frequency on an approximately 1 V amplitude (peak-to-peak, with spikes making it go to about 2 V peak-to-peak amplitude).

The image above was obtained with my 40 MHz Oscilloscope from ICEL Manaus (manufacturer) at pin 9 (I used the Arduino UNO), with a LED and a resistor as a load at pin 9:

• a green LED of about 1.79 V voltage drop
• and a 4.7 Ohms resistor with 5% tolerance

The oscilloscope probe (Channel 2, with fall slope trigger detect) is on the 4.7 Ohms resistor. It follows the connection order out of the pin 9 from Arduino UNO: PIN 9 > green LED > 4.7 Ohms resistor > GROUND.

The green LED keeps blinking at a normal luminescence, as compared when used at other output frequencies.

I used a external 5 V DC power supply, along with the USB connection on my PC.

If you have any questions just put them here. Thanks and thanks Nick Gammon

• Glad it worked for you! The slight discrepancy in frequency could be explained by the fact that the Uno has a resonator rather than a crystal as a frequency source for the main processor. – Nick Gammon Aug 13 '19 at 22:05