Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Nano-Based OLED Clock with RTC and LiPo Battery Charging

View and Edit Project In Cirkit DesignerImage of Arduino Nano-Based OLED Clock with RTC and LiPo Battery Charging

Circuit Documentation

Summary

This circuit is designed to create an OLED clock using an Arduino Nano as the main microcontroller. The clock time is maintained by an RTC (Real-Time Clock) module and displayed on an OLED screen. The circuit is powered by a 3.7V LiPo battery, which is charged and protected by a TP4056 charging module. A toggle switch is included to control the power supply to the Arduino Nano.

Component List

Arduino Nano

TP4056

OLED Display (128x32)

Toggle Switch

RTC Module

3.7V LiPo Battery

Wiring Details

Arduino Nano

TP4056

OLED Display (128x32)

Toggle Switch

RTC Module

3.7V LiPo Battery

Documented Code

/*
 * This Arduino Nano code was developed by newbiely.com
 *
 * This Arduino Nano code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/arduino-nano/arduino-nano-oled-clock
 */

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

#define OLED_WIDTH 128 // OLED display width,  in pixels
#define OLED_HEIGHT 32 // OLED display height, in pixels

Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
RTC_DS3231 rtc;

String time;

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

  // initialize OLED display with address 0x3C for 128x64
  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(2);          // 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);
  }

  // automatically sets the RTC to the date & time on PC this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  time.reserve(10); // to avoid fragmenting memory when using String
}

void loop() {
  DateTime now = rtc.now();

  time = "";
  time += now.hour();
  time += ':';
  time += now.minute();
  time += ':';
  time += now.second();

  oled_display_center(time);
}

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

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

  // center the display both horizontally and vertically
  oled.clearDisplay(); // clear display
  oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2);
  oled.println(text); // text to display
  oled.display();
}

This code initializes the OLED display and RTC module, then enters a loop where it continuously updates the display with the current time. The oled_display_center function is used to center the text on the OLED screen.