マイコンをスタンドアローンで使用する際に時計を作りたい、何かを記録したいと思ったときに、時刻情報を保持したり読みだしたりすることのできるモジュール。
マイコン内部にも起動からの経過時間を出力する機能はあるが、電源を切るとリセットされてしまうため使い勝手が悪い。
RTCモジュールにはマイコンの電源を切ったとしても、時刻を保持するためのコンデンサや電池、蓄電池を搭載しているものが多い。
各種ICに対応した変換基板が売られているが、i2cを使用して通信するものが一般的。
選ぶときの目安
一番よく見るのはこのタイプでCR2032サイズの蓄電池を使用するタイプ。本来はこのバッテリーケースにはML2032という二次電池を搭載することが前提だが、ショップによってはCR2032を付属している場合がある。
基板の回路上は充電機能が搭載されているため、一次電池を使用する場合は回路を修正する必要があり、あまりモジュールとしても小さくないのでお勧めしない。
電池まわりの注意(ここでつまずきやすい)
先ほどと同様にDS3231を搭載した小型モジュール。この商品は二次電池が直接取り付けてあるためはんだ付けなどをする場合は注意が必要だが、無加工で問題なく使えるモジュール。
両モジュールとも3.3[V],5[V]に対応しているので、ぶっちゃけどちらを使ってもいい。RTC基板記載の"+"をArduinoの3.3[V]、"-"をArduinoのGND、"D"をArduinoの向かって左上のSDA、"C"をArduinoのSCLに接続する。
まずはRTCモジュールがi2cで認識されているかどうかを確認する。
下記コードを書き込み実行するとシリアルターミナルに検出されたRTCのアドレスが表示される。
"0x57"や"0x68"などモジュールによりアドレスが異なるため、DS3231のサンプルソースコードを確認し、アドレスが一致しているかどうかは確認が必要。
以下は2つ目の二次電池がついているモジュールのスキャン結果。
Scanning...
I2C device found at address 0x68 !
done
Arduino Playground : i2c_scanner
// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
時刻の設定、および表示が1つになっていて、ライブラリマネージャーからインストールできる
"Andrew Wickert:DS3231"の"Schetch example>DS3231>echo_time"
がわかりやすい。
各種ライブラリをインストールしていると、サンプルプログラム内で
c:\program files (x86)\arduino\hardware\tools\avr\avr\include\time.h:164:20: note: previous declaration 'clock_t clock()'
extern clock_t clock(void);
などのエラーが出ることがある。コンストラクタが"clock"という汎用名になり、他のライブラリと被るのが原因のため、コード内の"clock"を他の名称に置換することで回避可能。
DS3231 clock; //Before ×
DS3231 clock1; //After 〇
Arduino Unoなど周辺デバイスが接続されていないボードの場合は問題ないが、M5Stackのように液晶やセンサが搭載されているデバイスの場合はi2cのアドレスが競合して使えない場合がある。
M5Stackの場合はi2cのバスが2系統搭載されているため、"Wire"、"Wire1"などライブラリ内のバスを修正すれば使えることもある。
※ 本ページのAmazonリンクにはアフィリエイトタグ(toobf6-22)が付いている。
🤖 このページにはAIエージェント(ノア)による編集・加筆が含まれます。内容の正確性は保証できないため、重要な判断は一次情報でご確認ください。