RTC Module — นาฬิกาเวลาจริงสำหรับ Arduino
RTC (Real Time Clock) DS1307 เป็นโมดูลนาฬิกาที่รักษาเวลาต่อเนื่องแม้ Arduino ดับ ด้วยแบตเตอรี่ CR2032 สำรอง ทำให้ระบบรดน้ำและเปิดไฟตรงเวลาทุกวัน
การต่อวงจร
DS1307 RTC Module: VCC → Arduino 5V GND → GND SDA → Arduino A4 (Uno/Nano) หรือ D21 (Mega) SCL → Arduino A5 (Uno/Nano) หรือ D22 (Mega)
Source Code — ตั้งเวลาและอ่านค่า
#include "RTClib.h"
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("❌ RTC not found!");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("⚠️ RTC stopped! Setting time...");
// ตั้งเวลาเมื่อ compile (ใช้เวลาของคอมพิวเตอร์)
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC); Serial.print('/');
Serial.print(now.month(), DEC); Serial.print('/');
Serial.print(now.day(), DEC); Serial.print(' ');
Serial.print(now.hour(), DEC); Serial.print(':');
Serial.print(now.minute(), DEC); Serial.print(':');
Serial.print(now.second(), DEC); Serial.println();
// ตรวจสอบว่ารดน้ำตอน 6:00 หรือไม่
if (now.hour() == 6 && now.minute() == 0 && now.second() < 10) {
Serial.println("💧 รดน้ำตอนเช้า!");
}
delay(1000);
}
ระบบรดน้ำและเปิดไฟตามเวลา (สมบูรณ์)
#include "RTClib.h"
RTC_DS1307 rtc;
const int RELAY_VALVE = 7;
const int RELAY_LIGHT = 8;
// ตาราง
struct Task {
int h, m;
const char* name;
int relay;
int duration; // seconds
};
Task tasks[] = {
{5, 30, "รดน้ำเช้า", RELAY_VALVE, 180},
{6, 0, "เปิดไฟสวน", RELAY_LIGHT, 3600},
{7, 30, "ปิดไฟสวน", RELAY_LIGHT, 0}, // 0 = turn off
{17, 0, "เปิดไฟสวน", RELAY_LIGHT, 3600},
{18, 0, "รดน้ำเย็น", RELAY_VALVE, 180},
{22, 0, "ปิดไฟสวน", RELAY_LIGHT, 0},
};
const int TASK_COUNT = 6;
int lastTaskMinute = -1;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) while(1);
pinMode(RELAY_VALVE, OUTPUT); digitalWrite(RELAY_VALVE, HIGH);
pinMode(RELAY_LIGHT, OUTPUT); digitalWrite(RELAY_LIGHT, HIGH);
}
void loop() {
DateTime now = rtc.now();
int currentMinute = now.hour() * 60 + now.minute();
if (currentMinute != lastTaskMinute) {
for (int i = 0; i < TASK_COUNT; i++) {
int taskMin = tasks[i].h * 60 + tasks[i].m;
if (taskMin == currentMinute) {
if (tasks[i].duration == 0) {
digitalWrite(tasks[i].relay, HIGH);
Serial.print(tasks[i].name); Serial.println(" — OFF");
} else {
digitalWrite(tasks[i].relay, LOW);
Serial.print(tasks[i].name); Serial.println(" — ON");
}
}
}
lastTaskMinute = currentMinute;
}
delay(15000);
}
Tip: DS3231 เป็นรุ่นที่ดีกว่า DS1307 (แม่นยำกว่า มี Temp Sensor) ราคาเพิ่มไม่กี่สิบบาท แนะนำให้ใช้ DS3231 แทน DS1307
💰 ราคาประมาณการ
| อุปกรณ์ | ราคา |
|---|---|
| Solenoid Valve 1/2" 12V | ~250-400 บาท |
| Solenoid Valve 3/4" 12V | ~300-500 บาท |
| ปั๊ม Diaphragm 12V | ~300-600 บาท |
| ปั๊ม Submersible 12V | ~150-300 บาท |
| RTC DS3231 | ~40-80 บาท |
| ท่อPVC 1/2" + ข้อต่อ | ~100-300 บาท |
| หัวพ่นน้ำ/สายน้ำหยด | ~50-200 บาท |
| Adapter 12V 5A (รดน้ำ) | ~200-300 บาท |
🔋 ทางเลือกใช้แบตเตอรี่
ระบบรดน้ำใช้ไฟมากสุดตอนเปิดวาล์ว/ปั๊ม (~1-2A) เหมาะกับ แบต 12V 7Ah (~350 บาท) + Solar Panel 20W (~500 บาท) — ใช้ได้ 2-3 วันต่อการชาร์จ 1 ครั้ง
หรือใช้ แบต LiFePO4 12V 10Ah (~1,000 บาท) — น้ำหนักเบา อายุใช้งานยาวนานกว่าแบตตะกั่วกรด
คำนวณง่ายๆ: ถ้ารดน้ำวันละ 30 นาที กระแส 2A → ใช้ไฟ 1Ah/วัน แบต 7Ah อยู่ได้ 5-7 วัน
⚠️ หมายเหตุ: เนื้อหานี้ค้นคว้าและเรียบเรียงโดย Hermes AI — ข้อมูลทางเทคนิคควรตรวจสอบก่อนนำไปใช้งานจริง