AVR PWM calculator (ATmega32, ATmega328P, Arduino)

Pick a timer, mode, prescaler and OCR value and get the PWM frequency, the duty cycle and the exact TCCR register lines. Or paste your C code and see what your registers really do. Includes what a DC motor behind an L293D gets. Free, no sign-up.

Microchip ATmega16/32 and ATmega328P datasheets · Arduino core · TI L293D · checked clock by clock · Updated 20 Nov 2026

DC motor on this PWM

Example motor: 4 Ω, 2 mH, 6,000 rpm at 12 V. Put in your motor's values; the speed is an estimate from the no-load speed (back EMF) and R.

PWM frequency31.37 kHzperiod 31.88 µs
Duty cycle25.10%high 8 µs
Resolution8 bitTOP 255
TOP0pinOCR = 64
// 0x61 = 0b01100001
TCCR0 = (1<<WGM00) | (1<<COM01) | (1<<CS00);
OCR0 = 64;

OC0 = PB3 (pin 4 on the 40-pin DIP). Set the PB3 bit in DDRB to 1, or the pin stays an input and nothing comes out.

  • Phase correct PWM, TOP 0xFF, N = 1: a valid PWM setup.

Motor

Average voltage2.36 Videal driver 3.01 V
Speed (est.)780 rpmat 0.2 A load
Current ripple28 mAL/R = 500 µs

    How AVR PWM frequency and duty cycle are calculated

    An AVR timer counts timer clocks, the CPU clock divided by the prescaler N. In fast PWM it counts 0 to TOP and starts again; the pin is set at 0 and cleared when the count passes OCR. In phase correct PWM it counts up to TOP and back down, clearing the pin on the way up and setting it on the way down, so each period takes 2 × TOP counts and the pulse is centred.

    TOP is 255 in the 8-bit modes. Timer1 also has 9-bit (511) and 10-bit (1023) modes, and modes where ICR1 or OCR1A sets TOP, which is how you get an exact frequency such as 20 kHz (ICR1 = 799 in fast PWM at 16 MHz). D is the fraction of the period the pin is high in non-inverting mode; inverting mode gives 1 − D.

    Prescaler NFast PWM, 8-bitPhase correct, 8-bit
    162.5 kHz31.37 kHz
    87.813 kHz3.922 kHz
    32 (Timer2 only)1.953 kHz980.4 Hz
    64976.6 Hz490.2 Hz
    128 (Timer2 only)488.3 Hz245.1 Hz
    256244.1 Hz122.5 Hz
    102461.04 Hz30.64 Hz

    At a 16 MHz clock. Two special cases from the datasheet: in fast PWM, OCR = 0 still leaves a one-count spike every period and OCR = TOP holds the pin high; in phase correct mode, OCR = 0 holds it low and OCR = TOP holds it high.

    What TCCR0 = 0x61 does (the course project's line)

    Of the 582 public reports of Skill-Lync's embedded project "Controlling a DC motor using PWM and monitoring its running status", 391 set the timer registers directly, and 322 of them write TCCR0 = 0x61, the single Timer0 control register of the ATmega16/32. In binary that is 0110 0001: WGM00 = 1 (phase correct PWM), COM01 = 1 (non-inverting on OC0, pin PB3), CS00 = 1 (no prescaler).

    That is a 31.4 kHz PWM, above hearing, which is a good choice for a motor. Only 1 of the 322 reports states this frequency. Another 13 write 0x64 (CS02 set: prescaler 256), which gives 122.5 Hz: audible, and slow enough for a motor with a millisecond-scale L/R to see its current rise and fall inside each pulse.

    #define F_CPU does not set the clock. It only tells util/delay.h how long a delay loop should be. The PWM runs at the clock the chip really has (its fuses, or the clock set on the part in your simulator), so check that clock before you trust any frequency. 5 reports define F_CPU as 1600000 (1.6 MHz) where they meant 16 MHz: their delays run 10 times too short, while the PWM frequency does not change.

    The duty-step bug: 64 + 20 in an unsigned char

    The project raises the speed with a push button on INT0. 279 reports use the same code: unsigned char i;, i = 64;, and i = i + 20; in the interrupt, with OCR0 = i; in the main loop. An unsigned char holds 0 to 255, so the 10th press overflows:

    Pressesi (OCR0)Duty
    06425.1%
    18432.9%
    210440.8%
    312448.6%
    414456.5%
    516464.3%
    618472.2%
    720480.0%
    822487.8%
    924495.7%
    108 (= 264 − 256)3.1%
    1128 (= 284 − 256)11.0%

    After press 9 the motor runs at 95.7%; press 10 drops it to 3.1%, nearly stopped. None of the 279 reports mentions it, and 321 of the 355 INT0 handlers in all the reports add a step with no limit. Steps of 10, 25 and 50 wrap on presses 20, 8 and 4. A fix that keeps the variable 8-bit:

    ISR(INT0_vect)
    {
        /* stop at the top: 255 = 100% */
        if (i <= 255 - 20) i += 20;
        else i = 255;
    }

    On real hardware a push button also bounces: one press can fire INT0 several times. Only 2 reports mention debouncing. A simulator does not show it.

    What the motor gets from an L293D

    The L293D's outputs are Darlington transistors, not switches with zero drop. TI's datasheet gives, at 0.6 A, a high-side drop of 1.4 V typical (1.8 V worst case) and a low-side voltage of 1.2 V typical (1.8 V worst case): about 2.5999999999999996 V lost, up to 3.6 V. 210 of the reports mention an L293D.

    On a 12 V supply at the course's 25.1% duty, the motor averages 2.36 V, not 3.01 V. With the example motor (4 Ω, 2 mH, 6,000 rpm at 12 V, 0.2 A load) that is about 780 rpm. Its L/R time constant is 500 µs, 15.7 times the 31.9 µs PWM period, so the current ripple is only about 28 mA peak to peak (a step-by-step simulation of the RL circuit gives 28 mA). At 122.5 Hz the same motor's current would rise and fall almost fully in every pulse.

    • Current limits. 600 mA continuous per channel and 1.2 A peak (non-repetitive, at most 100 µs). At start the motor has no back EMF, so it draws (Vs − drop)/R: 2.35 A for the example motor, above the peak rating. Use a soft start (raise OCR gradually) or a bigger driver.
    • EN pin or IN pin. PWM on the EN pin turns both outputs off between pulses, and the motor current has to flow back through the clamp diodes against the supply, so it dies away fast. PWM on one IN pin with the other held low keeps both motor terminals pulled low between pulses, and the current carries on through the low side. With EN PWM at low duty the current can stop between pulses, and then the speed no longer follows D × V.
    • Direction changes. Reversing a spinning motor puts the supply and the back EMF in series, so the current spike is far bigger than at start. Bring the duty down first, or stop, then reverse.

    Arduino analogWrite frequency and duty

    On an Uno, analogWrite() uses the timers as the core set them: pins 5 and 6 at 976.5625 Hz (Timer0, fast PWM), pins 3, 9, 10 and 11 at 490.196 Hz (Timers 1 and 2, phase correct). Because fast PWM's duty is (OCR + 1)/256 and phase correct's is OCR/255, analogWrite(127) gives 50.00% on pin 6 but 49.80% on pin 9. Values 0 and 255 bypass the timer: the core simply writes the pin LOW or HIGH. Both default frequencies are audible in a motor. For a quiet motor, set Timer1 or Timer2 yourself (the calculator's 328P options) and leave Timer0 alone, since millis() depends on it.

    How we checked the calculator

    The independent Python script scripts/verify/avrpwm.py runs 55 checks, and this page recomputes every example with the browser code at build time and refuses to build if they differ.

    • Clock-by-clock simulation. The counter and the output pin are stepped one timer clock at a time, following the datasheets' descriptions of fast and phase correct PWM. The measured frequency and duty match the formulas above for 26 setups across both chips and all three timers, and the simulation reproduces the datasheets' special cases (one-count spike at OCR = 0 in fast PWM, constant output at TOP).
    • Register tables. The bit layouts of TCCR0/TCCR2 (ATmega16/32), TCCR0A/B and TCCR2A/B (ATmega328P) and TCCR1A/B, the mode tables and the two prescaler lists were typed again from the datasheet PDFs. The course line 0x61, the 0x64, 0x71 and 0x81/0x01 variants in the reports, and Arduino's Timer0 setup all encode and decode to the same bits.
    • Code reading. Register lines from 5 snippets (hex, binary, (1<<BIT), _BV(), |=) are evaluated by Python's parser on a whitelist and by the page's own evaluator, and must agree.
    • Motor. The ripple formula is compared with an exact step-by-step solution of the armature's R-L circuit with back EMF (within 1%), and the simulated average current equals the load current.

    What this calculator does not cover

    • Other AVRs: ATmega2560 (Arduino Mega) has more timers with the Timer1 layout; ATtiny timers differ. The formulas are the same; the register names and pins are not.
    • Dead time and complementary outputs. The ATmega16/32 and ATmega328P timers have no dead-time hardware, so a discrete H-bridge driven by two pins needs a gap made in software. The L293D handles this inside.
    • Real motor dynamics (inertia, friction that changes with speed), the driver's switching losses and the clamp-diode current path with EN PWM. The motor panel is a steady-state estimate.
    • The MATLAB "BJT H-bridge" and four-quadrant chopper challenge of the EV courses: that is a Simulink model, not a timer setup.
    Learn embedded systems

    This DC motor PWM project is part of Skill-Lync's embedded courses, along with bare-metal AVR programming, timers, interrupts and communication protocols.

    Frequently asked questions

    What does TCCR0 = 0x61 do on an ATmega32?

    0x61 is 0110 0001 in binary: WGM00 = 1 (phase correct PWM), COM01 = 1 (non-inverting: the OC0 pin, PB3, is cleared on the compare match while counting up and set while counting down) and CS00 = 1 (no prescaler). At a 16 MHz clock the PWM frequency is 16,000,000 / 510 = 31,373 Hz, and the duty cycle is OCR0/255, so OCR0 = 64 gives 25.1%.

    How do you calculate the PWM frequency of an AVR timer?

    Fast PWM: f = f_clk / (N × (TOP + 1)). Phase correct PWM: f = f_clk / (2 × N × TOP). N is the prescaler (1, 8, 64, 256 or 1024; Timer2 also has 32 and 128) and TOP is 255 for the 8-bit modes, or ICR1 / OCRnA when they set TOP. At 16 MHz with no prescaler an 8-bit timer gives 62.5 kHz in fast PWM and 31.37 kHz in phase correct mode.

    What is the PWM frequency of Arduino Uno pins?

    Pins 5 and 6 (Timer0, fast PWM, prescaler 64) run at 16 MHz / (64 × 256) = 976.5625 Hz. Pins 3, 9, 10 and 11 (Timers 1 and 2, phase correct, prescaler 64) run at 16 MHz / (64 × 510) = 490.196 Hz. analogWrite(0) and analogWrite(255) do not use the timer at all: the core writes the pin LOW or HIGH.

    Why does my motor stop after pressing the speed button many times?

    If the duty value is an unsigned char (0 to 255) and each press adds 20 to a start value of 64, the 10th press makes 264, which an 8-bit variable stores as 264 − 256 = 8. The duty cycle drops from 95.7% to 3.1% and the motor nearly stops. 279 public reports of the course project use exactly this code and none of them mentions it. Limit the value before adding, for example: if (i <= 235) i += 20; else i = 255;

    Why is fast PWM with OCR = 0 not fully off?

    In fast PWM the pin is set at BOTTOM and cleared one count after the compare match, so OCR = 0 still leaves a pulse one timer count long every period (1/256 = 0.39% on an 8-bit timer). Phase correct mode gives a true 0% at OCR = 0. To switch the output off in fast PWM, clear the COM bits.

    How much voltage does an L293D lose?

    At 0.6 A TI's datasheet gives a high-side drop of 1.4 V typical (1.8 V worst case) and a low-side output voltage of 1.2 V typical (1.8 V worst case), so the motor sees about 2.5999999999999996 V less than the supply, up to 3.6 V less. On a 12 V supply at 25.1% duty that is 2.36 V average instead of 3.01 V. The L293D is rated 600 mA continuous per channel and 1.2 A peak (non-repetitive, at most 100 µs).

    Which PWM frequency is best for a DC motor?

    High enough that the PWM period is several times shorter than the motor's electrical time constant L/R, so the current stays smooth, and above about 20 kHz if the whine matters. Higher frequency costs switching loss in the driver, and an L293D's own switching times (propagation delays up to 0.8 µs and transitions of 0.1 to 0.35 µs in TI's table) become a visible part of the period above a few tens of kHz. The motor panel of the calculator shows the period against L/R for your motor.

    Related on Skill-Lync

    Sources

    • Microchip (Atmel), ATmega32(L) datasheet, 2503Q–AVR–02/11: Timer/Counter0 (fast and phase correct PWM equations, Tables 38–42), Timer/Counter1 (Tables 45–48), Timer/Counter2 (Tables 50–54), pin configuration. The ATmega16(L) datasheet (2466) has the same timers.
    • Microchip (Atmel), ATmega328P datasheet, 7810D–AVR–01/15: Tables 14-2 to 14-9 (Timer0), 15-3 to 15-6 (Timer1), 17-2 to 17-9 (Timer2).
    • Texas Instruments, L293x Quadruple Half-H Drivers, SLRS008D (rev. January 2016): absolute maximum ratings, Table 6.5 (output voltages at 0.6 A), 6.6 (switching characteristics).
    • Arduino, ArduinoCore-avr: cores/arduino/wiring.c (timer setup in init()) and wiring_analog.c (analogWrite).
    Free counselling

    From a timer register to an embedded job.

    Bare-metal C, timers, interrupts and motor control are what embedded and EV teams test freshers on. A Skill-Lync counsellor can map the course that fits the role you want.

    Talk to a course counsellor

    Free. We call you back, usually the same day.

    By submitting you agree to our Terms and Privacy Policy.

    Talk to a career counsellor

    Talk to a career counsellor

    Free 20-minute call. We'll map the right course to where you want to work.

    By submitting you agree to our Terms and Privacy Policy.