---
# System prepended metadata

title: 高英_物聯網實務課程
tags: [arduino, 物聯網實作課程, C]

---

---
title: 高英_物聯網實務課程
tags: C,arduino,物聯網實作課程
lang: zh
---
# 工商物聯網實務課程
###### tags: C#,Arduino,IoT

## 課堂重要分享網址
1. <a href="https://www.tinkercad.com/circuits">Arduion 模擬網址</a>

## 基礎練習-2021/03/08
### 練習一:利用三用電表測試Arduino輸出電壓

1. 利用標準電位量出5V
![](https://i.imgur.com/K5ZWHic.png)

2. 利用電位差的概念量出1.7V
![](https://i.imgur.com/tQVqM43.png)

### 練習二:Arduino 與 LED

1. 常見錯誤接法
![](https://i.imgur.com/0jFkIKz.png)
:::danger
LED 必須跨接電組，否則很容易燒毀
:::

2. 正確的接法
![](https://i.imgur.com/Mv0BMib.png)
:::info
LED 的亮度取決於通過的電流，可使用電組來進行控制
:::

### 練習三:利用三用電表量測電路上的電壓與電流

![](https://i.imgur.com/IZoYzNQ.png)

:::info
量測電壓需使用並聯，量測電流需使用串聯
:::

### 練習四:分壓電路實作
1. 利用固定電阻實作分壓電路

![](https://i.imgur.com/mrliZNZ.png)

2. 利用可變電阻實作分壓電路

![](https://i.imgur.com/7KPoRuF.png)

### 練習五:正式進入Arduino - Hello World
在模擬軟體上練習如何顯示出Hello World
```c=
void setup()
{
	Serial.begin(9600);
}

void loop()
{
	Serial.println("Hello World");
  	delay(5000);
}
```

:::info
注意: loop 建議加上 delay 函數，避免程式執行太快。
:::

## 基礎練習-2021/03/15

### 練習一:利用程式控制LED燈亮燈
![](https://i.imgur.com/5sGfi7O.png)
```c=
void setup()
{
	Serial.begin(9600);
  	pinMode(2,OUTPUT);
}

void loop()
{
	digitalWrite(2, HIGH); 
  	delay(5000);
}
```

:::info
:bulb:課堂練習:如果希望 Digital 針腳為 LOW 才會亮燈，該如何設計?
:::

:::spoiler 參考接法
![](https://i.imgur.com/CWlCTEG.png)
:::

### 練習二:利用程式控制LED燈閃燈
```c=
void setup()
{
	Serial.begin(9600);
  	pinMode(2,OUTPUT);
}

void loop()
{
	digitalWrite(2, LOW); //LED ON
  	delay(1000);
  	digitalWrite(2, HIGH); //LED OFF
  	delay(1000);
}
```
:::info
:book: 嘗試變更delay的時間，看會發生什麼有趣的事情。
:::

### 練習三:利用程式實作簡易紅綠燈控制系統
![](https://i.imgur.com/oHca0ZN.png)
```c=
void setup()
{
    Serial.begin(9600);
    pinMode(2,OUTPUT);
    pinMode(3,OUTPUT);
    pinMode(4,OUTPUT);
}

void loop()
{
    //亮紅燈，關黃燈
    digitalWrite(3, LOW);
    digitalWrite(2, HIGH);
    delay(5000);

    //亮綠燈，關紅燈
    digitalWrite(2, LOW);
    digitalWrite(4, HIGH);
    delay(5000);

    //亮黃燈，關綠燈
    digitalWrite(4, LOW);
    digitalWrite(3, HIGH);
    delay(2000);
}
```
:::info
:ice_skate: 如何利用1顆電阻來達到相同的目的呢?
:::

### 練習四:共陰與共陽線路設計
#### 共陰線路
![](https://i.imgur.com/yZgfN6Z.png)

#### 共陽線路
![](https://i.imgur.com/rTWNFsT.png)
:::spoiler 共陽線路參考程式碼
```c=
void setup()
{
	Serial.begin(9600);
  	pinMode(2,OUTPUT);
    pinMode(3,OUTPUT);
    pinMode(4,OUTPUT);
  	digitalWrite(2, HIGH);
  	digitalWrite(3, HIGH);
  	digitalWrite(4, HIGH);
}

void loop()
{
  	//亮紅燈，關黃燈
  	digitalWrite(3, HIGH);
	digitalWrite(2, LOW);
  	delay(5000);
  
  	//亮綠燈，關紅燈
  	digitalWrite(2, HIGH);
 	digitalWrite(4, LOW);
  	delay(5000);
  
  	//亮黃燈，關綠燈
  	digitalWrite(4, HIGH);
  	digitalWrite(3, LOW);
  	delay(2000);
}
```
:::
### 練習五:利用共陰/共陽實作RGB-LED燈
![](https://i.imgur.com/3lFhd5m.png)
:::info
:balloon: 利用色彩的三原色可以自由變換各式各樣的顏色
:::
```c=
void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
}
```
:::info
:baby: 除了上述的顏色之外，是否可以變化出更多的色彩呢?
:::

## 基礎練習-2021/03/22
### 練習一:熟悉PWM訊號
:::success
:question: 目前熟悉的數位輸出都是5V，我該如何調整數位輸出的電壓值呢?
:::

#### 初探PWM訊號
為了改變輸出電壓值，我們嘗試的改變HIGH與LOW的時間，並在極短的時間內切換來達到模擬電壓值得效果
![](https://i.imgur.com/4ACAbIX.png)
```c=
void setup()
{
  pinMode(5, OUTPUT);
}

void loop()
{
  digitalWrite(5, HIGH);
  delay(4);
  digitalWrite(5, LOW);
  delay(1);
}
```
:::success
:couch_and_lamp: 利用delayMicroseconds()函數可以將訊號控制在微秒等級
:::

#### 利用時序來模擬PWM輸出
![](https://i.imgur.com/ZBzYlA0.png)

```c=
void setup()
{
  pinMode(3, OUTPUT);	
  pinMode(5, OUTPUT);
  Serial.begin(9600);
}

long time = micros();
void loop()
{
	time = micros() % 5000;
  
  	if(time > 0 && time < 2000)
    {
    	digitalWrite(3,HIGH);
    }
  
    if(time > 0 && time < 3000)
    {
    	digitalWrite(5,HIGH);
    }
  
  	if(time > 2000 && time < 5000)
    {
    	digitalWrite(3,LOW);
    }
  
  	if(time > 4000 && time < 5000)
    {
    	digitalWrite(5,LOW);
    }
}
```


#### 利用analogWrite來實現PWM輸出(建議使用)
```c=
void setup()
{
  pinMode(3, OUTPUT);	
  pinMode(5, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
	analogWrite(3,102);
  	analogWrite(5,205);
  	delay(10);
}
```
### 練習二:利用PWM訊號控制三色LED燈
```c=
void setup()
{
  pinMode(3, OUTPUT); //R
  pinMode(5, OUTPUT); //G
  pinMode(6, OUTPUT); //B
  
  pinMode(5, OUTPUT);
}

void loop()
{
	analogWrite(3,58);
  	analogWrite(5,121);
  	analogWrite(6,166);
}
```

### 練習三:利用可變電阻控制三色LED
![](https://i.imgur.com/M73WR46.png)
```c=
void setup()
{
  pinMode(3, OUTPUT); //R
  pinMode(5, OUTPUT); //G
  pinMode(6, OUTPUT); //B
  Serial.begin(9600);
}

void loop()
{
	int R = analogRead(A5) / 4;
  	int G = analogRead(A4) / 4;
  	int B = analogRead(A3) / 4;
  	
  	analogWrite(3,R);
  	analogWrite(5,G);
  	analogWrite(6,B);

  	delay(10);
}
```
## 基礎練習-2021/04/12
### 練習一:七段顯示器的應用-1
請利用接線的方式顯示數字9
![](https://i.imgur.com/ayBLiCz.png)

### 練習二:七段顯示器的應用-2
利用程式控制七段顯示器
![](https://i.imgur.com/EsGnuJd.png)

```c=
int A = 4;
int B = 5;
int C = 7;
int D = 8;
int E = 9;
int F = 3;
int G = 2;
int DP = 6;

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop()
{
  digitalWrite(A, HIGH);
  digitalWrite(C, HIGH);
  digitalWrite(D, HIGH);
  digitalWrite(E, HIGH);
  digitalWrite(F, HIGH);
  digitalWrite(G, HIGH);
  delay(500);
}
```

## 基礎練習-2021/04/19
### 練習一:七段顯示器的應用-3
![](https://i.imgur.com/saCMMgK.png)

利用迴圈顯示七段顯示器
```c=
//宣告七段顯示器的角位
int a = 4;
int b = 5;
int c = 7;
int d = 8;
int e = 9;
int f = 3;
int g = 2;
int dp = 6;

//宣告七段顯示器亮燈角位
int N0[] = {a,b,c,d,e,f};
int N1[] = {b,c};
int N2[] = {a,b,g,e,d};
int N3[] = {a,b,g,c,d};
int N4[] = {f,g,b,c};
int N5[] = {a,f,g,c,d};
int N6[] = {a,f,e,d,c,g};
int N7[] = {a,b,c};
int N8[] = {a,b,c,d,e,f,g};
int N9[] = {a,f,g,b,c,d};

//輸出函數
void showNumber(int num[],int arrayLength)
{
	for(int i=2;i<=9;i++)
    {
    	digitalWrite(i, LOW);
    }

  	for(int i=0;i<arrayLength;i++)
    {
    	digitalWrite(num[i], HIGH);
    }
}


void setup()
{
  for(int i=2;i<=11;i++)
  {
  	pinMode(i, OUTPUT);
  }
}


void loop()
{
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  showNumber(N9,sizeof(N9)/sizeof(int));	
  delay(50);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  showNumber(N8,sizeof(N8)/sizeof(int));	  
  delay(50);
  

  
}
```
### 練習二:74HC595
![](https://i.imgur.com/HvEvtZF.png)
```c=
const int DS = 11;
const int STCP = 12;
const int SHCP = 13;


byte N0 = B01000100;//68
byte N1 = B01011111;//95
byte N2 = B01100010;//98
byte N3 = B01001010;//74
byte N4 = B01011001;//89
byte N5 = B11001000;//200
byte N6 = B11000000;//192
byte N7 = B01011110;//94
byte N8 = B01000000;//64
byte N9 = B01001000;//72

void setup()
{
  pinMode(DS, OUTPUT);
  pinMode(STCP, OUTPUT);
  pinMode(SHCP, OUTPUT);
}

void loop()
{
  digitalWrite(STCP,LOW);
  shiftOut(DS,SHCP,LSBFIRST,N0);
  digitalWrite(STCP,HIGH);  
  delay(1000);
}
```

## 基礎練習-2021/04/26
### 練習一:七段顯示器的應用-4
![](https://i.imgur.com/pgMXaMe.png)
```c=
const int DS = 11;
const int STCP = 12;
const int SHCP = 13;

byte N0 = B01000100;//68
byte N1 = B01011111;//95
byte N2 = B01100010;//98
byte N3 = B01001010;//74
byte N4 = B01011001;//89
byte N5 = B11001000;//200
byte N6 = B11000000;//192
byte N7 = B01011110;//94
byte N8 = B01000000;//64
byte N9 = B01001000;//72

byte array[] = {N0,N1,N2,N3,N4,N5,N6,N7,N8,N9};

void setup()
{
  Serial.begin(9600);
  pinMode(DS, OUTPUT);
  pinMode(STCP, OUTPUT);
  pinMode(SHCP, OUTPUT);
}

void loop()
{
  
  
  for(int i=0;i<=20000;i++)
  {
    digitalWrite(STCP,LOW);
    Serial.println(i/10);
    digitalWrite(STCP,LOW);
    shiftOut(DS,SHCP,LSBFIRST,array[i%10/1]);
    shiftOut(DS,SHCP,LSBFIRST,array[(i%100)/10]);
    shiftOut(DS,SHCP,LSBFIRST,array[(i%1000)/100]);
  	shiftOut(DS,SHCP,LSBFIRST,array[(i%10000)/1000]);
    digitalWrite(STCP,HIGH); 
    digitalWrite(STCP,HIGH); 
    delay(500);
  }
}
  
```

## 基礎練習-2021/05/10
### 練習一:LCD
![](https://i.imgur.com/KuFRjRq.png)

```c=
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);
void setup() {
  	Serial.begin(9600);
	lcd.begin(16, 2);
	lcd.setCursor(0, 0);
  	lcd.print("Hello World");
}
int t = 0;
void loop() {
    lcd.setCursor(0, 1);
  	lcd.print("Time:");
  	lcd.setCursor(5, 1);
  	lcd.print(t);
  	t = t + 1;
  	delay(1000);
}
```