In many different cases you need a square-wave signal of a certain frequency. The most easy (but not efficient) way is to set an Arduino pin alternatively to HIGH and LOW. This can be done by some code like this. Note that there are two ways to toggle the pin (version 1 and version 2).
const byte pin = 2;
boolean val = false;
void setup() {
/*
Serial.begin(9600);
while (!Serial);
Serial.println(__FILE__ " " __TIME__);
Serial.end();
/* */
pinMode(pin, OUTPUT);
while (true) {
// version 1:
//digitalWrite(pin, val = !val);
// version 2:
digitalWrite(pin, HIGH);
digitalWrite(pin, LOW);
}
}
void loop() {}
(The while-loop might have been replaced by the standard void-loop function but this loop also does some extra house-keeping.)
The Serial-code actually is deactivated but can be activated by adding a slash just before /*.
These are the results comparing an R3 to an R4 (Minima):
R3:
version 1: 166.015 Hz
version 2: 159.355 Hz
(no matter if Serial had been activated or not)
R4:
version 1:
without Serial: 543.910 Hz
with Serial: 498.625 Hz
version 2:
without Serial: 544.000 Hz
with Serial: 583.775 Hz
As R4 is operated with 48 MHz compared to R3 operated with 16 MHz it is clear that its perfomance is about three times higher, even a little bit more. I did not believe my eyes when I saw the results; it is completely unclear to me: version 1 is getting slower when Serial is temporarily activated but version 2 is getting faster when Serial is temporarily activated.
You are kindly invited to do your own tests.