I think you would need a filter with a very steep roll off and use 980Hz PWM to get a clean 440Hz sine wave without the filter attenuating it significantly.
Thanks for the reply -analogWrite - is a square wave.
I generate a PWM Sine wave on Pin 10.
My question is not about the Code but the Lowpass filter.
I thought the Lowpass must only cut of the High Freq PWM component but found out that the Sine wave "looks" the cleanest with R/C selected for 79Hz cut off Freq.
My Code: Please Note Copied from chatGpt!
#include <TimerOne.h> // Include TimerOne library
const int pwmPin = 10; // PWM output pin
const int sampleRate = 8000; // Sample rate in Hz
const int amplitude = 127; // Amplitude of the sine wave
const int frequency = 440; // Frequency of the sine wave in Hz
void setup() {
pinMode(pwmPin, OUTPUT);
Timer1.initialize(1000000 / sampleRate); // Set timer for sample rate
Timer1.attachInterrupt(generateSineWave); // Attach the interrupt
}
void loop() {
// Main loop does nothing, everything is handled in the interrupt
}
void generateSineWave() {
static int index = 0; // Index for the sine wave
float sineValue = sin(2 * PI * frequency * index / sampleRate); // Calculate sine value
int pwmValue = amplitude * (sineValue + 1) / 2; // Scale to 0-255
analogWrite(pwmPin, pwmValue); // Output PWM value
index++;
if (index >= sampleRate) {
index = 0; // Reset index after one second
}
}
That is true but your PWM frequency is only a few Hertz higher the the sinewave frequency. So for a simple RC lowpass filter with a rolloff of only 20dB/decade you would need to lower the cutoff frequency quite a lot to get a "clean" looking signal
Select a cut off Freq and calculate R and C.
If it does not work change the cut off Freq and try again.
or
Select a value for R and C , if it is not working change R and C.
We could not get it working 40+ years ago while studying and it still does not work.(After learning all these Formulae we try to make better sub woofer filters but the distortion was bad.)
Your problem is the low PWM frequency not the LPF cutoff. A PWM frequency of only 490Hz would probably be OK for making sinewaves of 50Hz or less. You need to use a higher PWM frequency to get a clean sinewave at 440Hz.
So if you used pins 5 or 6, you'd get double the PWM frequency, and hence you'd get smaller spikes for a given low pass filter compared to using pin 10.
As I, and others, said you won't be able to filter out the PWM frequency with a simple RC filter when you are trying to produce a 440Hz sine wave with a PWM frequency that's only just over 2x your 440Hz.
Ignoring filtering for the moment, the PWM frequency should probably be 10X the generated sine frequency.
Or with "infinite filtering" (or a filter with a much sharper cut-off than a simple RC filter) you can generate a square wave at the desired frequency (no PWM) and with the harmonics filtered-out, the square wave becomes a sine wave.
...Class-D audio amplifiers (which use something like PWM) typically have a switching frequency in the MHz range.
As a reference to above responses I tested this below code and the PWM freq is as what every one said 490Hz.Note the commented out line's .. chapter 2.
Sorry I didn't spot that you were bumping the PWM frequency up to 8kHz.
That's a lot better, now the frequency you are trying to filter out is ~20x the frequency of your sine wave.
Even so, I would expect, with a 440Hz RC low pass filter, that the spikes from the PWM would be about 10% of the amplitude of your sine wave.
I tested the filtered sine wave's freq and my friend chatGpt made a boo boo.
The freq is not 440Hz but 263 or 273Hz cant remember which one.
When I asked again for the code I got a complete different code that does not generate anything.
When I told AI the code is not working I got a new improved version did it 4 times all broken code when the code does generate something ,the top part is Sine but the bottom is a triangle.