Arduino Nano - Relógio OLED

Este tutorial ensina você a criar um relógio OLED usando o Arduino Nano, um módulo RTC e um display OLED. O tutorial traz instruções para os módulos RTC DS3231 e DS1307. Em detalhes:

Você pode escolher um dos dois módulos RTC: DS3231 e DS1307. Para mais informações, consulte DS3231 vs DS1307.

Hardware Necessário

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×Cabo USB A para Mini-B
1×Display OLED I2C SSD1306 128x64
1×Display OLED I2C SSD1306 128x32
1×Módulo Relógio de Tempo Real (RTC) DS3231
1×(Opcional) Real-Time Clock DS1307 Module
1×Bateria CR2032
1×Protoboard
1×Fios Jumper
1×(Recomendado) Placa de Expansão com Bornes de Parafuso para Arduino Nano
1×(Recomendado) Placa de Expansão Breakout para Arduino Nano
1×(Recomendado) Divisor de Alimentação para Arduino Nano

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Divulgação: Alguns dos links desta seção são links de afiliado da Amazon. Podemos receber uma comissão pelas compras feitas por meio desses links, sem custo adicional para você. Agradecemos o seu apoio.

Buy Note: If you want a bigger OLED display, use the 2.42 inch OLED Display 128x64 .

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

Se você não conhece o OLED, o DS3231 e o DS1307 (pinagem, como funcionam, como programar ...), os tutoriais a seguir podem ajudar:

Instalar as Bibliotecas do OLED e do RTC

  • Clique no ícone Libraries na barra à esquerda da Arduino IDE.
  • Pesquise por “SSD1306” e localize a biblioteca SSD1306 da Adafruit.
  • Pressione o botão Install para concluir a instalação.
Biblioteca OLED para Arduino Nano
  • Você será solicitado a instalar algumas outras dependências da biblioteca.
  • Para instalar todas elas, clique no botão Install All.
Biblioteca Adafruit GFX para Arduino Nano
  • Pesquise por “RTClib” e localize a biblioteca de RTC criada pela Adafruit. Essa biblioteca é compatível com o DS3231 e com o DS1307.
  • Pressione o botão Install para instalar a biblioteca de RTC.
Biblioteca RTC para Arduino Nano

Lendo a hora do módulo RTC DS3231 e exibindo no OLED

Diagrama de Ligação

Diagrama de ligação do DS3231 e OLED com Arduino Nano

This image is created using Fritzing. Click to enlarge image

Código Arduino Nano - DS3231 e OLED

/* * Este código de Arduino Nano foi desenvolvido por pt.newbiely.com * Este código de Arduino Nano é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.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 64 // 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(1); // 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(); }

Passos Rápidos

  • Copie o código e abra-o na Arduino IDE.
  • Clique no botão Upload na Arduino IDE para enviar o código ao Arduino Nano.
  • Coloque o sensor em água quente e fria ou segure-o na mão.
  • Veja os resultados no OLED.

Lendo a hora do módulo RTC DS1307 e exibindo no OLED

Diagrama de Ligação

Diagrama de ligação do DS1307 e OLED com Arduino Nano

This image is created using Fritzing. Click to enlarge image

Veja A melhor forma de alimentar o Arduino Nano e outros componentes.

Código Arduino Nano - DS1307 e OLED

/* * Este código de Arduino Nano foi desenvolvido por pt.newbiely.com * Este código de Arduino Nano é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.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 64 // OLED display height, in pixels Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // // create SSD1306 display object connected to I2C RTC_DS1307 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(1); // 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(); }

Passos Rápidos

  • Copie o código e abra-o na Arduino IDE.
  • Clique no botão Upload na Arduino IDE para enviar o código ao Arduino Nano.
  • Coloque o sensor em água quente e fria, ou segure-o na mão.
  • Veja o resultado no OLED.

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!