ESP8266 0.96 inch OLED Display with Arduino IDE

This post shows how to use the 0.96 inch OLED display with the ESP8266 using the Arduino IDE.

(There is also a post about how to use the OLED display with the Arduino).

Introducing the 0.96 inch OLED display

The OLED display is the one in the following figure:

dsc09476

This is a very small display that is made of 128 by 64 individual OLED pixels and no backlight is required. This display uses I2C communication. This means that it can communicate with the ESP8266 using just 2 pins.

Libraries

To control the OLED display, you will need to install the esp8266 oled ssd1306 library.

Installing the esp8266 oled ssd1306 library

  1. Click here to download the esp8266 oled ssd1306 library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get esp8266-oled-ssd1306-master folder
  3. Rename your folder from esp8266-oled-ssd1306-master to esp8266_oled_ssd1306
  4. Move the esp8266_oled_ssd1306 folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Example: Display the temperature and humidity in the OLED display

In this example you will display the temperature and humidity in the OLED display. This is just an example for you to understand how to integrate the OLED display with your ESP8266 using the Arduino IDE.

Build home automation projects with the ESP8266 Wi-Fi dev board using Arduino IDE and Lua firmware DOWNLOAD »

Build home automation projects with the ESP8266 Wi-Fi dev board using Arduino IDE and Lua firmware DOWNLOAD »

The idea of using the OLED display with the ESP8266 is to ilustrate how you can create a physical user interface for your ESP (you can also add web server capabilities to this project).

The temperature and humidity will be measured using the DHT11 temperature and humidity sensor.

dht-11

If you’re not familiar with the DHT11 sensor I recommend that you check out the following blog posts:

Parts required

For this example you’ll need the following components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

Assemble the circuit by following the schematics below.

display-dht11-oled_bb

Code

Before uploading the code, make sure you’ve installed all the necessary libraries. Note that for this example, you also need to install the DHT library.

Installing the DHT sensor library

  1. Click here to download the DHT-sensor-library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get DHT-sensor-library-master folder
  3. Rename your folder from DHT-sensor-library-master to DHT_sensor_library (you really need ro replace those “-” by “_”)
  4. Move the DHT_sensor_library folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Here’s the code that you should upload to your ESP8266.

/*
 * Random Nerd Tutorials - Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 * 
 * The MIT License (MIT)
 * Copyright (c) 2016 by Daniel Eichhorn
 */
 
// Include the correct display library
// For a connection via I2C using Wire include
#include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
// or #include "SH1106.h" alis for `#include "SH1106Wire.h"`
// For a connection via I2C using brzo_i2c (must be installed) include
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
// #include "SSD1306Brzo.h"
// #include "SH1106Brzo.h"
// For a connection via SPI include
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
// #include "SSD1306Spi.h"
// #include "SH1106SPi.h"

#include <DHT.h>

#define DHTPIN 5     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11

// Initialize the OLED display using SPI
// D5 -> CLK
// D7 -> MOSI (DOUT)
// D0 -> RES
// D2 -> DC
// D8 -> CS
// SSD1306Spi        display(D0, D2, D8);
// or
// SH1106Spi         display(D0, D2);

// Initialize the OLED display using brzo_i2c
// D3 -> SDA
// D5 -> SCL
// SSD1306Brzo display(0x3c, D3, D5);
// or
// SH1106Brzo  display(0x3c, D3, D5);

// Initialize the OLED display using Wire library
SSD1306  display(0x3c, D3, D5);
// SH1106 display(0x3c, D3, D5);

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

void setup(){
  // Initialising the UI will init the display too.
  display.init();

  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_16);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  dht.begin(); // initialize dht
}

void displayTempHumid(){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)){
    display.clear(); // clearing the display
    display.drawString(5,0, "Failed DHT");
    return;
  }
  display.clear();
  display.drawString(0, 0, "Humidity: " + String(h) + "%\t"); 
  display.drawString(0, 16, "Temp: " + String(t) + "C"); 
  display.drawString(0, 32, "Temp: " + String(f) + "F"); 
}
void loop(){
  displayTempHumid();
  display.display();
  delay(2000);
}

View raw code

Demonstration

Here’s what you will have in the end.

demonstration-esp8266-oled

Download our Free eBooks and Resources

Wrapping up

I hove you’ve found this post useful.

If you want to use the OLED display with the Arduino check the following post:

If you like this post probably you might like my next ones, so make sure you subscribe the RNT blog.

Thanks for reading,

Rui


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

18 thoughts on “ESP8266 0.96 inch OLED Display with Arduino IDE”

  1. Thats great. I actually need a new temp/humidity sensor and it would be nice to push the readings up to my server to read from my web app. I had heard you can write code to a ESP8266 without having to use AT commands, but haven’t looked it up yet. NodeMCU looks awesome. One question though? Where is the code to upload to the ESP8266? I don’t see it in the post.

  2. Hi – you have not shown any link (*or code) in the above 0.96 OLED / ESP8266 PROJECT; where you say “……… Here’s the [[Arduino??]] code that you should upload to your ESP8266 ……..”????? can you please provide it. Regards, Ian

  3. Hi Rui – I am just starting projects using the oled display and this is the first one I landed on – I have your Arduino projects book already. I am trying to compile the code (have all the libraries installed) and am getting an error on the display statement

    // Initialize the OLED display using Wire library
    SSD1306  display(0x3c, D3, D5);

    It is saying that “D3 and D5 are not declared in this scope”

    Can you tell me what might be going on?

    Thanks Bob

    • Hi Bob.
      The D3 and D5 are only declared “automatically” when you’re using the ESP-12E (in the Arduino IDE tools > boards > ESP-12E).
      If you’re using another ESP or you have “ESP generic module” you’ll have to declare GPIOs.

      I hope that makes sense.
      Regards.

    • Hi Andrew.
      I don’t know why you can’t see the code.
      Try this link with the raw code: raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/ESP8266_Oled_display.ino
      Regards,
      Sara

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.