128x64 LCDスマートクロック:アナログ/デジタル時刻・温度表示対応(Arduino Nano + DS3231 RTC)
必要な部品と材料
| 部品 | 数量 | 備考 |
|---|---|---|
| Arduino Nano R3 | 1 | |
| DS3231M RTCモジュール(±5ppm、I2C) | 1 | |
| 10kΩ可変抵抗 | 1 | LCDコントラスト調整用 |
| タクトスイッチ(モメンタリ) | 1 | 画面切替用 |
| 128x64 LCDグラフィックディスプレイ | 1 | ST7920コントローラー対応 |
必要な工具
- はんだごて
プロジェクト概要
128x64 LCDグラフィックディスプレイ、コントラスト調整用可変抵抗、DS3231 RTCモジュール、タクトスイッチを使用した多機能クロックです。RTCはA4(SDA)、A5(SCL)に接続。スイッチはD3に接続し、内部プルアップを有効化しています。
コンパイル前に以下のライブラリをArduino IDEのlibrariesフォルダに配置してください:
- U8glib
- DS3232RTC
- Streaming
- Time
- Wire
画面構成
タクトスイッチで2画面を切り替えます。
- 画面1: アナログ時計、デジタル時計、日付、温度
- 画面2: デジタル時計、曜日、日付、現在/最小/最大温度

RTCの日時設定
シリアルモニタから「年、月、日、時、分、秒」の順で入力するとRTCに反映されます。正しく入力されると確認メッセージが表示されます。
ソースコード
// Programa: Display LCD 128x64 e RTC DS3231
// Autor: Arduino e Cia
#include <U8glib.h>
#include <DS3232RTC.h>
#include <Streaming.h>
#include <Time.h>
#include <Wire.h>
// ディスプレイ接続ピン設定(使用モデルに合わせて調整)
U8GLIB_ST7920_128X64_1X u8g(6, 5, 4, 7); // Enable, RW, RS, RESET
int X2 = 0, Y2 = 0, X3 = 0, Y3 = 0;
float angulo = 0;
int posicao = 0, posicaoh = 0;
int temperatura = 0;
int min_temp = 500, max_temp = -500;
int ScreenWith = 128, ScreenWithC = 96;
int ScreenHeight = 64, ScreenHeightC = 32;
#define botao 3
int estado_botao = 0;
char* dia_da_semana[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void tela_1() {
u8g.drawRFrame(0, 0, 128, 64, 3);
u8g.setPrintPos(20, 30);
u8g.print(temperatura);
u8g.drawStr(42, 30, "C");
u8g.drawCircle(37, 34, 2);
mostra_relogio_digital();
fundo_relogio();
desenha_ponteiro(hour()-1, 12.0, 10); // 時針
desenha_ponteiro(minute()-5, 60.0, 19); // 分針
desenha_ponteiro(second()-5, 60, 21); // 秒針
}
void tela_2() {
u8g.drawRFrame(0, 0, 128, 64, 3);
u8g.drawRFrame(68, 4, 55, 56, 2);
mostra_relogio_digital();
u8g.setFont(u8g_font_5x8);
u8g.drawStr(78, 35, "MIN");
u8g.drawStr(78, 53, "MAX");
u8g.setFont(u8g_font_6x13);
u8g.setPrintPos(25, 41);
u8g.print(dia_da_semana[weekday()-1]);
u8g.setPrintPos(83, 19);
u8g.print(temperatura);
u8g.drawStr(105, 19, "C");
u8g.drawCircle(100, 12, 2);
u8g.setPrintPos(98, 36);
u8g.print(min_temp);
u8g.drawCircle(113, 29, 2);
u8g.setPrintPos(98, 54);
u8g.print(max_temp);
u8g.drawCircle(113, 47, 2);
}
void desenha_ponteiro(float valor, float rotacao, int Radius) {
angulo = valor * 2.0 * 3.1415 / rotacao - 1.5707;
X2 = ScreenWithC + Radius * cos(angulo);
Y2 = ScreenHeightC + Radius * sin(angulo);
u8g.drawLine(ScreenWithC, ScreenHeightC, X2, Y2);
}
void fundo_relogio() {
u8g.drawCircle(ScreenWithC, ScreenHeightC, 27);
u8g.drawCircle(ScreenWithC, ScreenHeightC, 1);
u8g.setFont(u8g_font_6x13);
u8g.setFontPosTop();
u8g.drawStr(90, 9, "12");
u8g.drawStr(114, 25, "3");
u8g.drawStr(94, 44, "6");
u8g.drawStr(74, 25, "9");
for(int traco_minuto = 0; traco_minuto < 12; traco_minuto++) {
angulo = traco_minuto / 12.0 * 2 * 3.1415;
X2 = ScreenWithC + 25 * cos(angulo);
Y2 = ScreenHeightC + 25 * sin(angulo);
X3 = ScreenWithC + 25 * cos(angulo);
Y3 = ScreenHeightC + 25 * sin(angulo);
u8g.drawLine(X2, Y2, X3, Y3);
}
}
void mostra_relogio_digital() {
u8g.setFont(u8g_font_5x8);
u8g.setPrintPos(8, 55);
u8g.print(day());
u8g.drawStr(19, 55, "/");
u8g.setPrintPos(24, 55);
u8g.print(month());
u8g.drawStr(35, 55, "/");
u8g.setPrintPos(41, 55);
u8g.print(year());
u8g.drawRBox(3, 4, 62, 21, 2);
u8g.setColorIndex(0);
u8g.setFont(u8g_font_fub17);
u8g.drawStr(29, 21, ":");
if (hour() < 10) { u8g.drawStr(3, 23, "0"); posicaoh = 16; }
else posicaoh = 3;
u8g.setPrintPos(posicaoh, 23);
u8g.print(hour());
if (minute() < 10) { u8g.drawStr(38, 23, "0"); posicao = 51; }
else posicao = 38;
u8g.setPrintPos(posicao, 23);
u8g.print(minute());
u8g.setColorIndex(1);
}
void setup() {
pinMode(3, INPUT_PULLUP);
Serial.begin(9600);
if (u8g.getMode() == U8G_MODE_R3G3B2) u8g.setColorIndex(255);
else if (u8g.getMode() == U8G_MODE_GRAY2BIT) u8g.setColorIndex(3);
else if (u8g.getMode() == U8G_MODE_BW) u8g.setColorIndex(1);
setSyncProvider(RTC.get);
Serial << F("RTC Sync");
if (timeStatus() != timeSet) Serial << F(" FAIL!");
Serial << endl;
}
void loop() {
temperatura = RTC.temperature() / 4.0;
if (temperatura >= max_temp) max_temp = temperatura;
if (temperatura <= min_temp) min_temp = temperatura;
static time_t tLast;
time_t t;
tmElements_t tm;
// シリアルから日時設定(年月日時分秒、12文字以上)
if (Serial.available() >= 12) {
int y = Serial.parseInt();
if (y >= 100 && y < 1000) Serial << F("Erro: Ano deve ter dois ou quatro digitos!") << endl;
else {
if (y >= 1000) tm.Year = CalendarYrToTm(y);
else tm.Year = y2kYearToTm(y);
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
t = makeTime(tm);
RTC.set(t);
setTime(t);
Serial << F("Horario modificado para: ");
printDateTime(t);
Serial << endl;
while (Serial.available() > 0) Serial.read();
}
}
t = now();
if (t != tLast) {
tLast = t;
printDateTime(t);
Serial << endl;
}
// ボタンで画面切替
boolean valor_botao = digitalRead(3);
if (valor_botao != 1) {
while(digitalRead(3) != 1) delay(100);
estado_botao = !estado_botao;
}
u8g.firstPage();
do {
if (estado_botao == 0) tela_1();
if (estado_botao == 1) tela_2();
} while(u8g.nextPage());
delay(10);
}
void printDateTime(time_t t) {
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
Serial << ' ';
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
void printI00(int val, char delim) {
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
}
ライブラリ
ライブラリファイルは別途ダウンロードしてください。
回路図

製造プロセス
- Arduino POVクロック:残像効果で浮かび上がるLED時計を自作する
- 12メートル先から見える大型LEDマスタークロック(アラーム機能付き)を自作しよう
- Arduino UNOで作るLCDアニメーション&ゲーム入門
- Arduino Dueと3.2インチTFTで作る温度監視付きリアルタイムクロック
- TM1637デジタル時計:時刻設定・アラーム機能付き Arduino Nano試作版
- ArduinoとNokia 5110ディスプレイで自作電圧計:ステップバイステップガイド
- BME280 温湿度・気圧データを Nextion タッチスクリーンに表示
- Arduino超音波センサーとProcessingで作るリアルタイム距離測定ソナー
- DS1302 RTCとArduino UNOで作る信頼性の高い目覚まし時計
- アナログとデジタル信号:産業計装の基礎
- アナログセンサーとデジタルセンサー:種類、応用、実例