We have mentioned about digital input, digital output and analog input. How do we do analog output ?
There are no analog output pins on the Arduino and how can I control the intensity or brightness of the LED or variable speed of the DC motor or fan. Yes, you can do analog output style, it is called Pulse Width Modulation (PWM) and Arduino have a function called analogWrite() to accomplished this with digital pins. This is done by turning on and off the digital pins very quickly at a rate of 500 times per seconds ( frequency of 500Hz).
If you look at the Arduino UNO board, there are ~ beside the following pins - 3, 5, 6, 9, 10 and 11. These are the PWM pins you can use. To use the analogWrite(), the syntax are :-
analogWrite( pin, dutyCycle );
The pins are 3,5,6,9,10 and 11 and the dutyCycle is a value from 0 to 255.
|PWM duty cycle|
If you want the LED to be 100% brightness or the PC fan running at full speed, set the dutyCycle to 255. If you want 50%, set the dutyCycle to 128. If you look at the Aduino Fade example, it uses pin 9 instead of the pin 13 for Blink example. This is because pin 13 is not a PWM pin.
Please note that the function analogWrite() have nothing to do with the Analog Pins!!!
|CPU Cooler with PWM|
An application of PWM is most commonly found in CPU air cooler with 4 wires to control the speed (RPM) of the fan based on the temperature sensor of the CPU. This is to reduce the noise level by controlling the speed (RPM) of the fan.
You can refer to this article on controlling DC motor / fan speed using PWM.