The Arduino library has always had an “analogWrite()” function, even though the ATmega doesn’t have any way to generate a varying voltage. So why the name?
Well, what most microcontrollers can do is generate a pulse-width modulated signal, also known as PWM. That’s just a fancy way of saying that the microcontroller periodically generates a pulse, of which the width can be varied under software control.
Here’s a diagram from the Arduino PWM tutorial page:
It’s very easy to generate an approximate voltage between 0 and VCC by simply adding a resistor and capacitor to act as low-pass filter:
That image came from an excellent article by Scott Daniels about this same topic on the ProvideYourOwn.com website. Check it out for much more background information.
So how do these two little components turn PWM into a voltage?
To see what’s going on, we can tie the resistor to a PWM pin, call “analogWrite(N,128)” to set a 50% duty cycle, i.e. halfway, and then watch the result on the oscilloscope:
For more detail: From PWM to voltage