PWM with variable frequency ?

I have this little code for pwm:

int inputPin = A5;
int inputValue = 0;
int ledPin = 9;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  inputValue = analogRead(inputPin);
  analogWrite(ledPin, inputValue/4);
}

my question is: is there a code so i can change the frequency of the pwm with another potentiometer ?

You need to learn how to program the Atmega 328 registers (assuming you are using an Uno) if you want to change the PWM frequency. Have a look at the Atmel datasheet.

I can understand why you might find the default PWM frequency unsatisfactory, but I have no idea why it might be advantageous to change the frequency dynamically.

The code in this Thread uses PWM to play a WAV file. Hopefully it is a simple illustration of the use of registers. You should be able to figure how the PWM frequency is set, in conjunction with the Atmel datasheet.

...R

I found out how to change the frequency from this link , but i was wondering if i could do it with a potentiometer, just like a normal PWM.

bogdan666:
if i could do it with a potentiometer, just like a normal PWM.

I don't understand this. What is usually changed with "normal PWM" is the duty cycle.

Please explain exactly what you are trying to achieve rather than how you think it might be done.

...R

well, i use the potentiometer to change the duty cycle, i want to change the frequency with another potentiometer. If i try to use this to let's say to control the speed of a big motor, that 490Hz "screaming" is a bit anoying.

bogdan666:
well, i use the potentiometer to change the duty cycle, i want to change the frequency with another potentiometer. If i try to use this to let's say to control the speed of a big motor, that 490Hz "screaming" is a bit anoying.

I would try changing to a different PWM frequency to see if the noise is more acceptable. But I would not bother with varying the frequency in response to a potentiometer.

In any case once you know how to change the frequency it will be straightforward to arrange to change the values based on the analogRead() value.

...R

Oooh it will not be straightforward belive me, i have a bit more to go to call myself a beginer..

bogdan666:
Oooh it will not be straightforward belive me, i have a bit more to go to call myself a beginer..

Did you study the code in the link I gave you earlier. That has the advantage tha I am familiar with it.

If you have another program that will cause the PWM to operate at some frequency other than 490Hz please post it so that we have a basis for further discussion and learning. If not, let's use my code.

...R