How To Change Frequency On PWM Pins Of Arduino UNO

Arduino Uno is one of the most commonly used Development boards these days. It can be used for almost any application that one can think of. One such application is in high-frequency circuits. But to use a controller in a high-frequency circuit like in a buck converter, the controller must be able to generate a high-frequency PWM waves. And if the controller you are using is Arduino Uno, then you must know how to change frequency on PWM pins of Arduino Uno.

Now there are many facts about Arduino with which many students are not familiar. And one of the facts is this: “There is a certain default frequency for each PWM pin, which is called when the analogWrite command is used on that pin. And this default frequency can be changed to a value as high as 65Khz and as low as 30Hz by using just one line code”.

Looking for Arduino Mega PWM frequency change?

How To Change Frequency On PWM Pins Of Arduino Mega

You can also watch this video for quick reference:



Here is the default frequency of each PWM pin of Arduino UNO:

PWM frequency for D3 & D11:

490.20 Hz (The DEFAULT)

PWM frequency for D5 & D6:

976.56 Hz (The DEFAULT)

PWM frequency for D9 & D10:

490.20 Hz (The DEFAULT)

Now, these frequencies are optimum for low-frequency applications like fading an LED. But these default frequencies are not suitable for High-frequency circuits. For example, 1Khz is nothing when it comes to an S.M.P.S.

There are many projects in which we require high-frequency pulses, One such project is a Buck-Converter. So to achieve frequency lower or higher than the default frequency on PWM pins, a one-line code you can use to change frequency on PWM pins of Arduino Uno is given below:

FOR ARDUINO UNO:

Code for Available PWM frequency for D3 & D11:

TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz

TCCR2B = TCCR2B & B11111000 | B00000010; // for PWM frequency of 3921.16 Hz

TCCR2B = TCCR2B & B11111000 | B00000011; // for PWM frequency of 980.39 Hz

TCCR2B = TCCR2B & B11111000 | B00000100; // for PWM frequency of 490.20 Hz (The DEFAULT)

TCCR2B = TCCR2B & B11111000 | B00000101; // for PWM frequency of 245.10 Hz

TCCR2B = TCCR2B & B11111000 | B00000110; // for PWM frequency of 122.55 Hz

TCCR2B = TCCR2B & B11111000 | B00000111; // for PWM frequency of 30.64 Hz

Code for Available PWM frequency for D5 & D6:

TCCR0B = TCCR0B & B11111000 | B00000001; // for PWM frequency of 62500.00 Hz

TCCR0B = TCCR0B & B11111000 | B00000010; // for PWM frequency of 7812.50 Hz

TCCR0B = TCCR0B & B11111000 | B00000011; // for PWM frequency of 976.56 Hz (The DEFAULT)

TCCR0B = TCCR0B & B11111000 | B00000100; // for PWM frequency of 244.14 Hz

TCCR0B = TCCR0B & B11111000 | B00000101; // for PWM frequency of 61.04 Hz

Code for Available PWM frequency for D9 & D10:

 

TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000010; // for PWM frequency of 3921.16 Hz

TCCR1B = TCCR1B & B11111000 | B00000011; // for PWM frequency of 490.20 Hz (The DEFAULT)

TCCR1B = TCCR1B & B11111000 | B00000100; // for PWM frequency of 122.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000101; // for PWM frequency of 30.64 Hz

Looking for Arduino Nano PWM frequency change?

How To Change Frequency On PWM Pins Of Arduino Nano

EXAMPLE:

To show you how the frequency changes on applying the above code, the Arduino circuit is simulated in Proteus:

Check out: How to add Arduino Library to Proteus and Simulate Arduino Projects

1. Two Arduino are selected and placed on Front-Panel

2. Digital Pin 3 ( PWM pin) of each Arduino is connected to the oscilloscope

3. Two separate programs are written for each Arduino:

Program A – Default frequency on Pin 3

void setup() {

pinMode(3,OUTPUT);
// put your setup code here, to run once:

}

void loop() {
analogWrite(3,155);
// put your main code here, to run repeatedly:}

Program B – Changed frequency on Pin 3

void setup() {

TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz
pinMode(3,OUTPUT);
// put your setup code here, to run once:

}

void loop() {
analogWrite(3,155);
// put your main code here, to run repeatedly:

}

4. Hex file of above programs are given to Arduino

5. Run Simulation

6. It can be clearly seen in oscilloscope that frequency is increased to a very high value when this piece of code is used :
TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz

Read Similar Articles:

| How to add Arduino Library to Proteus and Simulate Arduino Projects

| Add Microphone library to Proteus and generate audio waveforms

| How to Add And Simulate Ultrasonic Sensor Library in Proteus 

8 thoughts on “How To Change Frequency On PWM Pins Of Arduino UNO”

  1. Hi

    many thanks for the tutorial.

    Would you please let me know how I can implement these in LIFA_Base (LabVIEW interface)? I copied the relevant code e.g. “TCCR0B = TCCR0B & B11111000 | B00000101; // for PWM frequency of 61.04 Hz” but was not successful.

    Best regards
    MY

    Reply
  2. Thanks brother, it helped a lot.
    your given codes works perfectly.
    I checked the frequency on PicoScope.
    rise and fall time was also under 10 microseconds(80/20%).
    Thanks once again.
    by Himanshu

    Reply
  3. You can actually make much slower pwm signals (with variable duty cylcle), here’s an example of 1Hz at pin 9 or 10:
    void setup() {
    DDRB |= _BV(PB1) | _BV(PB2); /* set pins 9 and 10 as outputs */
    TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11) ; /* mode 10: PWM, phase correct, 16-bit */
    TCCR1B = _BV(CS12) | _BV(WGM13); /* 256 prescaling */
    ICR1 = 0x7918; /* TOP set to 31000 in order to get 1Hz */
    }
    void loop() {
    OCR1A = 0x1E46; // 25% duty cycle
    delay(1000);
    }

    Reply

Leave a Comment