Arduino 简明教程

Arduino - Fading LED

此示例演示了 analogWrite() 函数在关闭 LED 时如何使用淡出效果。AnalogWrite 使用脉宽调制 (PWM),快速地打开和关闭数字引脚,并使用开和关的不同比率来创建淡出效果。

This example demonstrates the use of the analogWrite() function in fading an LED off. AnalogWrite uses pulse width modulation (PWM), turning a digital pin on and off very quickly with different ratios between on and off, to create a fading effect.

Components Required

您将需要以下组件:

You will need the following components −

  1. 1 × Breadboard

  2. 1 × Arduino Uno R3

  3. 1 × LED

  4. 1 × 330Ω Resistor

  5. 2 × Jumper

Procedure

按照电路图并将组件连接到面包板上,如下图所示。

Follow the circuit diagram and hook up the components on the breadboard as shown in the image given below.

components on breadboard

Note − 要找出 LED 的极性,请仔细观察。灯泡扁平边缘朝向两条腿中较短的一条腿表示负极。

Note − To find out the polarity of an LED, look at it closely. The shorter of the two legs, towards the flat edge of the bulb indicates the negative terminal.

led

为了正确安装面包板插座,像电阻器这样的元件需要将其端子弯曲成 90° 角。您还可以剪短端子。

Components like resistors need to have their terminals bent into 90° angles in order to fit the breadboard sockets properly. You can also cut the terminals shorter.

resistors

Sketch

在您的计算机上打开 Arduino IDE 软件。使用 Arduino 语言编写代码将控制您的电路。通过单击“新建”打开新草图文件。

Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open the new sketch File by clicking New.

sketch

Arduino Code

/*
   Fade
   This example shows how to fade an LED on pin 9 using the analogWrite() function.

   The analogWrite() function uses PWM, so if you want to change the pin you're using, be
   sure to use another PWM capable pin. On most Arduino, the PWM pins are identified with
   a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
*/

int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:

void setup() {
   // declare pin 9 to be an output:
   pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:

void loop() {
   // set the brightness of pin 9:
   analogWrite(led, brightness);
   // change the brightness for next time through the loop:
   brightness = brightness + fadeAmount;
   // reverse the direction of the fading at the ends of the fade:
   if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
   }
   // wait for 30 milliseconds to see the dimming effect
   delay(300);
}

Code to Note

将引脚 9 声明为 LED 引脚后,您的代码的 setup() 函数中无需执行任何操作。您将在代码的主循环中使用的 analogWrite() 函数需要两个参数:一个告诉函数要写入哪个引脚,另一个指示要写入的 PWM 值。

After declaring pin 9 as your LED pin, there is nothing to do in the setup() function of your code. The analogWrite() function that you will be using in the main loop of your code requires two arguments: One, telling the function which pin to write to and the other indicating what PWM value to write.

为了让 LED 淡入淡出,逐渐增加 PWM 值,从 0(完全关闭)到 255(完全开启),然后回到 0,以完成循环。在上面给出的草图中,使用名为亮度的变量设置 PWM 值。每次通过循环,它都会按变量的值 fadeAmount 增加。

In order to fade the LED off and on, gradually increase the PWM values from 0 (all the way off) to 255 (all the way on), and then back to 0, to complete the cycle. In the sketch given above, the PWM value is set using a variable called brightness. Each time through the loop, it increases by the value of the variable fadeAmount.

如果亮度处于其值的任何极端(0 或 255),则 fadeAmount 会变为其负值。换句话说,如果 fadeAmount 为 5,则它将设置为 -5。如果为 -5,则将其设置为 5。下一次通过循环,此更改还会导致亮度改变方向。

If brightness is at either extreme of its value (either 0 or 255), then fadeAmount is changed to its negative. In other words, if fadeAmount is 5, then it is set to -5. If it is -5, then it is set to 5. The next time through the loop, this change causes brightness to change direction as well.

analogWrite() 可以非常快地更改 PWM 值,因此草图末尾的延迟控制了淡出效果的速度。尝试更改延迟的值,看看它如何改变淡出效果。

analogWrite() can change the PWM value very fast, so the delay at the end of the sketch controls the speed of the fade. Try changing the value of the delay and see how it changes the fading effect.

Result

您应该看到 LED 亮度逐渐发生变化。

You should see your LED brightness change gradually.