Arduino - Relógio LCD

Neste tutorial, vamos aprender a fazer um relógio LCD:

Você pode escolher um dos dois módulos RTC: DS3231 ou DS1307. Veja DS3231 vs DS1307

Sobre o LCD e os módulos RTC DS3231 e DS1307

Se você ainda não conhece o LCD, o DS3231 e o DS1307 (pinagem, como funcionam, como programar...), aprenda sobre eles nos tutoriais a seguir:

Instalar as Bibliotecas de LCD e RTC

  • Vá até o ícone Libraries na barra à esquerda do Arduino IDE.
  • Pesquise “DIYables LCD I2C” e encontre a biblioteca DIYables_LCD_I2C da DIYables
  • Clique no botão Install para instalar a biblioteca DIYables_LCD_I2C.
Biblioteca DIYables LCD I2C para Arduino
  • Pesquise “RTClib” e encontre a biblioteca RTC da Adafruit. Esta biblioteca funciona tanto com o DS3231 quanto com o DS1307
  • Clique no botão Install para instalar a biblioteca RTC.
Biblioteca RTC para Arduino
  • Pode ser que seja pedido para você instalar algumas outras dependências de biblioteca
  • Clique no botão Install All para instalar todas as dependências de biblioteca.
Biblioteca de dependência RTC para Arduino

Lendo a hora do módulo RTC DS3231 e mostrando-a no LCD

Diagrama de Ligação

Diagrama de Ligação do Arduino com DS3231 e LCD

This image is created using Fritzing. Click to enlarge image

Código Arduino - DS3231 e LCD

/* * Este código de Arduino foi desenvolvido por pt.newbiely.com * Este código de Arduino é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/tutorials/arduino/arduino-lcd-clock */ #include <DIYables_LCD_I2C.h> #include <RTClib.h> DIYables_LCD_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows RTC_DS3231 rtc; void setup() { Serial.begin(9600); lcd.init(); // initialize the lcd lcd.backlight(); // open the backlight // 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__))); } void loop() { DateTime now = rtc.now(); int year = now.year(); int month = now.month(); int day = now.day(); int hour = now.hour(); int minute = now.minute(); int second = now.second(); lcd.clear(); lcd.setCursor(0, 0); // start to print at the first row lcd.print("Date: "); lcd.print(year); lcd.print("/"); lcd.print(month); lcd.print("/"); lcd.print(day); lcd.setCursor(0, 1); // start to print at the second row lcd.print("Time: "); lcd.print(hour); lcd.print(":"); lcd.print(minute); lcd.print(":"); lcd.print(second); delay(1000); // Update every second }

Passos Rápidos

  • Copie o código acima e abra no Arduino IDE
  • Clique no botão Upload no Arduino IDE para carregar o código no Arduino
  • Veja o resultado no LCD

Lendo a hora do módulo RTC DS1307 e mostrando-a no LCD

Diagrama de Ligação

Diagrama de Ligação do Arduino com DS1307 e LCD

This image is created using Fritzing. Click to enlarge image

Código Arduino - DS1307 e LCD

/* * Este código de Arduino foi desenvolvido por pt.newbiely.com * Este código de Arduino é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/tutorials/arduino/arduino-lcd-clock */ #include <DIYables_LCD_I2C.h> #include <RTClib.h> DIYables_LCD_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows RTC_DS1307 rtc; void setup() { Serial.begin(9600); lcd.init(); // initialize the lcd lcd.backlight(); // open the backlight // 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__))); } void loop() { DateTime now = rtc.now(); int year = now.year(); int month = now.month(); int day = now.day(); int hour = now.hour(); int minute = now.minute(); int second = now.second(); lcd.clear(); lcd.setCursor(0, 0); // start to print at the first row lcd.print("Date: "); lcd.print(year); lcd.print("/"); lcd.print(month); lcd.print("/"); lcd.print(day); lcd.setCursor(0, 1); // start to print at the second row lcd.print("Time: "); lcd.print(hour); lcd.print(":"); lcd.print(minute); lcd.print(":"); lcd.print(second); delay(1000); // Update every second }

Passos Rápidos

  • Copie o código acima e abra no Arduino IDE
  • Clique no botão Upload no Arduino IDE para carregar o código no Arduino
  • Veja o resultado no LCD

Vídeo

※ NOSSAS MENSAGENS

  • Fique à vontade para compartilhar o link deste tutorial. Porém, por favor, não use nosso conteúdo em outros sites. Investimos muito esforço e tempo para criar este conteúdo, por favor respeite nosso trabalho!