PWM with Arduino UNO

Hi Friends,

I have a question about the PWM on the Arduino UNO board. The analogWrrite() - function produces a PWM-Signal with 980 Hz. I want to drive a DC motor and the frequency is too low, hence the motor makes an annoying sound. Is it possible to change the frequency to ~20kHz?

Would be nice if anyone could help me :D :D

If you can get ahold of a copy of the "Arduino Cookbook 2nd addition", see section 18.6

Hi Friends,

So I have one problem:

We have a project whch is made to feed BEARS! Through a motor the food will be flinged away. With a PWM-Code I have achieved to start the motor but the annoying thing about it is that there is always a noise at the beginning. The reason is that the frequency is to small and this is where i need help. How can I change the frequency from 7kHz to 16kHz.

The Code will be in the attachment!

Would be very thankful for fast replys!

Motortreiber_v2_konstante_Geschw.ino (1.23 KB)

There is a search bar on these forums - try putting "change PWM frequency" into it? Just a thought...

Uziowuzio: Hi Friends,

I have a question about the PWM on the Arduino UNO board. The analogWrrite() - function produces a PWM-Signal with 980 Hz. I want to drive a DC motor and the frequency is too low, hence the motor makes an annoying sound. Is it possible to change the frequency to ~20kHz?

Would be nice if anyone could help me :D :D

The highest PWM frequency you can get depends on how many bits of resolution you use. The PWM works by counting up to a setpoint or down to a setpoint, then toggling, setting or clearing a port. Obviously, the less it has to count, the faster the PWM frequency is.

Here's a small function that I use on an UNO R3 (16 bit PWM). You can vary the resolution simply by editing "_RES" (the top of the count). This changes your resolution and PWM frequency.

For example, setting _RES to "255" gives you 8 bit PWM really fast. Set it to 4095 and you get 12 bits, but slower. Play with this and see if it works for you.

// UNO R3
// PB1 = pin 9  = OCR1A
// PB2 = pin 10 = OCR1B

#define _RES 4095 // 12 bit resolution (max value is 16 bits = 65535)

int main (void)
{
    init ();
//  Serial.begin (115200); // not using serial now

    // clear bits in timer 1 control register A
    TCCR1A &= ~(_BV (COM1A1) | _BV (COM1A0) | _BV (COM1B1) | _BV (COM1B0) | _BV (WGM11) | _BV (WGM10));

    // enable PWM for channel A (pin 9)
    DDRB |= _BV (DDB1); // pinMode (9, OUTPUT);
    TCCR1A |= _BV (COM1A1);

    // enable PWM for channel B (pin 10)
    DDRB |= _BV (DDB2); // pinMode (10, OUTPUT);
    TCCR1A |= _BV (COM1B1);

    // clear bits in timer 1 control register B
    TCCR1B &= ~(_BV (ICNC1) | _BV (ICES1) | _BV (WGM13) | _BV (WGM12) | _BV (CS12) | _BV (CS11) | _BV (CS10));
    // enable PWM mode 8, fastest clock
    TCCR1B |= (_BV (WGM13) | _BV (CS10));

    ICR1 = _RES; // set top of count

    OCR1A = 2047; // test PWM value 0.5 (pin 9)
    OCR1B = 3071; // test PWM value 0.75 (pin 10)

    while (1); // main never exits
}

Hope this helps.

Uziowuzio: Hi Friends,

I have a question about the PWM on the Arduino UNO board. The analogWrrite() - function produces a PWM-Signal with 980 Hz. I want to drive a DC motor and the frequency is too low, hence the motor makes an annoying sound. Is it possible to change the frequency to ~20kHz?

Would be nice if anyone could help me :D :D

By the way, don't make the PWM frequency TOO high, or else the inductance of the motor windings will be a high enough impedance to prevent the motor from getting any energy. Remember:

Xl = 2(pi)FL

Where Xl is inductive reactance (impedance) in ohms, F is frequency in hertz and L in inductance in Henrys.

The higher F is, the higher Xl is.

Thx guys for the fast reply!

My friend though has found a different solution :D

My friend though has found a different solution

Would you care to share it ?

Krupski: By the way, don't make the PWM frequency TOO high, or else the inductance of the motor windings will be a high enough impedance to prevent the motor from getting any energy.

Sorry but this is not correct! Only the high frequency components of the PWM are filtered out by the inductance of the motor. The high frequency components are not needed since the motor is driven by the low frequency components which should be substantially unaffected. It is quite common to add a series inductance to increase the filtering.

stowite: Sorry but this is not correct! Only the high frequency components of the PWM are filtered out by the inductance of the motor. The high frequency components are not needed since the motor is driven by the low frequency components which should be substantially unaffected. It is quite common to add a series inductance to increase the filtering.

The motor mechanically integrates (or averages) PWM pulses into a smooth rotation, but the inductance of the windings means that when the PWM driver turns on, it will take a certain period of time for the winding current to reach equilibrium and therefore generate the strongest magnetic field and therefore generate the most torque.

A high PWM frequency doesn't give the motor inductance enough time to reach full current and the magnetic field ends up being weaker (which means less torque).

Also, at low duty cycle PWM settings (where the ON time is very short) the problem is even worse.

See this link for a MUCH better explanation: http://www.precisionmicrodrives.com/application-notes-technical-guides/application-bulletins/ab-022-pwm-frequency-for-linear-motion-control

Another factor concerning PWM modulation (which has nothing to do with the inductance issue) is the fact that typically the transistors or mosfets in an H bridge turn OFF slower than they turn ON. Therefore, unless the transistor driver is "smart" and leave a small dead time between OFF and ON, there will be a short time of overlap where both transistors (the pair on each side of the H bridge) will be on at the same time, shorting the power supply to ground, creating high current spikes, electrical noise and wasting power as pure heat.

Since the "bad" time is independent of switching frequency, you can see that a higher PWM rate increases this mode of loss because more "short circuit events per second" occur at high PWM frequencies.

Krupski: See this link for a MUCH better explanation: http://www.precisionmicrodrives.com/application-notes-technical-guides/application-bulletins/ab-022-pwm-frequency-for-linear-motion-control

Intuitively there is something very wrong with that analysis but at this time I can't put my finger on it. The authority of the author and publishers make it difficult for me to just dismiss so I shall have to spend more time studying it.

Another factor concerning PWM modulation (which has nothing to do with the inductance issue) is the fact that typically the transistors or mosfets in an H bridge turn OFF slower than they turn ON. Therefore, unless the transistor driver is "smart" and leave a small dead time between OFF and ON, there will be a short time of overlap where both transistors (the pair on each side of the H bridge) will be on at the same time, shorting the power supply to ground, creating high current spikes, electrical noise and wasting power as pure heat.

100% agree here. My experience of using PWM with torque motors many many years ago is that the losses associated with the crossover current while push-pull switching the drive transistors tend to dominate all other losses when the PWM frequency is too large.