Adding an RTC and OLED to ESP8266-01

dsc_0024Adding an RTC and OLED to ESP8266-01 is fairly easy with I2C. The ESP8266-01 has 4 I/O pins that can be used for  I2C. using the GPIO0 and GPIO2 for sda resp scl is more or less standard.

esp8266-rtcoledI presume you do know how to program the ESP8266. In short: Connect Tx<->Rx (meaning the Tx of your ESP to the Rx of your USB-TTL converter)
Connect Rx<->Tx Connect CH_PD<->Vcc Connect GPIO0 <->Grnd
As both modules already had pull up resistors, I didnt need to add those. If your modules do not, add 4k7 resistors as pull up on the SDA and SCL lines.

/* ************************************
 Read the time from RTC and display on OLED
 with an ESP8266<br> sda=0, scl=2
* *************************************/

// Libraries
#include <Wire.h>
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include "RTClib.h" //  Lady Ada
//Object declarations
RTC_DS1307 rtc;            // RTC
SSD1306  display(0x3c, 0, 2);//0x3C being the usual address of the OLED

//Month and Day Arrays. Put in Language of your choice, omitt the 'day' part of the weekdays
char *maand[] =
  "Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"
char *dagen[] = {"Zon", "Maan", "Dins", "Woens", "Donder", "Vrij", "Zater" };

// date and time variables
byte m = 0;    // contains the minutes, refreshed each loop
byte h = 0;    // contains the hours, refreshed each loop
byte s = 0;    // contains the seconds, refreshed each loop
byte mo = 0;   // contains the month, refreshes each loop
int j = 0;     // contains the year, refreshed each loop
byte d = 0;    // contains the day (1-31)
byte dag = 0;  // contains day of week (0-6)

void setup() {
  Wire.pins(0, 2);// yes, see text
  Wire.begin(0,2);// 0=sda, 2=scl

// reading of time here only necessary if you want to use it in setup
  DateTime now =;
  dag = now.dayOfTheWeek();
  j = now.year();
  mo = now.month();
  d =;
  h = now.hour();
  m = now.minute();
  s = now.second();
  DateTime compiled = DateTime(__DATE__, __TIME__);
  if (now.unixtime() < compiled.unixtime())
    Serial.print(F("Current Unix time"));
    Serial.print(F("Compiled Unix time"));
    Serial.println("RTC is older than compile time! Updating");
    // following line sets the RTC to the date & time this sketch was compiled<br>   // uncomment to set the time
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  // Initialise the display.
  display.flipScreenVertically();// flipping came in handy for me with regard 
                                                                // to screen position


void loop() {
  DateTime now =;
  dag = now.dayOfTheWeek();
  j = now.year();
  mo = now.month();
  d =;
  h = now.hour();
  m = now.minute();
  s = now.second();

  String t = String(h) + ":" + String(m) + ":" + String(s);
  String t2 = String(d) + ":"  + String(mo) + ":" + String(j);
  display.drawString(0, 10, t);//
  display.drawString(0, 24, t2);
  display.drawString(0, 38, maand[mo - 1]);
  String d = dagen[dag];
  d = d + "dag";//adding the word 'dag' (=day)  to the names of the days
  display.drawString(0, 52, d);
  // write the buffer to the display

The code is fairly straightforward but it does contain some peculiarities.
I make a call to ‘Wire.pins(sda,scl)’. That seems redundant and in fact the call was deprecated, but apparently if any other library would make a call to ‘Wire()’ the proper definition of the pins for the sda and scl can get lost. So I left them both in for safety.
If you still have an old RTCLib you may get an error on ‘dayOfTheWeek’. That is because it used to be called ‘dayOfWeek’ but it got changed: update your library.

The last line, with the day on it, may be just a bit too much size for your OLED: set the font smaller (say ‘Plain_10’) and alter the print positions (the second digits in the display.drawString(0, x, string); statements)

4 thoughts on “Adding an RTC and OLED to ESP8266-01

    1. it is kinda hard to understand what you see as line 9 as I dont know if you include blanks or comments, but I dont really see a mistake anywhere, unless you refer to the ‘br’ codes. Those are html codes that WordPress seems to keep putting in randomly when one uses the ‘pre’ tag to prevent code from being formatted. I have taken them out but they may pop up again. Which line do you refer to?

    1. Tnx Jeroen. Currently an RTC and display on GPIO0 and GPIO2 (SDA-SCL I2C) a DHT11 on GPIO4 (tx) and DS18B20 on GPIO3 (Rx).
      Just to see if possible. Perhaps BME280 on I2C easier (pressure, humidity and temp) but ‘easy’ wasnt the point🙂
      Anyway, with I2C the world opens. Have an ADC coming and still a BMP180 that wants to be attached.
      Now having said that… I probably would not buy the 01 anymore as the 12 has so many more pins and is hardly more expensive and for development the NodeMCU / Wemos D1 is much easier (has a USB on board).
      But for a small final application with I2C the 01 is easiest

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s