材料準備
範例說明
Fade這個example裡,使用PWM讓LED燈泡漸暗與漸亮。PWM是利用數位訊號模擬成類比訊號的方式,它在一段時間內,藉由調整3V3與0V出現的時間比例,達到輸出不同的平均電壓。
以下圖為例,隨著3V3出現的比例降低,平均電壓也跟著降低:
於是我們利用PWM,讓LED因為平均電壓的改變而達到漸暗與漸亮的變化。
要使用這個範例,請從 “File” -> “Examples” -> “01. Basics” -> “Fade” 打開這個 example:
這個範例裡,使用的Pin是D9, 所以我們這樣接線:
上傳程式碼之後,按下Ameba的Reset按鈕,就可以看到LED燈泡隨著時間漸暗與漸亮。
以下圖為例,隨著3V3出現的比例降低,平均電壓也跟著降低:
於是我們利用PWM,讓LED因為平均電壓的改變而達到漸暗與漸亮的變化。
要使用這個範例,請從 “File” -> “Examples” -> “01. Basics” -> “Fade” 打開這個 example:
這個範例裡,使用的Pin是D9, 所以我們這樣接線:
上傳程式碼之後,按下Ameba的Reset按鈕,就可以看到LED燈泡隨著時間漸暗與漸亮。
程式碼說明
我們使用的是與Arduino同一份程式碼,所以可以參考Arduino的Fade範例文件:
https://www.arduino.cc/en/Tutorial/Fade
以及Arduino的PWM參考文件:
https://www.arduino.cc/en/Tutorial/PWM
在Setup裡面, 使用pinMode決定D9為output:
https://www.arduino.cc/en/Reference/PinMode
使用analogWrite決定輸出的頻率(0~255):
https://www.arduino.cc/en/Reference/AnalogWrite
https://www.arduino.cc/en/Tutorial/Fade
以及Arduino的PWM參考文件:
https://www.arduino.cc/en/Tutorial/PWM
在Setup裡面, 使用pinMode決定D9為output:
https://www.arduino.cc/en/Reference/PinMode
使用analogWrite決定輸出的頻率(0~255):
https://www.arduino.cc/en/Reference/AnalogWrite
Materials
Procedure
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:
Afterwards, upload the sample code and press the reset button. Then you can see the LED fades and lights alternately.
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:
Afterwards, upload the sample code and press the reset button. Then you can see the LED fades and lights alternately.
Code Reference
You can find detailed information of this example in the documentation of Arduino:
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
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



0 comments:
Post a Comment