---
tags: Arduino
---
# Arduino 基礎(LED)輸入輸出
## 一、LED元件介紹
* 發光二極體(Light Emitting Diode,簡稱LED)
* 工作電壓為1.5-2.0V
* 工作電流則為10-20mA
* 當Arduino開發板提供電壓為5V 時,假設LED工作電壓為1.7V,工作電流若設定為15mA,則限流電阻220Ω。
* 計算如下:限流電阻= 電源電壓-LED工作電壓/工作電流= (5-1.7)/0.015 = 220 Ω
* 長腳為陽極,短腳為陰極。
## 二、函式說明
#### (一) pinMode(pin, mode):設定pin腳為數位輸出(OUTPUT)或數位輸入(INPUT)
例如:
pinMode(6, OUTPUT);
pinMode(8, INPUT);
#### (二)digitalWrite(pin, value):設定pin腳的value為HIGH或LOW (數位輸入)
例如:
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
#### (三)analogWrite (pin, value):對pin腳寫入一個類比訊號數值(PWM)
可用來控制LED的亮度或直流馬達的轉速
只能選擇支援PWM的接腳(3, 5, 6, 9, 10, 11pin腳有~ 符號)
PWM的數值介於0(duty cycle = 0%) ~255 (duty cycle =100%)之間
使用analogWrite(),不需再透過pinMode()將其設定為輸出
## (三)實作模擬
#### 1、LED閃爍
##### (1)程式碼
```cpp=
int led = 9;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led, LOW);
delay(1500); // Wait for 1000 millisecond(s)
digitalWrite(led, HIGH);
delay(1500); // Wait for 1000 millisecond(s)
}
```
##### (2)圖示
![](https://i.imgur.com/dZBkXMB.png)
#### 2、LED交替閃爍
##### (1)程式碼
```cpp=
int led1 = 8;
int led2 = 9;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop()
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(1500);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(1500);
}
```
##### (2)圖示
![](https://i.imgur.com/Qf9pIky.png)
#### 3、紅綠燈(綠轉紅時黃燈閃爍3次)
##### (1)程式碼
```cpp=
int ledpin1=8;
int ledpin2=9;
int ledpin3=10;
void setup()
{
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(ledpin3, OUTPUT);
}
void loop()
{
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, HIGH);
digitalWrite(ledpin3, LOW);
delay(3000); // Wait for 1000 millisecond(s)
////////////////第一次////////////////////
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, LOW);
digitalWrite(ledpin3, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(ledpin2, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
////////////////第二次/////////////////////
digitalWrite(ledpin2, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(ledpin2, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
///////////////第三次///////////////////////
digitalWrite(ledpin2, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(ledpin2, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
////////////////////////////////////////////
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, HIGH);
digitalWrite(ledpin3, HIGH);
delay(3000); // Wait for 1000 millisecond(s)
}
```
##### (2)圖示
![](https://i.imgur.com/8jlO79B.png)
#### 4、LED亮度調整 (呼吸燈,從暗到亮在由亮到暗)
##### (1)程式碼
```cpp=
int led = 6;
int b = 0;
int f = 5;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
analogWrite(led, b);
b=b+f;
if(b == 0 ||b == 255){
f= -f;
}
delay(50);
}
```
##### (2)圖示
![](https://i.imgur.com/qONhZh5.png)
#### 5、電燈(按鈕控制LED亮滅)
##### (1)程式碼
```cpp=
int Pin = 2; //按鈕輸入接腳設定為2
int led = 4; // LED燈接腳設定為13
int pinStatus; //讀取按鈕狀態的變數
void setup()
{
pinMode(led, OUTPUT); //將LED接腳設為輸出模式
pinMode(Pin, INPUT); //將按鈕接腳設為輸入模式
}
//迴圈程式
void loop()
{
pinStatus = digitalRead(Pin); //讀取按鈕開關狀態值
if ( pinStatus == LOW) //假如按鈕狀態為LOW,條件成立
{
digitalWrite(led,LOW); //LED燈亮
}
else //否則,按鈕狀態為HIGH
{
digitalWrite(led,HIGH); //LED燈暗
}
}
```
##### (2)圖示
![](https://i.imgur.com/onihzD9.png)
#### 6、電燈亮度調整(可控制電燈亮度)
##### (1)程式碼
```cpp=
int potPin = A0; // 宣告可變電阻輸入接腳
int ledPin = 8; // 宣告LED連接接腳
void setup()
{
Serial.begin(9600); //設定SerialPort的傳輸速率
}
void loop()
{
int sensorValue = analogRead(potPin); //讀取可變電阻值
Serial.println(sensorValue, DEC); //將電阻值列印到SerialPort
sensorValue = sensorValue/4; //將電阻值轉換成0-255範圍內
analogWrite(ledPin, sensorValue); //類比輸出LED亮度
delay(100);
}
```
##### (2)圖示
![](https://i.imgur.com/KwACA4v.png)
#### 7、RBG LED(暴力法)
##### (1)程式碼
```cpp=
int redPin=9;
int greenPin = 10;
int bluePin = 11;
void setup(){
pinMode(redPin, OUTPUT); //設定紅色LED接腳
pinMode(greenPin, OUTPUT); //設定綠色LED接腳
pinMode(bluePin, OUTPUT); //設定藍色LED接腳
}
void loop()
{
analogWrite(redPin,0); // 全暗
analogWrite(greenPin,0);
analogWrite(bluePin,0);
delay(1000);
analogWrite(redPin,255); // 紅
analogWrite(greenPin,0);
analogWrite(bluePin,0);
delay(1000);
analogWrite(redPin,0); // 綠
analogWrite(greenPin,255);
analogWrite(bluePin,0);
delay(1000);
analogWrite(redPin,0); // 藍
analogWrite(greenPin,0);
analogWrite(bluePin,255);
delay(1000);
analogWrite(redPin,255); // 白
analogWrite(greenPin,255);
analogWrite(bluePin,255);
delay(1000);
}
```
##### (2)圖示:
![](https://i.imgur.com/uZ43R6w.png)
#### 8、彩虹燈(副程式方法)
##### (1)程式碼
```cpp=
int redPin = 11; //宣告紅色LED接腳
int greenPin = 9; //宣告綠色LED接腳
int bluePin = 10; //宣告藍色LED接腳
void setup()
{
pinMode(redPin, OUTPUT); //設定紅色LED接腳
pinMode(greenPin, OUTPUT); //設定綠色LED接腳
pinMode(bluePin, OUTPUT); //設定藍色LED接腳
}
void loop()
{
setColor(255, 0, 0); // 紅
delay(3000);
setColor(255, 97, 0); // 橙
delay(3000);
setColor(255, 255, 0); // 黃
delay(3000);
setColor(0, 255, 0); // 綠
delay(3000);
setColor(0, 0, 255); // 藍
delay(3000);
setColor(8, 46, 84); // 靛
delay(3000);
setColor(160, 33, 240); // 紫
delay(3000);
}
//顏色調配副程式
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
```
##### (2)圖示
![](https://i.imgur.com/rTE5ZTe.png)