SIK Experiment Guide for the Arduino 101/Genuino 101 Board (Chinese)

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

Pages
Contributors: D___Run___
Favorited Favorite 1

实验 3:驱动 RGB LED

简介

您知道甚至比闪烁的 LED 更加有趣的东西么?使用一个 LED 更改颜色。在此电路中,您将学习如何使用 RGB LED 创建独一无二的颜色组合。根据每个二极管的亮度,几乎可以创造出任何颜色!

所需部件

您需要以下部件:

  • 1x 实验电路板
  • 1x Arduino 101 或 Genuino 101 开发板
  • 1x 共阴极 RGB LED
  • 3x 100Ω 电阻
  • 6x 跳线

没有 101 SIK?

如果您要执行本实验,但又没有 SIK,那么我们建议您使用这些部件:

Breadboard - Self-Adhesive (White)

Breadboard - Self-Adhesive (White)

PRT-12002
$5.50
48
Jumper Wires - Connected 6" (M/M, 20 pack)

Jumper Wires - Connected 6" (M/M, 20 pack)

PRT-12795
$2.10
2
LED - RGB Clear Common Cathode

LED - RGB Clear Common Cathode

COM-00105
$2.25
3

Resistor 100 Ohm 1/4th Watt PTH - 20 pack

COM-13761
Retired

您还需要一个 Arduino 101 Genuino 101 开发板。

Arduino 101

DEV-13787
9 Retired

Genuino 101

DEV-13984
Retired

红绿蓝 (RGB) LED 简介

替换文本

红绿蓝 (RGB) LED 是一体式的 3 个 LED。RGB 具有四个针脚,三个较短针脚中的每个针脚用于控制一种颜色:红色、绿色或蓝色。RGB 的较长针脚是通用接地针脚。您可以打开和关闭不同颜色以组合它们,从而创建自定义彩色 LED。例如,如果打开红色针脚和绿色针脚,则 RGB 会呈黄色亮起。

但是哪个针脚表示哪种颜色呢?拿起 RGB,以便最长针脚(通用接地)向左对齐,如下图所示。从最左侧开始,针脚分别是红色、接地、绿色和蓝色。

注:对 RGB 进行接线时,每个彩色针脚仍需要与您计划用于控制它的 101 开发板针脚并联一个限流电阻,如同任何标准 LED 一样。

硬件接线

是否准备好开始对每个部件进行接线?请查看下面的接线图和接线表以了解如何连接每个部件。

请特别注意组件用于指示如何在实验电路板上放置它的标记。极化组件只能按一个方向连接到电路。在下表中,极化组件使用黄色警告三角突出显示。

实验的接线图

替换文本

难以看清电路?单击接线图可放大查看。

打开草图

在计算机上打开 Arduino IDE 软件。采用 Arduino 语言进行编码可控制电路。通过访问您先前下载并置于“示例”文件夹中的“101 SIK 指南代码”,来打开用于电路 3 的代码。

要打开该代码,请转到:文件 > 示例 > 101 SIK 指南代码 > Circuit_03

还可以将以下代码复制并粘贴到 Arduino IDE 中。点击“上传”,然后查看发生的情况!

language:cpp
/*
SparkFun Inventor's Kit
Example sketch 03

RGB LED

Make an RGB LED display a rainbow of colors!

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.
*/

//create variables for pin numbers. We are making them constants here, because they       //never change.
const int RED_PIN = 5;
const int GREEN_PIN = 6;
const int BLUE_PIN = 9;

// How fast we plan to cycle through colors in milliseconds
int DISPLAY_TIME = 10;  

void setup()
{
//set the three pin variables as outputs
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}

void loop()
{
// We've written a custom function called mainColors() that steps
// through all eight of these colors. We're only "calling" the
// function here (telling it to run). The actual function code
// is further down in the sketch.
mainColors();

}

// Here's the mainColors() custom function we've written.
void mainColors()
{
// Off (all LEDs off):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);

//wait 1 second
delay(1000);

// Red (turn just the red LED on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);

//wait 1 seconds
delay(1000);

// Green (turn just the green LED on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);

//wait 1 second
delay(1000);

// Blue (turn just the blue LED on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);

//wait 1 second
delay(1000);

// Yellow (turn red and green on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);

//wait 1 second
delay(1000);

// Cyan (turn green and blue on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);

//wait 1 second
delay(1000);

// Purple (turn red and blue on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);

//wait 1 second
delay(1000);

// White (turn all the LEDs on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);

//wait 1 second
delay(1000);
}

要注意的代码

language:cpp
for (x = 0; x < 768; x++)
{}

for() 循环用于在某个范围内按设置的次数重复执行操作,并重复运行括号 {} 中的代码。此处的变量“x”从 0 开始,以 767 结束,每次增加一(“x++”)。

language:cpp
if (x <= 255)
{}
else
{}

“If/else”语句用于在程序中进行选择。括号 () 中的语句会进行计算;如果它为 true,则第一对括号 {} 中的代码会运行。如果它不是 true,则第二对括号 {} 中的代码会运行。

应看到的情况

您应看到 LED 亮起,但是这次是让人眼前一亮的新颜色!如果它未亮起,请确保正确装配了电路以及验证了代码并将它上传到开发板,或查看“故障诊断”部分。

替换文本

故障诊断

LED 不亮或显示不正确的颜色

由于 LED 的四个针脚相距很近,因此有时容易弄错位置。仔细检查每个针脚是否都处于应处的位置。

看到红色

RGB LED 中的红色二极管可能比其他两个二极管要更亮一些。要使颜色更加平衡,请使用欧姆值更高的电阻。