generate high freq tone in arduino

Hi, I'm trying to generate a ultrasonic tone ( > 20 khz) with that could be picked up by speakers .
I have tried just using the tone function, but I seem to be getting a lot of subharmonics also and the sound it makes is really annoying. Ideally, the sound will be inaudible for most people.
actually, I've also been buy piezo ultrasonic speaker with range 4 - 60 khz but the sound still annoying

So, (I know very little about all this) my questions are:

  1. What is the best way to generate the ultrasonic sound with an arduino uno?
    thanks if u want to help me :frowning:

I have tried just using the tone function, but I seem to be getting a lot of subharmonics

tone () produces a square wave; you're bound to get harmonics.
Try something like an AD9850 module

im sorry , im still newbie in arduino but how to do it ? (combining module to arduino)

Well, I'd start by Googling "Arduino AD9850", then I'd probably have a look at the Playground

i can use the library toneAC with genereate tone with > 20 khz but when i want to generate random is so annoying please check my code if i hv a mistake

#include <toneAC.h>
#include <Keypad.h>
int acak;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
byte colPins[COLS] = {6,7,8,12}; //connect to columnpinouts
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
//Serial.begin(9600);
}
void loop()
{
char key = keypad.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case ‘A’:
toneAC(20000);
break;
case ‘B’:
for(;:wink:
{
acak=random(25000,40000);
toneAC(acak);
}
break;

case ‘D’:
toneAC(25000);
break;

default:
noToneAC() ;
Serial.println(key);
}
}
}

Well, thanks for showing us why we ask you to use code tags when posting code.

 for(;;)
{
acak=random(25000,40000);
toneAC(acak);
}

You don’t give it any time to play the tone before changing it.

A square wave (or rectangle wave) contains harmonics but no subharmonics. If you are hearing low frequency noise, the piezo transducer might be over-driven so it's rattling/buzzing/resonating at a lower, more audible frequency.

Hi, I'm trying to generate a ultrasonic tone ( > 20 khz) with that could be picked up by speakers.

BE CAREFUL if you have a high-power amplifier..... i.e. You can usually fry the tweeter in a 100W speaker with a 20W constant test-tone. And, at around 20kHz, you might not hear it or it may not sound that loud.

You're probably getting subharmonics due to other interrupts interfering with the tone interrupt.

On AVR, you want to use hardware PWM with a library like TimerOne that lets you control the PWM carrier frequency. Then the hardware will generate the tone without gaps, even when interrupts like Serial and millis need CPU time.

On ARM, it's possible to set the priority level of interrupts, so if you make one higher priority, it can interrupt the others. But AVR chips don't have that feature.

You are most likely getting subtones due to Aliasing. Unless your output rate is over 40 kHz (Nyquist/Shannon sampling theorem), there is no way regardless of what interrupts you are using that you can create a 20 kHz audio waveform. Likely has nothing to do with squarewaves (which would create overtones, if sampled properly), interrupts, or anything else mentioned.

im just put the delay , but how can i interupt that infinite loop? thanks

benikyoshiro:
but how can i interupt that infinite loop?

Press the reset button.