0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ArduinoでRTCを使用して時刻を取得する際のハード側注意点

Last updated at Posted at 2022-06-12

やったこと

ESP32にてRTC(DS1307)とOLED(SSD1306)を使って時刻をディスプレイ表示します。ESP32を使うのでwifi接続して時刻取得すればいいいのですが、今回はあえてローカルな方法でやっていきます。

OLED(SSD1306)の使い方

image.png

過去の記事があるのでそちら参照してください

RTC(DS1307)の使い方

image.png

amazonでKKHMF製のRTCモジュールを購入しました。

使用する電池に注意してください
これ以外の製品もそうですが、こちらは2次ボタン電池仕様となっており充電回路があります。ですので、一般的にホームセンター等で売られている安いボタン電池(CR2023)をそのまま使うことは危険ですので、少し細工が必要になります。

充電回路カット

image.png

こちらの回路を見ると電源からVCCkらbatteryに電気が流れて充電されていることがわかります。
参考までにD1,R4,R5,R6をカットしてR6はメモリ電源として必要なため半田等で再度接続してください。

製品により異なりますので、実施する場合は必ず回路を入手して自己責任で行ってください

ソースコード

どちらもI2C通信モジュールです。ライブラリによっては干渉してしまう現象もありました。下記のコードは問題なく動作することは確認しています。

RTC_OLED.ino
/*
 DS1307+SSD1306
 ディスプレイSSD1306 ( 横 128ピクセル × 縦 64ピクセル )  
      GND → GND
      VCC → 3.3V
      SCL → GPIO 22 
      SDA → GPIO 21
 tiny RTC(充電回路カットしてから使うこと)
      GND → 
      vcc →  5V(降圧回路あるか確認してから)
      SCL →  GPIO 22
      SDA →  GPIO 21
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>

#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 
RTC_DS1307 rtc;

String time_;

void setup() {
  Serial.begin(9600);

  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }

  delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display

  oled.setTextSize(3);          // text size
  oled.setTextColor(WHITE);     // text color
  oled.setCursor(0, 10);        // position to display

  // SETUP RTC MODULE
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (true);
  }

//  // PCの時刻を書き込む(1度書き込んだらコメントアウトして再度書き込む)
//  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 
}

void loop() {
  DateTime now = rtc.now();
  time_ = "";
  time_ += now.hour();
  time_ += ':';
  time_ += now.minute();
  time_ += ':';
  time_ += now.second();
  Serial.println(time_);
  oledDisplayCenter(time_);
  delay(1000);         // wait for initializing

}

void oledDisplayCenter(String text) {
  int16_t x1;
  int16_t y1;
  uint16_t width;
  uint16_t height;

  oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);

  // display on horizontal and vertical center
  oled.clearDisplay(); // clear display
  oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
  oled.println(text); // text to display
  oled.display();
}

image.png

画像ボケてしまいましたが、こんなに感じになります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?