在这个项目中,我们将研究如何在 LCD 屏幕上显示时间和日期。这个项目对于制作闹钟或时钟非常有用。你只需在 Arduino 板上添加电池,然后用 3d 打印机打印一个盒子,就能拥有一个真正的独立时钟。

难度 :

所需材料

现在我们来看看项目所需的硬件:

  • Arduino Uno 板
  • 一个 16×2 液晶屏
  • 一个电位器
  • 一个 220 欧姆的电阻器
  • 连接线(约 15 根)

Arduino 时钟图

Schéma horloge Arduino

电位器的用途是什么?

电位器用于调节屏幕亮度。您可以不使用电位器,直接将 5V 电压连接到棕色导线上,从电位器到屏幕的 GND 可以直接连接到 Arduino 电路板上。

Arduino 时钟程序

#include <LiquidCrystal.h> // Library for LCD Screen

int time_date[6] = {10, 34, 18, 18, 10, 2022}; //second minute hour day month year
int limits[6] = {60, 60, 24, 31, 12, 3000}; // increment limits
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the library with the connected pins

void setup() {
  lcd.begin(16, 2); // Initialize the LCD with its dimensions
}

void loop() {
  time_date[0]++; // time increment
    for (int i = 0; i < 6 ; i++){
      if (time_date[i] == limits[i]) { // check limit overflow
        if (i<5) {time_date[i+1]++;}
        time_date[i] = 0;
      }
    }
    lcd.setCursor(0,0); // Position cursor on first line
    for (int j = 3; j < 5; j++) { // display date
      if (time_date[j]<10){
        lcd.print("0");
        lcd.print(time_date[j]);
      }
        else {lcd.print(time_date[j]);}
      lcd.print("/");    
    }
    lcd.print(time_date[5]); 
    lcd.setCursor(0,1); // Position cursor on second line
    for (int j = 2; j >= 0; j--) { // display time
      if (time_date[j]<10)
      {
        lcd.print("0");
        lcd.print(time_date[j]);
      }else {lcd.print(time_date[j]);}
      if (j != 0){
        lcd.print(":"); 
      }   
    }
    delay(250);
}

更改程序中的时间和日期!

如图所示,开始日期和时间是固定的,不一定在正确的日子。因此,您可以在程序中自行更改日期。

下面是需要更改的一行:

int time_date[6] = {10, 34, 18, 18, 10, 2022}; //second, minute, hour, day, month, year

如何在 Arduino 关机时保持当前时间?

如您所见,一旦关闭 Arduino 板,时钟和日期就不会继续更新。例如,如果您正在制作一个时钟,这可能会很烦人。为了解决这个问题,您可以在项目中添加一个 rtc 时钟模块(如 DS3231),由于电池独立于 Arduino 板的电源,即使 Arduino 板关闭,它也能计算时间。

模拟

下面是项目模拟: