i2c OLED on ESP8266

Not exactly revolutionary stuff, but getting an OLED running is a spiritual experience if you’ve never done it before. And actually, I never get tired of it myself.

Here’s the quick and dirty guide on running your own OLED code.

Step One: Install Libraries

You may already have these installed, but if not, go to SKETCH >> INCLUDE LIBRARY >> MANAGE LIBRARIES… and download the following:

  • Adafruit_GFX
  • Adafruit_SSD1306

Step Two: Wiring

Don’t mess up GND and 3V3. I write this to make you feel bad when you do it one day, regardless of how experienced you are (speaking from experience…)

Step Three: Code

Plug you NodeMCU or whatever in, check the COM port (TOOLS >> PORT… >>> COMX whatever), put the following code into the editor and upload. If it give you a warning like:

esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

…just relax. Upload again, adn this time when it starts saying Connecting……..__, hold the BOOT/FLASH button, and press the RESET button on the device.

Do this a few times if your timing isn’t great. You can’t really break it this way, but its a bit fiddly, and you should come right.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(128, 64, &Wire, LED_BUILTIN);

void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  testdrawtriangle();
  testfilltriangle();
  display.invertDisplay(true);
  delay(1000);
  display.invertDisplay(false);
  delay(1000);

  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(F("Consider buying Paul"));
  display.println(F("some coffee. He was"));
  display.println(F("up till super late "));
  display.println(F("testing and making"));
  display.println(F("for all to enjoy."));
  display.display();
  delay(6000);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(F("Or just subscribe to his instagram : "));
  display.setCursor(0,30);
  display.println(F("@paulhoets"));
  display.display();
  delay(6000);

}

  void testdrawtriangle(void) {
  display.clearDisplay();

  for (int16_t i = 0; i < max(display.width(), display.height()) / 2; i += 5) {
  display.drawTriangle(
  display.width() / 2  , display.height() / 2 - i,
  display.width() / 2 - i, display.height() / 2 + i,
  display.width() / 2 + i, display.height() / 2 + i, SSD1306_WHITE);
  display.display();
  delay(1);
}

  delay(2000);
}

  void testfilltriangle(void) {
  display.clearDisplay();

  for (int16_t i = max(display.width(), display.height()) / 2; i > 0; i -= 5) {
  display.fillTriangle(
  display.width() / 2  , display.height() / 2 - i,
  display.width() / 2 - i, display.height() / 2 + i,
  display.width() / 2 + i, display.height() / 2 + i, SSD1306_INVERSE);
  display.display();
  delay(1);
}
  delay(2000);
}

Enjoy. More to come!

Paul Hoets is a freelance maker who lives in South Korea. If you liked this article and would like to contribute to his empire of dirt, silicon and tech. education, buy him a coffee!