ESP8266 - reloj OLED

Este tutorial te enseña cómo crear un reloj OLED usando ESP8266, un módulo RTC y una pantalla OLED. El tutorial proporciona instrucciones para los módulos RTC DS3231 y DS1307. En detalle:

Puede elegir uno de dos módulos RTC: DS3231 y DS1307. Para obtener más información, consulte DS3231 vs DS1307.

Hardware Requerido

1×ESP8266 NodeMCU
1×Cable USB Tipo-A a Tipo-C (para PC USB-A)
1×Cable USB Tipo-C a Tipo-C (para PC USB-C)
1×Pantalla OLED I2C SSD1306 128x64
1×Pantalla OLED I2C SSD1306 128x32
1×Módulo Reloj de Tiempo Real (RTC) DS3231
1×(Opcional) Real-Time Clock DS1307 Module
1×Batería CR2032
1×Protoboard
1×Cables Puente
1×(Recomendado) Placa de Expansión de Terminales de Tornillo para ESP8266
1×(Recomendado) Divisor de Alimentación para ESP8266 Tipo-C

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Divulgación: Algunos de los enlaces proporcionados en esta sección son enlaces de afiliado de Amazon. Podemos recibir una comisión por las compras realizadas a través de estos enlaces sin costo adicional para usted. Apreciamos su apoyo.

Acerca del módulo RTC OLED, DS3231 y DS1307

Si no estás familiarizado con OLED, DS3231 y DS1307 (diagrama de pines, funcionamiento, programación...), los siguientes tutoriales pueden ayudarte:

Instalar bibliotecas OLED y RTC

  • Haz clic en el icono de Bibliotecas en la barra izquierda del IDE de Arduino.
  • Busca “SSD1306” y localiza la biblioteca SSD1306 de Adafruit.
  • Luego, pulsa el botón Instalar para completar la instalación.
Librería OLED para ESP8266 NodeMCU
  • Se le pedirá que instale dependencias de bibliotecas adicionales.
  • Para instalarlas todas de una vez, haga clic en el botón Instalar todo.
Biblioteca de sensores Adafruit GFX para ESP8266 NodeMCU
  • Busca “RTClib” y localiza la biblioteca RTC de Adafruit. Esta biblioteca es compatible con ambos DS3231 y DS1307.
  • Presiona el botón Instalar para instalar la biblioteca RTC.
Biblioteca RTC para ESP8266 NodeMCU

Lectura de la hora desde el módulo RTC DS3231 y mostrarla en la pantalla OLED

Diagrama de cableado

Diagrama de cableado ESP8266 NodeMCU DS3231 OLED

This image is created using Fritzing. Click to enlarge image

Para obtener m\u00e1s informaci\u00f3n, consulte Pines del ESP8266 y c\u00f3mo alimentar ESP8266 y otros componentes.

Código ESP8266 - DS3231 y OLED

/* * Este código de ESP8266 NodeMCU fue desarrollado por es.newbiely.com * Este código de ESP8266 NodeMCU se proporciona al público sin ninguna restricción. * Para tutoriales completos y diagramas de cableado, visite: * https://es.newbiely.com/tutorials/esp8266/esp8266-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(); }

Pasos R\u00e1pidos

Para empezar con ESP8266 en Arduino IDE, sigue estos pasos:

  • Consulta el tutorial cómo configurar el entorno para ESP8266 en Arduino IDE si es tu primera vez usando ESP8266.
  • Conecta los componentes tal como se muestran en el diagrama.
  • Conecta la placa ESP8266 a tu ordenador utilizando un cable USB.
  • Abre Arduino IDE en tu ordenador.
  • Elige la placa ESP8266 correcta, como (p. ej. NodeMCU 1.0 (ESP-12E Module)), y su puerto COM correspondiente.
  • Copia el código y ábrelo con el Arduino IDE.
  • Haz clic en el botón Subir en el Arduino IDE para enviar el código al ESP8266.
  • Coloca el sensor en agua caliente y fría, o sostenlo en tu mano.
  • Consulta el resultado en la pantalla OLED.

Lectura de la hora desde el módulo RTC DS1307 y mostrarla en OLED

Diagrama de cableado

Diagrama de cableado ESP8266 NodeMCU DS1307 OLED

This image is created using Fritzing. Click to enlarge image

Código ESP8266 - DS1307 y OLED

/* * Este código de ESP8266 NodeMCU fue desarrollado por es.newbiely.com * Este código de ESP8266 NodeMCU se proporciona al público sin ninguna restricción. * Para tutoriales completos y diagramas de cableado, visite: * https://es.newbiely.com/tutorials/esp8266/esp8266-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(); }

Pasos R\u00e1pidos

  • Conecta los componentes como se muestra en el diagrama.
  • Conecta la placa ESP8266 a tu computadora usando un cable USB.
  • Abre el IDE de Arduino en tu computadora.
  • Elige la placa ESP8266 correcta, como (p. ej. NodeMCU 1.0 (Módulo ESP-12E)), y su puerto COM respectivo.
  • Copia el código y ábrelo en el IDE de Arduino.
  • Haz clic en el botón Subir para transferir el código al ESP8266.
  • Coloca el sensor en agua caliente y en agua fría, o sostenlo en tu mano.
  • Verifica el resultado en el OLED.

Video Tutorial

Estamos considerando crear tutoriales en video. Si considera que los tutoriales en video son importantes, suscríbase a nuestro canal de YouTube para motivarnos a crear los videos.

※ NUESTROS MENSAJES

  • No dude en compartir el enlace de este tutorial. Sin embargo, por favor no use nuestro contenido en otros sitios web. Hemos invertido mucho esfuerzo y tiempo en crear el contenido, ¡por favor respete nuestro trabajo!