九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 5121|回复: 0
打印 上一主题 下一主题

arduino ibox项目实战8-PWM实现LED灯渐变实验

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-25 18:03:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,串联有电阻的LED灯外设一个
软件平台:WIN7操作系统,android4.0android4.4系统
打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->DigitalReadSerial,DigitalReadSerial的示例程序将会被打开,其源码如下:
  1. int led = PWM0;           // the pin that the LED is attached to PWM0 = 5.
  2. int brightness = 0;    // how bright the LED is
  3. int fadeAmount = 80;    // how many points to fade the LED by

  4. // the setup routine runs once when you press reset:
  5. void setup() {
  6.   // declare pin 5(PWM0) to be an output:
  7.   pinMode(led, OUTPUT);
  8. }

  9. // the loop routine runs over and over again forever:
  10. void loop() {
  11.   // set the brightness of pin 5(PWM0):
  12.   analogWrite(led, brightness);

  13.   // change the brightness for next time through the loop:
  14.   brightness = brightness + fadeAmount;

  15.   // reverse the direction of the fading at the ends of the fade:
  16.   if (brightness <= 0 || brightness >= 4095) {
  17.     fadeAmount = -fadeAmount ;
  18.   }
  19.   // wait for 30 milliseconds to see the dimming effect
  20.   delay(30);
  21. }
复制代码
       从源码可以看出,程序通过pwm0口控制LED灯,通过analogWrite函数的传入参数brightness的渐变,实现PWM脉宽的调制,最终实现LED灯的渐亮渐灭的效果。
       LED灯外设的负极接到PWM0口,正级接到3.3V电源口,运行arduino,可以发现,LED灯确实实现了渐亮渐灭的效果。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-6-14 18:23 , Processed in 0.019002 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表