# 參考連結
- Arduino Introduction
- 五環電阻色碼計算器
# 溫度與濕度感測器
#include <DHT.h> // 匯入程式庫 DHT sensor library | |
#include <DHT_U.h> | |
#define DHTPIN 2 // DHT pin 2 連接 Arduino pin 2 | |
#define DHTTYPE DHT11 // 適用 DHT 11 | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(9600); | |
Serial.println(F("DHT11 test!")); | |
dht.begin(); | |
} | |
void loop() { | |
delay(2000); // 等 2 秒鐘 | |
float h = dht.readHumidity(); // 讀取濕度值 h | |
float t = dht.readTemperature(); // 讀取攝氏溫度值 t | |
if (isnan(h) || isnan(t)) { // 如果讀不到數值,印出讀取失敗訊息 | |
Serial.println(F("Failed to read from DHT sensor!")); | |
return; | |
} | |
Serial.print("Humidity: "); // 在序列埠螢幕中印出濕度、攝氏溫度 | |
Serial.print(h); | |
Serial.print(" %\t"); | |
Serial.print("Temperature: "); | |
Serial.print(t); | |
Serial.print(" *C \n"); | |
} |
# LED 閃燈
// the setup function runs once when you press reset or power the board | |
void setup() { | |
// initialize digital pin LED_BUILTIN as an output. | |
pinMode(8, OUTPUT); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(1000); // wait for a second | |
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW | |
delay(1000); | |
} |
控制繼電器也是一樣的程式: 繼電器的使用
有源蜂鳴器也一樣: 第六篇 有源蜂鳴器、無源蜂鳴器
# 超音波測距模組 HC-SR04
#include <Ultrasonic.h> | |
Ultrasonic ultrasonic(12, 13); | |
int distance; | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
distance = ultrasonic.read(); // 不加參數就是輸出 CM,可用 read (INC) 輸出英寸 | |
Serial.print("Distance in CM: "); | |
Serial.println(distance); | |
delay(500); // 每次間格 0.5 秒 | |
} |
# 時間模組和 LCD 顯示器
參考 1: DS1302 時鐘模組
參考 2: Arduino 大字型 LCD 時鐘
參考 3: Arduino 1602 LCD
程式碼太長,請直接至上述連結參閱。
注意:顯示器的 i2c 位址,是 0x27
# 光敏電阻
void setup() { | |
Serial.begin(9600); | |
pinMode(3,OUTPUT); | |
} | |
void loop() { | |
int sensorValue = analogRead(A0); | |
Serial.println(sensorValue); | |
if(sensorValue < 900){ // 當光敏電阻給的值小於基準值。這個基準值依個別情況修改 | |
digitalWrite(3,HIGH); // 燈亮 | |
}else{ | |
digitalWrite(3,LOW); // 燈滅 | |
} | |
delay(10); | |
} |
# 蜂鳴器
參考: Arduino 入門第六篇
有源蜂鳴器同上 (LED 閃燈) 之程式碼
無源蜂鳴器則使用範例 02.Digital > toneMelody 即可。