ITPub博客

首页 > 应用开发 > IT综合 > Arduino例子----Fading LED

Arduino例子----Fading LED

原创 IT综合 作者:lixinli 时间:2014-08-19 10:12:26 0 删除 编辑

1. 功能描述

以逐渐点亮和关闭一个LED的例子,演示analogWrite()函数的应用。AnalogWrite使用PWM(脉宽调频)非常快速的打开、关闭一个数字针脚,以此创建一种渐变的效果。

 

2. 需要硬件

ü  Arduino 板子

ü  一个LED灯

ü  一个220欧姆电阻

ü  面包板

 

3. 电路连接图

通过一个220欧姆电阻把LED灯的阳极(即长针脚)链接到3号针脚(支持PWM),LED的阴极(即短针脚)链接到地。



4. 原理图

5. 程序

/*

  Fading

 this example shows how to fade an LED on PWM pin 3

 using the analogWrite() function.

  模拟电压的输入范围0---5V对应的表示范围是 0---1023

  模拟信号映射为数字信号的范围为0----255

 */

 

int ledPin = 3;

int brightness = 0;    // how bright the LED is

int fadeAmount = 5;    // how many points to fade the LED by

 

void setup()

{

  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);

}

 

void loop()

{

  Serial.println(brightness);

  analogWrite(ledPin, brightness);

  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade

  if(brightness == 0 || brightness == 255)

  {

    fadeAmount = -fadeAmount;

  }

  delay(30);  // wait for 30 milliseconds to see the dimming effect

}

6. 实验照片




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/263104/viewspace-1253665/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-05-11

  • 博文量
    11
  • 访问量
    61376