Loading...

Learning   Examples | Foundations | Hacking | Links

PWM

Fading 示例演示了用模拟输出来使 LED 亮度渐变。这个示例可以在 Arduino 软件的 File->Sketchbook->Examples->Analog 菜单下找到。

脉冲宽度调制,或PWM,是一种使用数字方法来获得模拟结果的技术。数字控制用来产生方波(一种在开/关之间跳变的信号), 通过改变开信号和关信号所占的时间,这种开-关组合能模拟出介于全开(5V)和全关(0V)之间的电压。开信号的持续时间称为脉冲宽度。要获得不同的模拟值,你只需要改变(调制)脉冲的宽度。如果这个开-关组合的变化速度足够快,在LED示例中的结果看起来就象是一个稳定在0-5V之间的电压在控制LED的亮度。

下图中,绿色的线代表一段固定的时间。这段时间范围就是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.

Written by Timothy Hirzel

Foundations