① 今日の目標
🏆 Chapter 5 最終総合作品 — このレッスンで24レッスンすべての学習が完結します!
温度・光の複数センサーを同時に使える
LCDにページ切り替えで複数データを表示できる
しきい値に応じた警報機能を実装できる
Ch1〜4の全技術を1つの作品に統合できる
📚 このプロジェクトで使う全技術
Ch1
analogRead
digitalWrite
analogRead
digitalWrite
Ch2
if/else if
変数・演算
if/else if
変数・演算
Ch3
TMP36
CdSセンサー
TMP36
CdSセンサー
Ch4
tone()ブザー
I2C LCD
tone()ブザー
I2C LCD
Ch5
複数センサー
統合制御
複数センサー
統合制御
② 必要なもの
Arduino Uno
マイコンボード本体
TMP36温度センサー
気温をアナログ電圧で出力
CdSセル(光センサー)
明るさを電圧で出力
I2C LCD 16×2
測定データをリアルタイム表示
パッシブブザー
温度・暗さの警告音
10kΩ抵抗×1本
CdSのプルダウン用
③ しくみを学ぼう
📺 LCDページ切り替え
16×2のLCDには一度に2行しか表示できません。複数のデータを表示するためにページ切り替えを使います。
ページ0(温度ページ)
ページ1(光ページ)
ページ2(アラートページ)
Temp: 25.3CEngiKids Wx ページ1(光ページ)
Light: Sunny A0: 720 ページ2(アラートページ)
Status: OK All Normal
ボタンで手動切り替え、または一定時間で自動切り替えの2パターン!
☀️ CdSから天気を判定
if (light >= 700) weather = "Sunny";
else if (light >= 400) weather = "Cloudy";
else weather = "Dark";
🔔 警報システム
🌡️
高温警報
temp > 35℃ で警告音(1000Hz)
temp > 35℃ で警告音(1000Hz)
🌑
暗闇警報
light < 200 で低音(500Hz)
light < 200 で低音(500Hz)
✅
正常
条件外は noTone() で消音
条件外は noTone() で消音
④ Arduinoコード
weather_station.ino
// ── ライブラリ ──
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ── ピン定義 ──
const int TEMP_PIN = A0;
const int LIGHT_PIN = A1;
const int BTN_PIN = 2;
const int BUZZER_PIN = 8;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int page = 0; // 表示ページ番号(0〜2)
bool lastBtnState = HIGH;
unsigned long lastUpdate = 0;
void setup() {
lcd.begin(); lcd.backlight();
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
lcd.setCursor(0,0); lcd.print("EngiKids WxStn");
lcd.setCursor(0,1); lcd.print("Starting... ");
delay(2000);
}
void loop() {
// ── センサー読み取り ──
float voltage = analogRead(TEMP_PIN) * 5.0 / 1024.0;
float temp = (voltage - 0.5) * 100.0;
int light = analogRead(LIGHT_PIN);
// ── 天気を判定 ──
String weather;
if (light >= 700) weather = "Sunny ";
else if (light >= 400) weather = "Cloudy ";
else weather = "Dark ";
// ── ボタンでページ切り替え ──
bool btn = digitalRead(BTN_PIN);
if (btn == LOW && lastBtnState == HIGH) {
page = (page + 1) % 3; // 0→1→2→0...
lcd.clear();
}
lastBtnState = btn;
// ── LCD表示(ページ切り替え)──
if (millis() - lastUpdate > 500) {
lastUpdate = millis();
switch (page) {
case 0: // 温度ページ
lcd.setCursor(0,0); lcd.print("Temp: ");
lcd.print(temp,1); lcd.print((char)223); lcd.print("C ");
lcd.setCursor(0,1); lcd.print("EngiKids Wx ");
break;
case 1: // 光ページ
lcd.setCursor(0,0); lcd.print("Light: ");
lcd.print(weather);
lcd.setCursor(0,1); lcd.print("A1: ");
lcd.print(light); lcd.print(" ");
break;
case 2: // ステータスページ
lcd.setCursor(0,0);
lcd.print(temp > 35 ? "!!HOT ALERT!! " : "Status: OK ");
lcd.setCursor(0,1);
lcd.print(light < 200 ? "!!DARK ALERT!! " : "All Normal ");
break;
}
}
// ── 警報 ──
if (temp > 35) tone(BUZZER_PIN, 1000);
else if (light < 200) tone(BUZZER_PIN, 500);
else noTone(BUZZER_PIN);
}
switch文でページ管理
switch(page)でcase 0/1/2ごとに表示内容を切り替えます。if/else ifでも同じことができますが、switchの方がスッキリ書けます。
switch(page)でcase 0/1/2ごとに表示内容を切り替えます。if/else ifでも同じことができますが、switchの方がスッキリ書けます。
millis()で非ブロッキング更新
delay()を使わずmillis()で時間管理することで、センサー読み取りとボタン検知を同時に行えます。
delay()を使わずmillis()で時間管理することで、センサー読み取りとボタン検知を同時に行えます。
INPUT_PULLUPでボタン
Arduinoの内蔵プルアップ抵抗を使うと、外部に抵抗を追加しなくてもボタンが使えます。
Arduinoの内蔵プルアップ抵抗を使うと、外部に抵抗を追加しなくてもボタンが使えます。
三項演算子で短く
「35℃超なら"HOT"、そうでなければ"OK"」を1行で書く方法です。
temp > 35 ? "HOT" : "OK" は「35℃超なら"HOT"、そうでなければ"OK"」を1行で書く方法です。
⑤ 配線図
⑥ チェックリスト
⑦ チャレンジクイズ!
⑧ 🌤️ 気象ステーションシミュレーター
スライダーで温度・明るさを変えてLCDの表示と警報を確認しよう!
⛅
✅ 正常範囲内
温度
25.0°C
明るさ (A1)
720
天気判定
Sunny
ブザー
OFF
float temp = 25.0; // ℃
int light = 720; // 0〜1023
// 天気: Sunny
// ページ: 0 → 温度ページ
if (temp > 35) tone(BUZZER, 1000); // off
int light = 720; // 0〜1023
// 天気: Sunny
// ページ: 0 → 温度ページ
if (temp > 35) tone(BUZZER, 1000); // off
⑨ まとめ
switch文でページ切り替え
case 0/1/2ごとに
LCD表示を変える
case 0/1/2ごとに
LCD表示を変える
millis()で非ブロッキング
delay()不要で
センサーとボタンを同時処理
delay()不要で
センサーとボタンを同時処理
複合アラートシステム
温度・光の両方を
監視して警報
温度・光の両方を
監視して警報
全Ch技術の集大成!
Ch1〜4の全部を
1作品に統合完了!
Ch1〜4の全部を
1作品に統合完了!