PWM
Syntax
PWM Pin, Duty, Cycles
Overview
Output pulse-width-modulation on a pin, then return the pin to input state.
Parameters
Pin is a Port.Pin constant that specifies the I/O pin to use. Duty is a variable, constant (0-255), or expression, which specifies the analogue level desired (0-5 volts). Cycles is a variable or constant (0-255) which specifies the number of cycles to output. Larger capacitors require multiple cycles to fully charge. Cycle time is very dependant on the oscillator frequency of the microcontroller. The faster the oscillator, the faster the duty cycle.
Notes.
PWM can be used to generate analogue voltages (0-3.3V) through a pin connected to a resistor and capacitor to ground; the resistor-capacitor junction is the analogue output (see circuit). Since the capacitor gradually discharges, PWM should be executed periodically to refresh the

analogue voltage.
PWM emits a burst of 1s and 0s whose ratio is proportional to the duty value you specify. If duty is 0, then the pin is continuously low (0); if duty is 255, then the pin is continuously high. For values in between,
the proportion is duty/255. For example, if duty is 100, the ratio of 1s to Voltage
0s is 100/255 = 0.392, approximately 39 percent.
When such a burst is used to charge a capacitor, the voltage across it is equal to:-
(duty / 255) * 3.3.
So if duty is 100, the capacitor voltage is approximately:
(100 / 255) * 3.3 = 1.29 volts.
