# Lesson 4:PWM [TOC] ## PWM * **PWM**( Pulse Width Modulation)是指調整數位訊號中脈衝工作週期( Duty Cycle),藉以模擬類比訊號的輸出。 ![](https://www.arduino.cc/en/uploads/Tutorial/pwm.gif =400x) * **函式介紹** * `analogWrite(pin, value)` : 設定 'pin' 腳輸出PWM訊號,value介於0~255(0 = 0% , 255 = 100% ) * `analogRead(pin)` : 讀取 'pin' 腳的類比輸入(7697 的ADC為P14\~P17,也可以用A0\~A4表示。解析度為12-bit(0~4095)) `value = analogRead(pin); //設定 'value' 為 'pin' 腳輸入的類比訊號` :::warning 設定成 'analog' 的pin腳不需要額外指定為INPUT/OUTPUT mode ::: **實作**:撰寫 PWM 程式去控制外接 LED 漸強、漸弱(需 LED、220Ω 電阻) > analogWrite(), delay() ```c++ int ledPin = 13; void setup(){} void loop() { for (int i=0; i<=255; i+=5) //電壓漸強 { analogWrite(ledPin, i); //輸出PWM訊號 delay(100); } for (int i=255; i>=0; i-=5) //電壓漸弱 { analogWrite(ledPin, i); delay(100); } } ``` ## 可變電阻接法 ![](https://i.imgur.com/wfvxmOH.jpg)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up