Hello, I'm trying to generate 18kHz high-frequency audio sound in several ways, and tested them with a FFT analyser.
tone(pin, 18000);
I can see clear peak at 18kHz.
I tried to manually send signal HIGH and LOW alternatevely, which is one of example in Arduino.
void playTone(int pitch, long len)
{
long elapse = 0;
int u = 1000000L/2/pitch;
while(elapse<len)
{
digitalWrite(pin, HIGH);
delayMicroseconds(u);
digitalWrite(pin,LOW);
delayMicroseconds(u);
elapse += u*2;
}
}
I can see the peak, but at 16kHz, not 18kHz, also it has other small peaks around.
I used TimerOne library instead of delayMicroseconds().
void playTone2(int pitch, long len)
{
Timer1.setPeriod(1000000L / 2 / pitch);
Timer1.attachInterrupt(isr);
delay(len);
Timer1.detachInterrupt();
}
It shows a clear peak but at 16kHz, not 18kHz.
- Does delayMicroseconds() conflict with something? what are those peaks around in (2)?
- Why I'm getting 16kHz, not 18kHz? (it's a simple transducer)

