@JeroenJK

PWM is weird / hard to understand when you see it for the first time, especially implementing it yourself.

Your videos (this one and the one about Timers) really helped me. Thanks man!

@RobGirdler

I know this is many years later, but just came across this series and it has been a great help.

However, I came across an issue in this episode where the LED was staying at a constant voltage. Fix for it was to make the dutyCycle variable a volatile. Just thought I'd add it for anyone else who may come across the same issue in the future.

@GingerNingerGames

These videos are still brilliant.  I'm still finding them helpful in learning to get this stuff running outside the arduino IDE, it's a bit of fun and helping me find ways to make my code WAY faster

@humanHardDrive

The ISR function is called when the specified vector occurs. In this case, the ISR is run when Timer0 overflows. In the function you put code that is necessary to be run when the interrupt occurs. It's really applications specific, but you generally want to keep is small and concise. When the interrupt occurs, the code address where the program just was is stored, so after the ISR, the program just jumps back to that point.

@MarcinRdest

Handling interrupts makes _delay_ms longer than expected - that's why you have used so high prescaler (1024) what eliminates so high delay in _delay_ms :). The result is low freq of wave. There are two solutions:
1. Do not use interrupts :)
2. Set TCCR0B = (1<<CS00); // no prescaling
and before executing _delay_ms disable interrupts (cli) and after _delay_ms enable them again (sei) and delay is closer to reality.

@humanHardDrive

Not using hardware PWM. You have to use the two timer compare registers to produce the ON time then the OFF time. Only that way you can control the frequency of the PWM.

@shvideo1

Some videos on this topic said that all delay functions will be affected if you change the behavior of the Timer/Counter 0! If that's true, then the behavior of _delay_ms() may be unpredictable. I love these detailed videos on MCU programming. Thank you very much for all the details!

@user21XXL

thx for a great episode. 
if you put a cap  parallel to the led, you can filter out that spike which turns 
the led on at low OCR0A values (low pass filter)

@meTimetraveler

human hard drive πŸ˜ƒπŸ˜ƒπŸ˜ƒπŸ˜ƒπŸ˜ƒπŸ˜†πŸ˜†πŸ˜†πŸ˜†πŸ˜†

@humanHardDrive

How do you mean? When the duty cycle is 0, when the timer resets, the LED comes on, then immediately turns off. So it still turns on. If you want to avoid that you can add an exception to have it held low.

@adarshkrishnan3699

Using 2 PWM Channels on ATmega32.
- I want 10 pulses from OC0 & turn off PWM on OC0
- Then wait for x us.
- Then I want 10 Pulses from OC2 & turn off PWM on OC2.
- Then wait for x us.
- Repeat this step in while(1) loop.

I'hve tried many ways to obtain this.....but some times, OC0 & OC2 produces output at the same time.....which is not acceptable.
When OC0 is producing 10 pulses..... OC2 should wait till 10 pulses on OC0 pin + x us.
Quite a challenging thing....I think.
Pls do u have any idea how to do this kind of task....Pls help

@humanHardDrive

In timer video, you had to manually change the state of the pin. With PWM, all of the pin changing is handled by the hardware of the chip.

@PatrickHansen101

Marcin RdestΒ Thank you :) i was wondering why it was taking ages to get to the full 100% and reset, disabling interrupts before delay and enabling them again after, made it work as it's supposed to.

@grave8digger8

Hi, i need PWM for my Trainspeed Regulation. I didn't find anything.
Can you help me ?

@javeshbhardwaj

how can we change the frequency of the PWM wave i am using AVR atmega2560?
Can we do the same kind of overflow and ISR thing u did with a 8 bit register, with a 16 bit register?
an example will be much appreciated. I am new to hardware programming.
Thank you

@MrAshwindersingh

nice explanation

@justinberdell7517

Could someone explain to me what all these different lines mean in detail?  I am an EE, I don't often program MCUs, but I need to right now.  Cutting right to the meat and potatoes, my application requires me to set the PWM frequency of Arduino Micro digital pins 9, 10, 11 to ~32kHz, I believe it works out to 31,250 Hz specifically.  I can find examples on doing this on a ATmega328p all over the interned, but I am doing it on a Micro which uses an ATmega32u4.  I would really like to use timer4 since my application is BLDC motor control and from what I see online the ATmega32u4 timer4 is perfect for this application, but I think that looks a little complex for my sill level.  I've seen other solutions with TCCR0,1,2 in the ATmega328p that look much simpler, and I think I can pull this off with similar solutions only on the ATmega32u4.  Can anyone explain to me how I might do this???

@kampkrieger

why do you use timers and interrupts, why not just set OCR0A to another value instead of the duty cycle variable?

@stratorenzo

thanks for the tutorial. i have a problem with the PWM ATtiyn85, i want to use the "dead time" Do you have any idea for this? Thanks!!

@ainstaink8312

Great video! I have been working mostly with AtMega324...looks like it slightly different in that chip