Arduino PWM fading led
From your code you are using Timer0 in FastPWM mode with top = 0xFF.

In this mode, as used in your code, OCR0A defines the pulse width rate (more precisely, OCR0A / 256).

Timer0 uses port OC0A and OC0B to ouput PWM signals, in your code, you are using only OC0A (which is perfectly fine).

Now if you take a look at ATmega 328 data sheet, chapter 14.3.3 you will see that pin OC0A maps to port D, pin PD6 (on Arduino UNO, this is port D6, according to http://arduino.cc/en/Hacking/PinMapping168).

So this means that you have used the wrong port in your setup (Arduino pin D9 maps to OC1A, which is pin PB1 of port B on ATmega328, OC1A is used by Timer1, not Timer0).

Hence you should do the following to fix the problem:


Plug the LED to Arduino pin D6 instead of D9
Fix initialize_PWM() by replacing DDRB|=(1&lt;&lt;PB1); with DDRD|=(1&lt;&lt;PD6);


Otherwise, if you want to keep Arduino pin D9, then you will have to change the timer you use: Timer1 instead of Timer0. Updating the code is left as an exercise!