How to play specific frequency?

I want my Arduino to play a certain frequency and avoid any noise outside that frequency. For example, I try to play the frequency 20kHz - but besides 20kHz I get a huge spectrum and my speakers all produce this noise.

I connected my loudspeaker to pin 8 (+) and to Gnd (ground). Here is a screenshot of Audacity.

Note: I'm aware that tone() produces a square wave. But how can I use a sine wave instead?

My code:

void loop() {
 tone(8, 20000);
}

How can I avoid this noise? :frowning:

How can I avoid this noise?

Use a sine wave, not a square wave.

Yeah I'm aware that the problem is that tone() produces a square wave - but I've found not other WORKING possibility to use a sin wave instead. Would you mind to share some code?

I'd use one of the cheap Analogue Devices DDS devices, like AD9850

Any noise from using a square wave will be above 20KHz, you have noise below this so it is not being generated by the square wave.

Of course it is generated by the Square Wave. This is exactly how SW are working I would say.

Please correct me if I'm wrong :confused: So tbh your post made me confused a bit.

Of course it is generated by the Square Wave. This is exactly how SW are working I would say.

A square wave contains [u]harmonics[/u] which are multiples of the fundamental frequency. There are no lower subharmonics in a true square wave. The Arduino-output only goes positive so there is a DC (zero Hz) component.

The Tone library uses interrupt routine to actually toggle the pins, which will have jitter in its
timing, leading to subharmonics.

What kind of speaker are you using - direct connection of a low impedance speaker to an Arduino
pin is likely to fry the pin.

While jitter will occur in generation the signal this is many times higher in frequency than the signal you are generating. What this might do is to frequency modulate the signal at a very high frequency. This will then cause FM side bands in both directions, that is above and below the target frequency.

However I think it is more likely that what you are hearing is simple noise from the rest of the system. Try connecting your system up and just setting the digital pin high and see if you still get the noise. If you do it is coming from the power supply / system.

It is also possible that resonances in the speaker mechanics could be being activated.

The normal answer would be to provide a good band pass filter centered on the frequency you want to make. But you would need much better than your average op-amps to make this work.

What are you doing?

Most people can't here 21KHz although I know my son can. He is 37 now and it has come down from 25KHz when he was young, but such hearing is not common.

jonas00:
Of course it is generated by the Square Wave. This is exactly how SW are working I would say.

You can see this is not as simple as you thought.

Probably FM sidebands in both directions, but soundcard anti-aliasing filter has removed the ones
above 20kHz very efficiently.

To be honest I think the lack of harmonics above 20KHz is to do with the sampling rate of Audacity.