PWM - Let LED to fade and light in turn
Materials
- Breadboard x 1
- Ameba x 1
- LED x 1
- 1 KΩ Resistor x 1
Example
In this example, we use the PWM component to fade and light the LED in turn. A PWM component uses digital signal to simulate analog signal. By adjusting the duration of 3V3 and 0V, PWM produces the average voltage output with different value.
For example, in the figure below, as the proportion of 3v3 in a duration drops, the average voltage output decreases:
Therefore, we use the varying of average voltage output of PWM to make the LED to fade and light alternately.
Open the sample code in “File” -> “Examples” -> “01. Basics” -> “Fade”:
In this example, we use pin D9. Please refer to the wiring diagram:
Below is the RTL8710 Wiring Diagram:
上传程式码之后,按下Ameba的Reset按钮,就可以看到LED灯泡随着时间渐暗与渐亮。
Afterwards, upload the sample code and press the reset button. Then you can see the LED fades and lights alternately.
Code Reference
https://www.arduino.cc/en/Tutorial/Fade
And the documentation of PWM on the Arduino website:
https://www.arduino.cc/en/Tutorial/PWM
In Setup, we use pinMode(pin, mode) to set D9 to output mode:
https://www.arduino.cc/en/Reference/PinMode
and use analogWrite(pin, value) to set output frequency (0~255):
https://www.arduino.cc/en/Reference/AnalogWrite