PWM Pulse Convert To Voltage1
PWM Pulse Convert To Voltage1
Quick Facts
BS2 and
BS1 BS2sx BS2p BS2pe BS2px
BS2e
Units in Duration 5 ms 1 ms 0.4 ms 0.65 ms 1.62 ms 0.4 ms
Average voltage equation Average Voltage = (Duty ÷ 256) x 5 volts
Require charge time
Charge time = 5 x R x C, Duration = Charge time ÷ Units in Duration
(Duration ) equation
Special Notes Pin is set to output initially, and set to input at end
Related Commands FREQOUT, DTMFOUT
Explanation
Pulse-width modulation (PWM) allows the BASIC Stamp (a purely digital device)
to generate an analog voltage. The basic idea is this: If you make a pin output
high, the voltage at that pin will be close to 5 V. Output low is close to 0 V.
What if you switched the pin rapidly between high and low so that it was high
half the time and low half the time? The average voltage over time would be
halfway between 0 and 5 V (2.5 V). PWM emits a burst of 1s and 0s whose
ratio is proportional to the duty value you specify.
The proportion of 1s to 0s in PWM is called the duty cycle. The duty cycle
controls the analog voltage in a very direct way; the higher the duty cycle the
higher the voltage. In the case of the BASIC Stamp, the duty cycle can range
from 0 to 255. Duty is literally the proportion of 1s to 0s output by
the PWM command. To determine the proportional PWM output voltage, use
this formula: (Duty ÷ 256) x 5 V. For example, if Duty is 100, (100 ÷ 256) x 5
V = 1.953 V; PWM outputs a train of pulses whose average voltage is 1.953 V.
In order to convert PWM into an analog voltage we have to filter out the pulses
and store the average voltage. The resistor/capacitor combination shown below
will do the job. The capacitor will hold the voltage set by PWM even after the
instruction has finished. How long it will hold the voltage depends on how much
current is drawn from it by external circuitry, and the internal leakage of the
capacitor. In order to hold the voltage relatively steady, a program must
periodically repeat the PWM instruction to give the capacitor a fresh charge.
How do you determine how long to charge a capacitor? Use this rule-of-thumb
formula: Charge time = 5 x R x C. For instance, the circuit below uses a 10 kΩ
(10 x 103 ohm) resistor and a 1 µF (1 x 10-6 F) capacitor:
Since, on the BS2, each unit in Duration is approximately a millisecond, it would
take at least 50 units to charge the capacitor. Assuming the circuit is connected
to P0, here's the complete PWM instruction:
PWM charges the capacitor; the load presented by your circuit discharges it.
How long the charge lasts (and therefore how often your program should repeat
the PWM command to refresh the charge) depends on how much current the
circuit draws, and how stable the voltage must be. You may need to
buffer PWMoutput with a simple op-amp follower if your load or stability
requirements are more than the passive circuit can handle.
The term "PWM" applies only loosely to the action of the BASIC
Stamp's PWM command. Most systems that output PWM do so by splitting a
fixed period of time into an on time (1) and an off time (0). Suppose the
interval is 1 ms and the duty cycle is 100 ÷ 256. Conventional PWM would turn
the output on for 0.39 ms and off for 0.61 ms, repeating this process each
millisecond. The main advantage of this kind of PWM is its predictability; you
know the exact frequency of the pulses (in this case, 1 kHz), and their widths
are controlled by the duty cycle.