This code sets a pin HIGH for 1 microsecond and then LOW for 1 microsecond. The expected frequency should be about 500 kHz. When measuring the output the frequency is about 96.4khz. Why ?

int del = 1;

void setup() {
   // put your setup code here, to run once:
pinMode(3, OUTPUT);

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

digitalWrite(3, HIGH);
digitalWrite(3, LOW);

  • Your code says 1 microsecond, but delayMicroseconds is actually much slower than that. Also the loop has some overhead. Try putting the code inside while(true){ }. Recent versions of the Arduino IDE have got better at making code faster, but if you want timing accuracy it is better to use low-level timer/counters of the AVR.
    – MichaelT
    Commented Nov 16, 2018 at 16:07

2 Answers 2


digitalWrite() takes a few microseconds to execute as well. There is also some overhead around the delayMicroseconds() function.

If you need exactly 500 kHz, you would want to consider using a timer. You can read up on how to set one up in the microcontrollers datasheet.

Unfortunately, I don't have access to an Arduino Leonardo, but here is an example code for the ATmega328P. The register names will be different, but you should be able to adapt the code to fit your Arduino.

// 500 kHz frequency

void setup() {
  cli();                 //Disable interrupt during setup
  TCCR2A = 0;
  TCCR2B = 0;

  TCCR2B |= (1 << CS20); //Enable timer without prescaler
  OCR2A = 15;            //Do interrupt TIMER2_COMPA after reaching this counter value
  TIMSK2 |= (1 << 1);    //Enable TIMER2_COMPA interrupt

  DDRD |= (1 << PORTD3); //Set Digital Pin 3 as Output Pin
  sei();                 //Enable interrupts again

ISR(TIMER2_COMPA_vect) { //If the counter has reached the point where the output needs to be low
  TCNT2 = 0;
  PIND |= (1 << PORTD3); //Toggle Digital Pin 3

void loop() {
  //your looping code here

Explanation: TIMER2 is an 8-bit timer, which means it will always count from 0 to 255, regardless of what the CPU is doing.

Normally, it counts up at the Arduino's clock frequency, like 16 MHz.

When the counter hits 15, a special code is immediately executed, called an "Interrupt Service Routine". In this code, we reset the timer and change the state of your output pin, resulting in a 500 kHz square wave on that pin.

We count to 15 since there are 16 steps in between "0" where the counter begins and 15, where we reset it.

16'000'000 Hz / 1 (no prescaler) / 16 (counter) / 2 = 500 kHz

The divisor of 2 comes from the fact that we need to change the pin state twice for every full square of the output signal.

  • 3
    Sometimes you write 500kHz, then you write 500Hz. As calculated the code is for 500Hz. As the OP asked for 500kHz, the prescaler could be set to 1 and the compare value (at which the timer fires the interrupt) to 16. In my calculation this gives you exactly 500kHz. Am I right?
    – chrisl
    Commented Nov 16, 2018 at 15:02
  • But as you wrote, digitalWrite() takes to long, so bit manipulation on the port registers are the right way to go: arduino.cc/en/Reference/PortManipulation
    – chrisl
    Commented Nov 16, 2018 at 15:04
  • Yes, of course, I'm so sorry. This is what happens when you recycle code after a long work day. I'll fix it. And you're also right, digitalRead and -Write are too slow for 500 kHz, I'll implement it via the registers.
    – towe
    Commented Nov 16, 2018 at 15:07
  • 1
    You could use one of the hardware time outputs like OC2A or OCRB and avoid the ISR/digitalWrite/bitbanging overhead entirely.
    – Dave X
    Commented Nov 16, 2018 at 15:15
  • It's fixed now - so sorry again. My scope isn't fast enough for 500 kHz, but it works as intended for 50 kHz. The "standard" outputs are nice, but you're then limited to the two pins per timer. Is that already necessary at 500 kHz?
    – towe
    Commented Nov 16, 2018 at 15:18

delayMicroseconds() has a minimum lower value of 3-4 us per the IDE documentation. It won't do 1.

loop() also adds some delay mechanisms doing it's background stuff.

You will see better results with a while() :

  while (1){
  digitalWrite(3, HIGH);
  digitalWrite(3, LOW);

with direct port manipulation in place of the digital writes.

There will also be jitter from the millis() background time keeping, if you want a smoother signal it will be best to turn off interrupts as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.