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.
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?
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.
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.