Arduino周边模块:LED部件
Arduino周边模块:LED部件
Arduino周边模块:LED部件
1. LED的使用
LED的原理:
LED是会发光的二极管,它具有单向导电性。两端加上正向电压,即能将电能转化为光能。
正向电压就是正极加高电压,负极加低电压
对于LED的正负极判断:
- 一般长引脚的是正极,短引脚的是负极。
- 观察LED的头部,里面有一宽一窄两个金属块,一般窄的金属块连接的引脚是正极,宽的金属块连接的引脚是负极。
数字电平:
电压的另一种解读方式
高电平对应数字逻辑的1,低电平对应数字逻辑的0
Arduino的数字端口
(在数字端口不够用的情况下,模拟端口也能够充当数字端口使用)
Arduino的数字端口有两种模式:输入、输出
电路连接
我们将LED的负极连接到Arduino的八号端口,然后通过一个限流电阻,将LED的负极连接到Arduino的GND端口
面包板
面包板能够提供一个宽松的实验环境。元件直接插拔,无需焊接。很适合电子电路的组装、调试和训练。
面包板分为三个区域,上下两个区域是横向贯通的,中间的区域是纵向贯通的
Arduino控制程序的一般结构
1.void setup(){
2. //在此做一些准备工作
3.}
4.
5.void loop(){
6. //在此实现应用程序的功能
7.}
进入Arduino IDE,正式编写代码
1.void setup(){
2. pinMode(8,OUTPUT);
3.}
4.
5.void loop(){
6. digitalWrite(8,HIGH);
7. delay(1000);
8. digitalWrite(8,LOW);
9. delay(1000);
10.}
LED以每秒一次的频率闪烁着,delay()函数的单位是毫秒。
优化:
1.int led=8;
2.void setup(){
3. pinMode(led,OUTPUT);
4.}
5.
6.void loop(){
7. digitalWrite(led,HIGH);
8. delay(1000);
9. digitalWrite(led,LOW);
10. delay(1000);
11.}
Arduino数字IO相关库函数
| 函数原型 | 函数说明 |
|---|---|
| pinMode(pin,mode) | 配置特定引脚的工作模式 |
| digitalWrite(pin,value) | 向特定引脚输出数字电平 |
| delay(ms) | 产生一段固定时长的延时 |
2. LED点阵的使用
我们在街上看到的LED广告屏就是使用多个LED点阵拼接起来的
LED点阵原理图
LED点阵显示图形
关键字:
- 行扫描
- 视觉暂留
而计算机显示字符的原理,其实就是计算机中存储的字形码,字形码的作用就是记录字符的点阵数据
那么如何在Arduino程序中存储字库?
使用数组
数组是同一类型数据的集合
硬件连接
程序的框架图
1.//2-dimensional array of row pin numbers:
2.const int row[8]={
3.17,16,2,3,4,5,6,7};
4.
5.//2-dimensional array of column pin numbers:
6.const int col[8]={
7.8,9,10,11,12,13,19,18};
8.
9.//2-dimensional array of pixels:
10.const byte character_bank[2][8]={
11. {B00111100,
12. B01000010,
13. B01000010,
14. B01000010,
15. B01000010,
16. B01000010,
17. B01000010,
18. B00111100},
19.
20. {B00010000,
21. B00110000,
22. B00010000,
23. B00010000,
24. B00010000,
25. B00010000,
26. B00010000,
27. B00111000}
28.};
29.
30.int pixels[8][8];
31.
32.void setup(){
33. //initialize the I/O pins as output
34. for (int thisPin=0;thisPin<8;thisPin++){
35. //initialize the output pins:
36. pinMode(col[thisPin],OUTPUT);
37. pinMode(row[thisPin],OUTPUT);
38. //take the col pins(i.e. the cathodes) high to ensure that
39. //the LEDS are off:
40. digitalWrite(col[thisPin],HIGH);
41. }
42. //initialize the pixel matrix:
43. for (int x=0;x<8;x++){
44. for (int y=0;y<8;y++){
45. pixels[x][y]=HIGH;
46. }
47. }
48.}
49.
50.void loop(){
51. //draw the screen:
52. displayNum(1);
53. refreshScreen();
54.}
55.
56.void displayNum(int num)
57.{
58. for (int rowindex=0;rowindex<8;rowindex++)
59. {
60. for (int colindex=0;colindex<8;colindex++){
61. pixels[rowindex][colindex]=((character_bank[num][rowindex]>>(7-colindex))&0x01)==07HIGH:LOW;
62. }
63. }
64.}
65.
66.void refreshScreen(){
67. //iterate over the rows (anodes):
68. for (int thisRow=0;this Row<8;thisRow++){
69. //take the row pin (anode) high:
70. digitalWrite(row[thisRow],HIGH);
71. //iterate over the cols (cathodes):
72. for (int thisCol=0;thisCol<8;thisCol++){
73. //get the state of the current pixel;
74. int thisPixel=pixels[thisRow][thisCol];
75. //when the row is HIGH and the col is LOW,
76. //the LED where they meet turns on:
77. digitalWrite(col[thisCol],thisPixel);
78. //turn the pixel off:
79. if (thisPixel==LOW){
80. digitalWrite(col[thisCol],HIGH);
81. }
82. }
83. //take the row pin low to turn off the whole row:
84. digitalWrite(row[thisRow],LOW);
85. }
86.}
3. 三色LED的使用
加到各个引脚上的电压大小决定了相应分量的亮度,进而决定了混合而成的光的颜色
那么用什么来输出模拟电压呢?
PWM :PWM是用占空比(duty cycle)不同的方波,来模拟“模拟输出”的一种方式
Arduino中端口标号前有’~’的均可以输出PWM波
Arduino控制程序中PWM波的输出
analogWrite(pin,value)
第一个参数pin代表引脚标号
第二个参数value代表占空比的值。取值范围:0~255
总共有三个LED,因此我们可以有255*255*255=16581375个颜色
硬件连接
程序
1.//端口配置
2.int redPin=3;
3.int greenPin=5;
4.int bluePin=6;
5.
6.void setup()
7.{
8. pinMode(redPin,OUTPUT);
9. pinMode(greenPin,OUTPUT);
10. pinMode(bluePin,OUTPUT);
11.}
12.
13.void loop()
14.{
15. int red,green,blue;
16. blue=0;
17. for(blue=0,red=255;blue<256;blue++)
18. {
19. setColor(red,green,blue);
20. delay(4);
21. red--;
22. }
23. delay(100); //绿色向红色渐变
24.
25. green=0;
26. for(red=0,green=255;red<256;red++)
27. {
28. setColor(red,green,blue);
29. delay(4);
30. green--;
31. }
32. delay(100); //红色向篮色渐变
33.
34. red=0;
35. for(green=0,blue=255;green<256;green++)
36. {
37. setColor(red,green,blue);
38. delay(4);
39. blue--;
40. }
41. delay(100); //篮色向绿色渐变
42.}
43.
44.void setColor(int red,int green,int blue)
45.{
46. analogWrite(redPin,red);
47. analogWrite(greenPin,green);
48. analogWrite(bluePin,blue);
49.}
Arduino周边模块:LED部件的更多相关文章
- Arduino周边模块:传感器部件(温敏、光敏、湿敏)
Arduino周边模块:传感器部件(温敏.光敏.湿敏) Arduino周边模块:传感器部件(温敏.光敏.湿敏) Arduino的模数转换 对于Arduino来说,它只认识数字量,模拟量对其来说就是一门 ...
- Arduino周边模块:执行部件(舵机、直流电机、步进电机)
Arduino周边模块:执行部件 Arduino周边模块:执行部件 嵌入式系统的构成 如今已经有各种各样的基于Arduino的嵌入式系统, 比如:智能小车.3D打印机.机器人,甚至还有基于Arduin ...
- Arduino周边模块:LCD与数码管
Arduino周边模块:LCD与数码管 Arduino周边模块:LCD与数码管 数码管的介绍 数码管一般是用来显示数字和字符的 数码管原理 一位数码管 该图是一个8段数码管,该数码管中包含了8个LED ...
- 【Arduino】开发入门【十】Arduino蓝牙模块与Android实现通信
[Arduino]开发入门[十]蓝牙模块 首先show一下新入手的蓝牙模块 蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机 ...
- Arduino 翻译系列 - LED 灯闪烁
原文地址 - https://www.arduino.cc/en/Tutorial/Blink 闪烁 这个例子展示了你能拿 Arduino / Genuino 板子来干的最简单的事:使开发板上的 LE ...
- Arduino 各种模块篇 蓝牙模块 手机蓝牙控制Arduino LED灯
解决方案. 条件: 1.手机android 商店下载 blueTerm 2.向arduino中载入如下代码: char val; ; void setup() { Serial.begin(); pi ...
- Arduino 各种模块篇 RGB LED灯
示例代码: 类似与这样的led,共阴rgb led,通过调节不同的亮度,组合成不同的颜色. 示例代码: /* 作者:极客工坊 时间:2012年12月18日 IDE版本号:1.0.1 发布地址:www. ...
- Arduino蓝牙模块实现通信
蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机可通过该脚状态判断蓝牙是否已经连接 2.led指示蓝牙连接状态,闪烁表示没有 ...
- Arduino 各种模块篇 摇杆模块
Arduino的另外几种模块,我们常见的joystick摇杆模块. 用起来很爽,摇杆 有X,Y轴可调 这里有一篇非常想尽的示例代码: http://www.geek-workshop.com/foru ...
随机推荐
- 【floyd存字典序路径】【HDU1385】【Minimum Transport Cost】
题目大意 求多组i到j的最短路径 并输出字典序最小.... 现在只会floyd的方式 利用dis[i][j] 表示i到j的路径中i 后面的节点 更新是比较dis[i][j] dis[i][k]. 记住 ...
- Open Replicator
Open Replicator ( http://code.google.com/p/open-replicator/ ) 开源了.Open Replicator是一个用Java编写的MySQL bi ...
- 使用FileSystemWatcher捕获系统文件状态
源代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- JQ对JSON的增删改
var userlist={ }, } } //方法一 userlist.row1.sex="女";//添加 userlist.row3={name:};//添加 userlist ...
- JS判断终端浏览器类型
根据终端浏览器类型不懂加载不同的JS或CSS文件 <script> var browser = { versions: function () { var u = navigator.us ...
- 【JS】导出table到excel,同时兼容FF和IE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js cookies存取删操作实例
//写cookies函数 function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值 { var Days = 30; //此 cookie 将被保存 ...
- linux里的bootproto的none,static,dhcp有什么区别
这个是网络配置参数:BOOTPROTO=static 静态IP BOOTPROTO=dhcp 动态IP BOOTPROTO=none 无(不指定)通常情况下是dhcp或者static,通过指定方式 ...
- 处理json中的异常字符
在很多场景中需要通过json传递数据,如果json中包含英文的",""'"之类的字符,会导致json解析失败 可以用一些在线的json格式检查网站检查是否含有异 ...
- 可爱的 Python : Python中函数式编程,第二部分
英文原文:Charming Python: Functional programming in Python, Part 2,翻译:开源中国 摘要: 本专栏继续让David对Python中的函数式编 ...