Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Hackster is hosting Impact Spotlights: Smart Home. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Smart Home. Stream on Thursday!
PCBX
Published

I2C Displays the Time and Date

Introducing I2C display project: Your sleek solution for displaying time and date effortlessly

BeginnerProtip330

Things used in this project

Story

Read more

Schematics

i2c_zUtxSBn7RX.png

Code

Untitled file

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

#define SCREEN_WIDTH 128 // OLED显示屏宽度,SSD1306 128x64
#define SCREEN_HEIGHT 64 // OLED显示屏高度

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

const int button1Pin = 2;
const int button2Pin = 3;

void setup() {
pinMode(button1Pin, INPUT_PULLUP); // 使用内置上拉
pinMode(button2Pin, INPUT_PULLUP); // 使用内置上拉

// SSD1306初始化
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 请确认I2C地址是否为0x3C
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
display.display(); // 显示初始化图像
delay(2000); // 防止困惑的短暂延迟
display.clearDisplay();
}

void loop() {
if (digitalRead(button1Pin) == LOW) {
displayDate();
} else if (digitalRead(button2Pin) == LOW) {
displayTime();
}
delay(200); // 消抖延迟
}

void displayDate() {
display.clearDisplay();
display.setTextSize(1); // 设置字号
display.setTextColor(SSD1306_WHITE); // 设置字体颜色
display.setCursor(0, 0); // 设置显示起始位置
display.println("Date:");
display.setCursor(0, 10);
display.println("2025/01/06");
display.display();
}

void displayTime() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Time:");
display.setCursor(0, 10);
display.println("12:34:56");
display.display();
}

Credits

PCBX
33 projects • 9 followers
Customer Success: Your one-stop solution for PCB and PCBA services, plus component sourcing. Enjoy FREE online simulation and EDA.
Contact

Comments

Please log in or sign up to comment.