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 with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.
脈衝寬度調製或PWM,是通過數字均值獲得類比結果的技術。數字控制被用來建立一個方波,訊號在開和關之間切換。這種開關模式通過改變“開”時間段和“關”時間段的比值完全類比從開(5伏特)和關(0伏特)之間的電壓。“開時間“的周期稱為脈衝寬度。為了得到不同的類比值,你可以更改,或調節脈衝寬度。如果你重複這種開關模式速度足夠快,其結果是一個介於0和5V之間的穩定電壓用以控制LED的亮度。
In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is 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.
中,綠色線表示一個固定的時間期限。此期間或周期是PWM的頻率的倒數。換言之,Arduino的PWM頻率約為500Hz,每個綠線之間表示2毫秒。一個analogWrite()的調用區間為0- 255,例如analogWrite(255)需要100%占空比(常開),和analogWrite(127)是50%占空比(上一半的時間)。
Once you get this example running, grab your arduino and shake it back and forth. What you are doing here is essentially mapping time across the space. To our eyes, the movement blurs each LED blink into a line. As the LED fades in and out, those little lines will grow and shrink in length. Now you are seeing the pulse width.
一旦你運行這個例子中,抓住你的Arduino來回搖晃。你這麼做的實質上是時間跨越時空的映射。對我們的眼睛,每個移動模糊成一條線的LED閃爍。由於LED消失和縮小,那些小行的長度會增長和收縮。現在你就可以看到脈衝寬度。