Tuesday, April 19, 2016

esp8266-OLED, esp8266-Arduino library for I2C-OLED displays


esp8266-OLED is an esp8266-Arduino library for I2C-OLED displays. This post show how to download and install to Arduino IDE, and test with example.

- It's assumed you are programming NodeMCU on Arduino Software, with ESP8266 core for Arduino installed.

- Connect I2C OLED to NodeMCU.


OLED VCC - NodeMCU 3v3
OLED GND - NodeMCU GND
OLED SCL - NodeMCU D1
OLED SDA - NodeMCU D2

(reamrk: the Fritzing parts of can OLED_SSD1306_I2C_128x64 can be download HERE)


- Add esp8266-OLED library to Arduino Software:
visit https://github.com/klarsys/esp8266-OLED, follow the steps to install the library:
  • Click on the Download ZIP button in the top right corner.
  • Uncompress it.
  • Rename the uncompressed folder to OLED.
  • Check that the OLED folder contains OLED.cpp and OLED.h files.
  • Place the OLED folder in your <arduinosketchfolder>/libraries/ folder - you may need to create the libraries subfolder if it is your first library.
  • Restart the IDE.

Open the example,
File > Examples > ESP8266-OLED Display Library > example

In order to match with our connection, we have to modify it to correct SDA and SCL pins:
change the code:
OLED display(2, 14);

to
OLED display(4, 5);

where 4, 5 correspond to NodeMCU D2 and D1. Refer to the "The pin definition" in NodeMCU - ESP8266/CP2102.

// Example sketch for testing OLED display

// We need to include Wire.h for I2C communication
#include <Wire.h>
#include "OLED.h"

// Declare OLED display
// display(SDA, SCL);
// SDA and SCL are the GPIO pins of ESP8266 that are connected to respective pins of display.
OLED display(4, 5);

void setup() {
  Serial.begin(9600);
  Serial.println("OLED test!");

  // Initialize display
  display.begin();

  // Test message
  display.print("Hello World");
  delay(3*1000);

  // Test long message
  display.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  delay(3*1000);

  // Test display clear
  display.clear();
  delay(3*1000);

  // Test message postioning
  display.print("TOP-LEFT");
  display.print("4th row", 4);
  display.print("RIGHT-BOTTOM", 7, 4);
  delay(3*1000);

  // Test display OFF
  display.off();
  display.print("3rd row", 3, 8);
  delay(3*1000);

  // Test display ON
  display.on();
  delay(3*1000);
}

int r = 0, c = 0;

void loop() {
  r = r % 8;
  c = micros() % 6;

  if (r == 0)
    display.clear();

  display.print("Hello World", r++, c++);

  delay(500);
}

Result:




Related:
- Another library of I2C OLED for ESP8266 core for Arduino - Adafruit SSD1306 library
- esp8266-oled-ssd1306 library

7 comments:

  1. Thanks so much!! I spent hours trying to get it working and this was so simple!

    I have a problem, after playing around (I have a lolin module) sudenly windows didn't recognice the 8266 any more but it does it with arduino. Seems that the conection is missed. Any ideas how to recover? or I just kill my module? (Was only conected to the USB and the OLED)

    Thanks a lot in advice

    ReplyDelete
  2. Thank you for this tutorial. Everything works. But I have one question,

    How to display variables ?

    For ex:

    String x = "Hello";

    display.print(x); // Throws an error.
    Same error for integers.

    Can you tell me how to display Strings & Numbers from variable using OLED.h?

    Thank you.
    Regards.

    ReplyDelete
    Replies
    1. I found the following solution to display variables with the OLED.h library, here is an example to display the temperature & humidity from DHT22 that worked for me:

      float h; // read the humidity value from the DHT22
      float t; // read the temperature value from the DHT22
      char bufHumi[5]; // array to store the humidity value to display on OLED ex: 20.40
      char bufTemp[5]; // array store the temperature value to display on OLED ex: 22.00
      String strTemp;
      String strHumi;

      code to display variable:

      h = dht.readHumidity(); // read temperature as float from DHT22
      t = dht.readTemperature();
      strTemp = String(t); // conversion of t as float to a string
      strHumi = String(h);
      strTemp.toCharArray(bufTemp,5); // conversion of t as string to array
      strHumi.toCharArray(bufHumi,5);
      display.print((bufTemp),5,1); // Display the Temperature value (array) on OLED
      display.print("Celcius",5,6);
      display.print((bufHumi),7,1); // Display the Humidity value (array)on OLED
      display.print("% Humidity",7,6);

      Hope that helps,
      Regards

      Delete
  3. Great Work but How I can change font size ?please inform me.

    ReplyDelete
  4. речь должна быть о SSD1106 какого хрена сдесь 1306 ?

    ReplyDelete
  5. hello , this is amazing , please help me to run a analog magnometer sensor with this way, thank you

    ReplyDelete