0.96 OLED inch display interfacing with ESP8266 NodeMCU

Interfacing OLED display to ESP8266 / NodeMCU

0.96 inch OLED display is 3.3v and 5v compatible tiny display that can be used to provide messages to users. We will use a DHT 11 sensor to measure humidity and temperature and display them on OLED display.

Left: Separate OLED Right: ESP with Inbuilt OLED


Please watch both videos to understand more on DHT11 and 0.96 inch OLED display.


Video: DHT11 sensor explained


Video: 0.96 inch 128 X 64 resolution OLED display explained

Parts Needed

1. DHT11 sensor
2. NodeMCU
3. 0.96 inch OLED display with 4 pins for i2c
4. Jumper wires

Circuit Diagram


NodeMCUDHT11 OLED 
3vVccVcc
GndGndGnd
D4OUT-
D1-SCL
D2-SDA


You will need to install following library to work with OLED.

Go to Tools -> Manage Libraries Search OLED.  I am using Adafruit SSD1306. Install all supporting packages as well.



 

Code

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

#include "DHT.h"

#define OLED_SCREEN_WIDTH 128 
#define OLED_SCREEN_HEIGHT 64 
#define DHTPIN D4
#define DHTTYPE DHT11 

DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(OLED_SCREEN_WIDTH, OLED_SCREEN_HEIGHT, &Wire, -1);

void setup() {
  // Wire.begin(D1,D2); // Uncomment if Your esp with inbuilt OLED display has SCA as D1 and SCL as D2
  Serial.begin(115200);
  dht.begin();

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
}

void loop() {
  delay(2000);
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  display.setCursor(0, 0);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("Sensorism");
  display.setTextSize(1);

  if (isnan(humidity) || isnan(temperature)) {
    const String errorMessage = "Failed to read from DHT sensor!";
    Serial.println(errorMessage);
    display.print(errorMessage);
    display.display();
    return;
  }

  Serial.print(F("Humidity: "));
  Serial.println(humidity);
  Serial.print(F("%  Temperature: "));
  Serial.print(temperature);
  Serial.println(F("°C "));

  
  display.print(F("Humidity: "));
  display.println(humidity);
  display.print(F("Temperature: "));
  display.print(temperature);
  display.println(F(" C "));
  display.display();
  delay(2000);
  display.clearDisplay();
}

Output

Troubleshooting

1. If your OLED display is not showing output, you might have to chagne the address and try again. 

 In "if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))"  change 0x3C to 0x3D.
More on I2c and how 7 bit address is determined for i2c can be found below.



2. DHT sensor is not giving measurements. Please check if you have a minimum of 2 secs between readings.

Comments

Popular posts from this blog

Microphone Sound Sensor explained | HW-484 | Interfacing with ESP 8266

[Experiment] DIY Robotic 2 wheel drive car using L298N motor driver and NodeMCU | ESP 8266

Using ESP 01 in Access Point (AP) mode to control ESP relay switch | ESP wifi tutorial | Part - 1