LoginSignup
4
6

More than 5 years have passed since last update.

Arduinoにリアルタイムクロックモジュール とLCDを繋いで小さな時計を作成する

Posted at

リアルタイムクロックモジュール(RTC)と、LCDを繋いで時刻を表示します。
どちらもI2C接続する場合はArduinoとピン間を直接接続せず、I2Cバス経由で接続します。

準備

過去の記事1記事2、で準備したものを再利用します。

Arduino UNO R3 互換品
HiLetgo® 2個セットDC 5V HD44780 1602 LCD ディスプレイモジュール 16×2キャラクタ LCDブルーブラックライト
HiLetgo® 5個セット5V 1602LCD IIC/I2C/TWI/SPIシリアルインターフェイスモジュールポートarduinoに対応
RTC DS3231 AT24C32 時計モジュール
電池 LIR2032
・ブレットボード
・ジャンパワイヤ (オス-メス)×8個

回路図

下記の通り接続します。
LCDとシリアルインタフェースの接続は過去の記事2をご参照下さい。
0012.png

コード

setup関数: RTC/LCDの初期化を行います
PrintRTCInfoToLCD関数: RTCの情報をLCDに出力します
GetIntegerFromRTC関数: RTCの情報から日、月、年をint型で取得します。
loop関数: 1秒置きに処理を繰り返します

#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
#include <string.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
DS3231  rtc(SDA, SCL);

void setup() {
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Hello, world!");
  lcd.setCursor(0,1);
  lcd.print("Arduino LCM IIC 2004");

  // Setup Serial connection
  Serial.begin(115200);
  // Initialize the rtc object
  rtc.begin();

  delay (3000);
  String BLANK = "                ";
  lcd.setCursor(0,0);
  lcd.print( BLANK );
  lcd.setCursor(0,1);
  lcd.print( BLANK );

  // set the date and time.
  //rtc.setDOW(SUNDAY);     // Set Day-of-Week
  //rtc.setTime(13, 20, 0);     // Set the time to HH:MM:SS (24hr format)
  //rtc.setDate(10, 2, 2019);   // Set the date (DD, MM, YYYY)
}

void PrintRTCInfoToLCD() {

  String strDOW = rtc.getDOWStr();
  String strDate = rtc.getDateStr();
  String strTime = rtc.getTimeStr();
  int iTemp = rtc.getTemp();

  lcd.setCursor(0,0);
  lcd.print( strDate );

  lcd.setCursor(0,1);
  lcd.print( strTime );

  lcd.setCursor(9,1);
  lcd.print( strDOW );  
}

void GetIntegerFromRTC() {

  String strDate = rtc.getDateStr();
  int len = strDate.length() + 1;
  char tmpstr[len];
  char *tp;
  char cDay[3], cMonth[3], cYear[5];
  int  iDay, iMonth, iYear;

  strDate.toCharArray(tmpstr, len);
  Serial.println(tmpstr);

  // get day
  tp = strtok(tmpstr, ".");
  strcpy(cDay, tp);
  iDay = atoi(cDay);

  // get month
  tp = strtok(NULL, ".");
  strcpy(cMonth, tp);
  iMonth = atoi(cMonth);

  // get year
  tp = strtok(NULL, ".");
  strcpy(cYear, tp);
  iYear = atoi(cYear);

  Serial.println("----");
  Serial.print("Day   :");Serial.println(iDay);
  Serial.print("Month :");Serial.println(iMonth);
  Serial.print("Year  :");Serial.println(iYear);
  Serial.println("----");
}

void loop() {

  PrintRTCInfoToLCD();
  GetIntegerFromRTC();

  delay (1000);
}

テスト

1秒置きに時刻を更新することができました。

014.png

日、月、年もint型で取得することができました。
0013.png

参考

How to Present DS3231's Temp Signal as a float onto LCD
RTC DS3231 AT24C32 時計モジュールをArduinoに繋げて動かす
ArduinoにLCDを繋いで文字を表示する
組み込みエンジニアでなくても週末にArduinoを使って遊ぶ

4
6
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
4
6