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 ...
随机推荐
- GetBuffer与ReleaseBuffer的用法,CString剖析
转载: http://blog.pfan.cn/xman/43212.html GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象 ...
- sublime常用快捷键整理(未完待续)
sublime常用快捷键整理: 基本操作 cmd+o 打开文件 cmd+w 关闭当前tab cmd+n 打开新页 cmd+shift+n 打开刚刚关闭的页签 一.选择命令 1.多个单词选择 cmd+d ...
- asp.net cookie的使用.
Cookie就是服务器暂时存放在你的电脑里的资料(.txt格式的文本文件),好让服务器用来辨认你的计算机.当你在浏览网站的时候,Web服务器会先送一小小资料放在你的计算机上,Cookies 会帮你在网 ...
- Knime 使用 初探
使用数据挖掘工具 Knime,分析某公司用户使用情况. 首先,打开csv文件数据,看到有以下门类: 时间.track id 歌曲名.用户行为.用户id.日期.snap_id 即歌曲门类 然后,打开Kn ...
- IQueryable与IEnumberable的区别(转)
转自 http://www.cnblogs.com/fly_dragon/archive/2011/02/21/1959933.html IEnumerable接口 公开枚举器,该枚举器支持在指定类型 ...
- ORACLE 查询表定义
很多文章使用DESC tablename查看,这是查看的表结构,不是表定义. 如下: 1.set long 99999; --增大输出缓冲区 2.SELECT dbms_metadata.get_d ...
- 使用 hibernate 存取大对象数据类型(clob和blob)
数据库表如下: book表 id 该表的主键.number类型. photo 代表图书的图片,blob类型. description 图书的描述,clob类型. 使用 hibernate3 往 boo ...
- C#4 for循环 迭代法 穷举法应用
for()循环. 四要素: 初始条件,循环条件,状态改变,循环体. 执行过程: 初始条件--循环条件--循环体--状态改变--循环条件.... 注意:for的小括号里面分号隔开,for的小括号后不要加 ...
- UNIX环境高级编程--#include "apue.h"
apue.h头文件为作者自己编写而非系统自带,故需要自行添加! 第一:打开网站 http://www.apuebook.com/第二:选择合适的版本(一共有三个版本,根据书的版本选择)下载源码sour ...
- dependency injection via inversion of control
依赖注入DI是一个程序设计模式和架构模型, 一些时候也称作控制反转,尽管在技术上来讲, 依赖注入是一个IOC的特殊实现, 依赖注入是指一个对象应用另外一个对象来提供一个特殊的能力, 例如:把一个数据库 ...