And the source code and explanation is below .
. You have already learned a lot of things about timer and counter in previous chapter . That section is the base of this chapter . So please make a clear concept about timer and counter and interrupt . However PWM is one kind of avr timer mode . From which we can generate a square wave signal which pulse can be controlled by us . There are three output compare register OC0, OC1A, OC1B , & OC2 we have used only one OC0. Please refer datasheet for detail about these register . And you can find which register bit need to be set or reset according to your need . In above example circuit output is taken from OC0/PB3 . This is 8 bit register so you have to place value from 0 to 255 and according width of pulse is generated with respective duty cycle . Say if you place 0 the speed of motor will be minimum (width is minimum) else if you place 255 the speed of motor will be faster( width will be maximum ) .
Trick : from ADC you can generate value from 0-1023 use if condition and use this value from ADC according to value from ADC place to OCR and make your changes and you can make a temperature controlled DC fan using this concept