About arduino analog and digital i/o pins

In the example program 'fade' in arduino software, pin 9 is used as output pin. LED fades due to increasing or decreasing voltage and it is analog. But pin 9 is digital i/o pin. Then how analog output is obtained on digital o/p pin ?

It’s not analog, it is PWM - Pulse Width Modulation.
The pin is rapidly toggled on/off to simulate the effect of analog control. The voltage is either 0V or 5V at any given moment.

By varying the dutycycle, you vary the RMS value: http://en.wikipedia.org/wiki/Root_mean_square

If the PWM is run thru a low pass filter, it makes quite good levels.
10K with 4.7uF to Gnd for example.

For fading LEDs tho, where you need a minimal voltage to turn them on, driving them with bursts of voltage (and the same current level) works better.

The 10K of the lowpass filter would limit the current to drive the LED, so this technique is better suited to low current levels applications, like inputs into an amplifier.

Your eyes averaged/interpreted the rapid on/off into a moderate brightness, depending on how often the led is on vs. it is off. Lots of motor drives do the same thing. Instead of creating variable driving voltages, they turn full on and full off, using the "duty cycle" of the percentage of on as speed control. As long as you have a device that can integrate/average the effect into intermediate result, such as eyes and spinning motors (they don't stop spinning the moment you shut them off), and the devices don't complain about the rapid change (heaters will complain and can only be driven a lot less frequently, same for magnetron inside of a microwave when you set say 75% power).

Here is a fun led project. It uses a led as a light sensor to adjust the brightness of the same led as a light source. It controls the led brightness by turning a pin on and off on a time basis.

http://playground.arduino.cc/Learning/LEDSensor

The technique is used in remotes to save battery and not be too bright in low light rooms.

It was also tested for "led wireless". Two controllers with 1 led & resistor each about a thumb width away from each other were able to do error-free serial communication both ways at the same time. That's all in a Mitsubishi Labs paper up on the net. For them that's like 2 cents in parts wireless.