What's new
Sure! Here's a wiring diagram and code for a 0.96 inch OLED display, an Arduino Nano, and an RTC DS3231 module. This code will display the current time and date on the OLED display.

Wiring Diagram:

OLED Display:

  • VCC to Arduino 5V
  • GND to Arduino GND
  • SCL to Arduino A5 (or SCL pin)
  • SDA to Arduino A4 (or SDA pin)

RTC DS3231:

  • VCC to Arduino 5V
  • GND to Arduino GND
  • SCL to Arduino A5 (or SCL pin)
  • SDA to Arduino A4 (or SDA pin)

Code:

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

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay();
  Wire.begin();
  rtc.begin();

  if (! rtc.isrunning()) {
    display.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    display.display();
    delay(2000);
  }
}

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

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.clearDisplay();
  display.println("Current Time:");
  display.setTextSize(3);
  display.print(now.hour());
  display.print(":");
  if (now.minute() < 10) {
    display.print("0");
  }
  display.println(now.minute());
  display.setTextSize(2);
  display.println("Date:");
  display.setTextSize(3);
  display.print(now.month());
  display.print("/");
  display.print(now.day());
  display.print("/");
  display.println(now.year());
  display.display();
  delay(1000);
}

This code uses the Adafruit_SSD1306 OLED driver library and RTClib library to control the OLED display and RTC module respectively.

In the setup() function, we initialize the OLED display and RTC module. If the RTC module is not running, we set the time to the compile time of the sketch.

In the loop() function, we get the current time from the RTC module using rtc.now(). We then display the time and date on the OLED display using the display.print() and display.println() functions.

I hope this helps! Let me know if you have any questions.
 

Similar threads

Back
Top