วันศุกร์ที่ 8 พฤษภาคม พ.ศ. 2558

DS3231 Module โมดูลนาฬิกา


โมดูลนาฬิกา DS3231 module  ความแม่นยำสูง RTC DS3231 AT24C32 IIC Module
Precision Clock Module for Arduino

 DS3231 module เป็นโมดูนาฬิกาแบบเวลาจริง RTC( Real Time Clock ) ที่มีความถูกต้องแม่นยำสูง
เพราะข้างในมีวงจรวัดอุณหภูมิ เพื่อนำอุณหภูมิจากสภาพแวดล้อมมาคำนวนชดเชยความถี่ของ Crystal ที่
ถูกรบกวนจากอุณหภูมิภายนอก มาพร้อมแบตเตอร์รี่ ใช้งานได้แม้ไม่มีแหล่งจ่ายไฟจากภายนอก สามารถตั้ง
ค่า วัน เวลา ได้อย่างง่าย มีไลบารีมาพร้อมใช้งาน สามารถเลือกแสดงผลเวลาแบบ 24 ชั่วโมงหรือแบบ 12
ชัวโมงก็ได้
นอกจากจะแสดงวันและเวลาได้อย่างแม่นยำแล้ว โมดูลนี้ยังสามารถ แสดงอุณหภูมิภายนอกได้ เป็นเหมือน
นาฬิกาดิจิตอลที่บอกอุณหภูมิได้ด้วย


DS3231 module  Arduino
SCL  A5
SDA  A4
VCC  5V
GND  GND
 
 



// Date, Time and Alarm functions using a DS3231 RTC connected via I2C and Wire lib
#include "Wire.h"
#include "SPI.h"  // not used here, but needed to prevent a RTClib compile error
#include "RTClib.h"
RTC_DS3231 RTC;
void setup () {
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  // ตั้งเวลาเตือนให้เอา // ข้างหน้า RTC.adjust ออก
  //RTC.adjust(DateTime(__DATE__, __TIME__));
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  DateTime now = RTC.now();
//กำหนดให้ นาฬิกาเตือนตอน 13:05
  RTC.setAlarm1Simple(13, 5);
  RTC.turnOnAlarm(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);
  if (RTC.checkIfAlarm(1)) {
    // ถ้านาฬิกาเดินถึง 13:05 จะมาทำงานในนี้
    Serial.println("Alarm Triggered");
  }
  Serial.println();
  Serial.print("Tempeature = ");
  Serial.print(RTC.getTemperature()); // คำสั่งดึงอุณหภูมิออกมาแสดง
  Serial.println(" C");
  delay(1000);
}