code for changing PWM freq?

does any one have a code for changing the PWM freq on UNO pin 9 , from 500hz to 30kHz?

thanks emmanuel

TCCR1B = TCCR1B & 0b11111000 | 0x01;

Just add this under void setup this will give you 31372.55Hz(31.372KHz).

Take note this change will effect anything that uses your timer 1

If you take sometime to google there are many different ways to change frequency there is even a library for it , you can use the above but go do some research.

M…

Ps. just checked your other post no need to make two of the same topics, the people will help you if you do your part.

ok noted....thank you very much...

Emmanuel

As a matter of interest, although a bit off-topic, why do people want / need to change the PWM frequency? I'm guessing it's something to do with the (non-)responsiveness of the PWM'd device?

The default frequencies are in the audible range. Driving some loads (like motors) causes them to emit an annoying noise. Try connecting a piezoelectric buzzer to a pin outputting a PWM signal (analogWrite(P,127)).

Some loads respond better to higher frequencies (like high speed motors; I seem to have motors on the mind :grin:)

Some PWM driven devices require a specific frequency range; I would guess because of an internal low-pass filter.

If an LED is in motion relative to the viewer a lower PWM frequency can result in a visible blinking.

Undermentioned: TCCR1B = TCCR1B & 0b11111000 | 0x01;

Just add this under void setup this will give you 31372.55Hz(31.372KHz).

Take note this change will effect anything that uses your timer 1

If you take sometime to google there are many different ways to change frequency there is even a library for it , you can use the above but go do some research.

M......

Ps. just checked your other post no need to make two of the same topics, the people will help you if you do your part.

Thanks for sharing the info. Very Useful.

[quote author=Coding Badly date=1426827029 link=msg=2148542] The default frequencies are in the audible range. Driving some loads (like motors) causes them to emit an annoying noise. [/quote] Apart from the noise motors generate when PWMed, is there any mechanical issue that can arise from poor choice of frequency. I have one largish motor that whines during ramp up PWM period, the noise is tolerable, but it always bothered me in case it would cause premature failure.

Shpaget: Apart from the noise motors generate when PWMed, is there any mechanical issue that can arise from poor choice of frequency.

Was that meant as a statement or a question?

Apart from the noise motors generate when PWMed, is there any mechanical issue that can arise from poor choice of frequency.

You can set up mechanical resonances in the motor and load that could cause damage due to excess vibration.

I worked at a gas compressing station that used four Allison jet engines (Yes, that kind of "jet engine"; an engine that is normally connected to very large aircraft). There was an RPM range in which the engines had exactly that problem; resonance that could cause serious damage. The control strategy was to bring the engines to a speed just below that range, wait for everything to warm up, then quickly move over that range. The day before we turned the engines fully over to computer control I stopped by a 7-Eleven and picked up a fist full of job applications in case something went terribly wrong. (And the answer is yes, "terribly wrong" does mean "turbine blades ripping through the engine body". With the added caveat that the gas being compressed was hydrocarbons. Fun times.)

That was a question. Should have been a ? at the end. I guess I missed that while proofreading.

There is no vibration worth mentioning. Motor is about 50-60W (I don't have the specs nor the access to the machine at the moment) and driving a coil winding machine through a worm gear. The spool is rotating at about 1,5 RPM. The motor itself is not particularly fast. My friend chose it for torque, not RPMs. My concern was more along the lines of wear on the brushes, more sparking, commutator pitting etc...

Will the motor driver tolerate a higher frequency? Have you tried raising the frequency?

It's just a MOSFET, so yes, it should tolerate higher frequencies. No, I haven't tried.

Shpaget: It's just a MOSFET, so yes, it should tolerate higher frequencies.

Uh, well, from the perspective of the processor it's a capacitor. If the processor cannot charge / discharge the capacitor fast enough the MOSFET will spend too much time in the linear region and may turn to slag.

I can't imagine a higher frequency causing any harm to a motor. I've never heard of such a problem.

No, I haven't tried.

If you are interested, I suggest asking in General Electronics if the MOSFET you are using will tolerate switching at a higher (e.g. 62500 Hz) frequency. The question is beyond my ability to answer.

well im using the PWM for a dc dc converter, and the switching frequency of the device is 30kHz, the freq. is selected such that the size of the inductor is reduced…

I ve written a code for a PWM with freq. 31.2khz , and a sketch to control the duty ratio of the output at pin 9 by changing the a pot on pin 0…but im not get the above result???

the sketch is as below:

int sensorPin=0;
int ledPin=9;
int sensorValue=0;
float value,vin,vout,x1;

void setup() {
// put your setup code here, to run once:
pinMode(ledPin,OUTPUT);
TCCR1B = TCCR1B & 0b11111000 | 0x01;
}

void loop() {
// put your main code here, to run repeatedly:
sensorValue=analogRead (sensorPin/4);
vin=analogRead(sensorPin/4);
vout=3.8/4;
value=1-vin/vout;
x1=value*255;
analogWrite(ledPin,x1);
}

place pot at pin0
pin 9 for PWM
PWM freq=31.372kHz

can some find the mistake and correct me…??

Emmanuel

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

im using a UNO...

the freq. is selected such that the size of the inductor is reduced...

So is the core of your inductor able to stand that frequency / current without going into saturation?

Having the PWM frequency going at the same rate as a DC-DC converters switching frequency sounds like a very silly thing to do. You will get all sorts of beating.

variable PWM @31khz

int ledPin1 = 9; int ledPin2 = 10; int analogPin = 2; int value=0;

void setup() { pinMode(ledPin1,OUTPUT); pinMode(ledPin2,OUTPUT); TCCR1B = TCCR1B & 0b11111000 | 0x01; }

void loop() { value = analogRead(analogPin); analogWrite(ledPin1,value/4); analogWrite(ledPin2,value/4); }

hi Grumpy_Mike

i havent check wether the inductor can withstand that freq...is there an alternative design method??

emmanuel

.is there an alternative design method?

What do you mean? You can always use a linear regulating supply, but switching supplies need the correct sort of inductor, especially if you are running at high current.

Not sure what the code is going to tell us. You can't see beating effects from code.