## **LED亮度渐隐**
> 本例向你展示模拟信号的输出,通过**脉宽调制技术**(**PWM**)让LED亮度渐弱。PWM技术会快速地开关一个数字引脚,通电的时间和断电的时间按照一定的比例分配,通过这来让输出所谓的“模拟信号”,这种方式并非是直接输出模拟信号,而是通过脉宽调制来模拟。
### **所需硬件**
* Arduino板Genuino板
* LED
* 220Ω电阻
* 跳线
* 面包板
* 杜邦线/面包板线
* 电路搭建
* LED连接通过220Ω电阻连接到9号引脚。
### **电路搭建**
![图片来自官网](http://img.blog.csdn.net/20160511124945948)
### **原理图**
![图片来自官网](http://img.blog.csdn.net/20160511124909556)
### **代码**
在这个例子中,有两个循环,一个逐渐增加施加在9号引脚上的PWM值,一个逐渐减小施加在9号引脚上的PWM值。
~~~
/*
LED亮度渐隐
本例展示analogWrite()函数的使用。
电路搭建:
* LED连接9号引脚和GND
代码是公开的
*/
int ledPin = 9; //LED连接到9号引脚
void setup() {
// 什么都不做
}
void loop() {
//步长为5,从小到大:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// 设置数值 (0到255):
analogWrite(ledPin, fadeValue);
// 等待30毫秒,来让人看清效果。
delay(30);
}
// 步长为-5,从大到小:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// 设置数值 (0到255):
analogWrite(ledPin, fadeValue);
// 等待30毫秒,来让人看清效果
delay(30);
}
}
~~~
### **相关资料**
[for()](https://www.arduino.cc/en/Reference/For)
[analogWrite()](https://www.arduino.cc/en/Reference/AnalogWrite)
[delay()](https://www.arduino.cc/en/Reference/Delay)
- 说明
- 系统示例文件目录结构及说明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (与特定硬件相关,暂无)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暂无)
- ArduinoISP