Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough, the result is as if the signal is a steady voltage between 0 and 5V.
evive has 12 PWM pins.
Generally, Arduino’s PWM frequency is about 500Hz. In Arduino IDE, we use PWM concept through analogWrite() function. We give a value ranging on a scale of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
Where pin is the pin to write to and value is the duty cycle between 0 (always off) and 255 (always on).
In this example we will create the brightness of a LED. A led brightness depends on the amount of current flowing through it. Hence if we control the voltage supply to the LED, we can control the current flowing though it or the brightness of the LED.
We will use potentiometer as the analog input, and according to the analog reading we will output the voltage on the LED pin 13.
Given below is the Arduino sketch: