LoginSignup
3
4

More than 5 years have passed since last update.

RTC DS3231 AT24C32 時計モジュールをArduinoに繋げて動かす

Last updated at Posted at 2019-02-05

RTC(リアルタイムクロック)DS3231 AT24C32 時計モジュールをArduinoに繋げて動かす際の手順を記載します。

準備

Arduino UNO R3 互換品
RTC DS3231 AT24C32 時計モジュール
電池 LIR2032
・ジャンパーワイヤ 4本(メス-オス)

回路図

下記モジュールにジャンパーワイヤを接続してArduinoと繋ぎます。
001.png

RTC ----- Arduino 間を下記の通り配線する。
VCC <---> 5V
GND <---> GND
SDA <---> Analog4
SDC <---> Analog5

コード

[参考]記載のライブラリをインストールした後下記を書込み
時刻取得

void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // Initialize the rtc object
  rtc.begin();
}

void loop()
{
  // Get data from the DS3231
  t = rtc.getTime();  
  // Send date over serial connection
  Serial.print("Today is the ");
  Serial.print(t.date, DEC);
  Serial.print(". day of ");
  Serial.print(rtc.getMonthStr());
  Serial.print(" in the year ");
  Serial.print(t.year, DEC);
  Serial.println(".");

  // Send Day-of-Week and time
  Serial.print("It is the ");
  Serial.print(t.dow, DEC);
  Serial.print(". day of the week (counting monday as the 1th), and it has passed ");
  Serial.print(t.hour, DEC);
  Serial.print(" hour(s), ");
  Serial.print(t.min, DEC);
  Serial.print(" minute(s) and ");
  Serial.print(t.sec, DEC);
  Serial.println(" second(s) since midnight.");
  // Send a divider for readability
  Serial.println("  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -");  
  // Wait one second before repeating :)
  delay (1000);
}

時刻設定

// The following lines can be uncommented to set the date and time
rtc.setDOW(TUESDAY);     // Set Day-of-Week to SUNDAY
rtc.setTime(13, 0, 0);     // Set the time to 12:00:00 (24hr format)
rtc.setDate(5, 2, 2019);   // Set the date to January 1st, 2014

テスト

時刻が取得できました。
IMG_0209.JPG

参考

Library: DS3231
 API仕様書は、「Manual:」から参照できます。
 ライブラリは、「ダウンロード」から取得できます。

組み込みエンジニアでなくても週末にArduinoを使って遊ぶ
Arduino Uno 互換品を使おうとした際に、Mac(OS:Siera)上のArduino IDEからシリアルデバイスが見つからない問題を解決した件について

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4