1

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);
delayMicroseconds(del);
digitalWrite(3, LOW);
delayMicroseconds(del);

}
  • 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 Nov 16 '18 at 16:07
5

Welcome to SE.

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

I 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 outpin 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.

  • 2
    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 Nov 16 '18 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 Nov 16 '18 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. – Tobias Weiß Nov 16 '18 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 Nov 16 '18 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? – Tobias Weiß Nov 16 '18 at 15:18
0

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() :

loop(){
  while (1){
  digitalWrite(3, HIGH);
  delayMicroseconds(del);
  digitalWrite(3, LOW);
  delayMicroseconds(del);
  }
}

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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