Skip to content

Instantly share code, notes, and snippets.

@9arduino
Last active April 23, 2025 13:55
Show Gist options
  • Save 9arduino/e0b4e97bbe8660572818c89165d52499 to your computer and use it in GitHub Desktop.
Save 9arduino/e0b4e97bbe8660572818c89165d52499 to your computer and use it in GitHub Desktop.
สอนใช้งาน DS3231 โมดูลนาฬิกา ตั้งเวลา ง่ายๆ https://www.ab.in.th/b/58
/*
สอนใช้งาน DS3231 โมดูลนาฬิกา ตั้งเวลา ง่ายๆ
บทความ https://www.ab.in.th/b/58
download : https://www.ab.in.th
*/
#include <Wire.h>
#include <SPI.h>
#include <RTClib.h>
RTC_DS3231 RTC;
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__)); //จุดนี้เป็นการตั้งเวลา ตั้งครั้งแรกเสร็จแล้วให้ // ไว้ด้วย
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
DateTime now = RTC.now();
RTC.setAlarm1Simple(21, 58); //เป็นการตั้งเวลาปลุก เวลา 22.58 น.
RTC.turnOnAlarm(1); //ปลุกช่วงเวลาที่ 1
if (RTC.checkAlarmEnabled(1)) {
Serial.println("Alarm Enabled");
}
}
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();
if (RTC.checkIfAlarm(1)) { // เมื่อถึงเวลาให้ทำการปลุกโดยการทำตามเงือนไขใน if
Serial.println("Alarm Triggered");
}
Serial.println();
delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment